unity3d TextMesh清晰字体


直接出效果如图所示,由于unity3d中字体在3d中显示十分的渣,要想在场景中显示比较清晰的字体比较困难。
好在unity插件扩展性,让过去的不可能变成 了可能 。

要使用的工具如下:
  1、抽取文字软件:BMFont字体制作工具
  2、免费插件:bitmapFontImpoter

制作的原理是将文字转换为清晰的图片,将对应的座标记录下来保存在一相指定格式的文件中,然后通过插件将对应的文字图片座标写入到unity 支持的字体Rect中、

下载好之后,就开始来使用吧:

一、安装软件
   将要使用的字体类型安装到电脑中:
  TTF格式的字体双击就可以支持安装
二、录入文字
  将需要使用到的文字保存一个Text文档中
  不需要考虑是否有重复的问题
  注意将文本保存为UTF-8格式(只是英文就随意了)
三、导入字库
   打开BitMapFont制作工具
   选择Editor中的从文件导入,并导入刚刚配制的文档
 

四、配制
    打开Option中的FontSetting 和 ExportOptions选项进行配制
    可以设置字体大小字体格式等,如果想要文字清晰就要调大字体
  

五、导出文字图片和信息
   选择Saveg bitMap Font as ...可以将制作好的图片和信息保存起来

六、在Unity3d中导入字体制作插件
  
七、使用字体
    生成的font字体就可以直接用了
    注意可以设置方字大小,但斜体和加粗貌似不能使用了

八、设置字体间距
如果用于UGUI,可以使用以下脚本(和Outline,Shadow用法相同):
原文:
unity 5.3.4可用
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;

[AddComponentMenu( "UI/Effects/Letter Spacing", 14)]
public class LetterSpacing : BaseMeshEffect
{
    [SerializeField]
    private float m_spacing = 0f;

    protected LetterSpacing() { }

#if UNITY_EDITOR
    protected override void OnValidate()
    {
        spacing = m_spacing;
        base.OnValidate();
    }
#endif

    public float spacing
    {
        get { return m_spacing; }
        set
        {
            if (m_spacing == value) return;
            m_spacing = value;
            if (graphic != null) graphic.SetVerticesDirty();
        }
    }

    public override void ModifyMesh( VertexHelper helper)
    {
        List< UIVertex> verts = new List< UIVertex>();
            helper.GetUIVertexStream(verts);
        Debug.Log(verts.Count);
        if (!IsActive()) return;

        Text text = GetComponent< Text>();
        if (text == null)
        {
            Debug.LogWarning( "LetterSpacing: Missing Text component" );
            return;
        }

        string[] lines = text.text.Split( '\n');
        Vector3 pos;
        float letterOffset = spacing * ( float)text.fontSize / 100f;
        float alignmentFactor = 0;
        int glyphIdx = 0;

        switch (text.alignment)
        {
            case TextAnchor.LowerLeft:
            case TextAnchor.MiddleLeft:
            case TextAnchor.UpperLeft:
                alignmentFactor = 0f;
                break;

            case TextAnchor.LowerCenter:
            case TextAnchor.MiddleCenter:
            case TextAnchor.UpperCenter:
                alignmentFactor = 0.5f;
                break;

            case TextAnchor.LowerRight:
            case TextAnchor.MiddleRight:
            case TextAnchor.UpperRight:
                alignmentFactor = 1f;
                break;
        }

        for ( int lineIdx = 0; lineIdx < lines.Length; lineIdx++)
        {
            string line = lines[lineIdx];
            float lineOffset = (line.Length - 1) * letterOffset * alignmentFactor;
            for ( int charIdx = 0; charIdx < line.Length; charIdx++)
            {
                int idx1 = glyphIdx * 6 + 0;
                int idx2 = glyphIdx * 6 + 1;
                int idx3 = glyphIdx * 6 + 2;
                int idx4 = glyphIdx * 6 + 3;
                int idx5 = glyphIdx * 6 + 4;
                int idx6 = glyphIdx * 6 + 5;


                // Check for truncated text (doesn't generate verts for all characters)
                if (idx4 > verts.Count - 1) return;

                UIVertex vert1 = verts[idx1];
                UIVertex vert2 = verts[idx2];
                UIVertex vert3 = verts[idx3];
                UIVertex vert4 = verts[idx4];
                UIVertex vert5 = verts[idx5];
                UIVertex vert6 = verts[idx6];


                pos = Vector3.right * (letterOffset * charIdx - lineOffset);

                vert1.position += pos;
                vert2.position += pos;
                vert3.position += pos;
                vert4.position += pos;
                vert5.position += pos;
                vert6.position += pos;


                verts[idx1] = vert1;
                verts[idx2] = vert2;
                verts[idx3] = vert3;
                verts[idx4] = vert4;
                verts[idx5] = vert5;
                verts[idx6] = vert6;


                glyphIdx++;
            }

            // Offset for carriage return character that still generates verts
            glyphIdx++;
        }
        helper.AddUIVertexTriangleStream(verts);
    }
}


  
发布了32 篇原创文章 · 获赞 12 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章