Untiy-文本转Texture

来源:互联网 发布:淘宝数据采集器破解版 编辑:程序博客网 时间:2024/06/07 23:25

网上找了很久,没有找到比较现成的方案,于是自己做了以种方案,由于比较急,所以自由度不是很高,希望以后能找到更好的方法~

其中一篇文章:http://blog.almostlogical.com/2010/08/20/adding-text-to-texture-at-runtime-in-unity3d-without-using-render-texture/

1.首先用BMFont生成所需文件

参考:http://www.cnblogs.com/hejianchun/articles/3022732.html

2.利用Unity生成自定义字体CustomFont

参考:http://blog.sina.com.cn/s/blog_89d90b7c0102vk20.html

原博主没有给出BMFontReader的库,我这里收集了一下打包放出来
[链接: https://pan.baidu.com/s/1hsjqNeG 密码: rtaq]

另外响应原博主号召,把自定义字体的数据导入功能写成了菜单,使用起来方便一些:

using UnityEngine;using System.Collections;using UnityEditor;using UnityEngine.UI;public class MakeCustomFont : EditorWindow {    static MakeCustomFont() {       }      [MenuItem("Function/Make CustomFont")]      private static void ShowWindow() {          MakeCustomFont cw=   EditorWindow.GetWindow<MakeCustomFont>(true, "Make CustomFont");      }      TextAsset txt;    static TextAsset sTxt;      Material png;      static Material sPng;      Font font;    static Font sFont;    private void OnGUI() {          GUILayout.Space(10);          GUILayout.Label("描述文件:");          txt = (TextAsset)EditorGUILayout.ObjectField(txt, typeof(TextAsset), true, GUILayout.MinWidth(100f));          sTxt = txt;        GUILayout.Space(10);          GUILayout.Label("材质:");          png = (Material)EditorGUILayout.ObjectField(png,typeof(Material),true, GUILayout.MinWidth(100f));          sPng = png;        GUILayout.Space(10);         GUILayout.Label("字体:");          font = (Font)EditorGUILayout.ObjectField(font,typeof(Font),true, GUILayout.MinWidth(100f));          sFont = font;        GUILayout.Space(10);         if (GUILayout.Button("创建字体!")) {              Change();          }      }      public static void Change() {          if (sTxt == null || sPng == null || sFont == null) {            Debug.LogError ("不完整的参数");            return;        }        BMFont mbFont = new BMFont();          BMFontReader.Load(mbFont, sTxt.name, sTxt.bytes);  // 借用NGUI封装的读取类        CharacterInfo[] characterInfo = new CharacterInfo[mbFont.glyphs.Count];          for (int i = 0; i < mbFont.glyphs.Count; i++)          {              BMGlyph bmInfo = mbFont.glyphs[i];              CharacterInfo info = new CharacterInfo();              info.index = bmInfo.index;              info.uv.x = (float)bmInfo.x / (float)mbFont.texWidth;              info.uv.y = 1-(float)bmInfo.y / (float)mbFont.texHeight;              info.uv.width = (float)bmInfo.width / (float)mbFont.texWidth;              info.uv.height = -1f * (float)bmInfo.height / (float)mbFont.texHeight;              info.vert.x = (float)bmInfo.offsetX;              //info.vert.y = (float)bmInfo.offsetY;              info.vert.y = 0f;//自定义字库UV从下往上,所以这里不需要偏移,填0即可。             info.vert.width = (float)bmInfo.width;              info.vert.height = (float)bmInfo.height;              info.width = (float)bmInfo.advance;              characterInfo[i] = info;          }          sFont.characterInfo = characterInfo;           sFont.material = sPng;    }   }

3.最终自己把转化过程实现一下

由于时间关系,没有做很大的自由度,不支持换行等特殊字符功能

//自定义字体文字转贴图    public static Texture2D TextToTexture(this Font target,string text,Color color)    {        //字体贴图//只支持自定义字体CustomFont        Texture2D t = (Texture2D)target.material.mainTexture;        char letter;        int width = 0;        int height = 0;        //计算图片长宽//暂不支持换行        for (int n = 0; n < text.Length; n++) {            letter = text [n];            CharacterInfo c = new CharacterInfo ();            if (target.GetCharacterInfo (letter,out c)) {                width += c.advance;                if (c.vert.height > height) {                    height = (int)c.vert.height;                }            }        }        //创建字体        Texture2D fontTexture = new Texture2D (width,height);        int widthCurrent = 0;        for (int n = 0; n < text.Length; n++) {            letter = text [n];            CharacterInfo c = new CharacterInfo ();            if (target.GetCharacterInfo (letter, out c)) {                for (int x = 0; x < c.advance; x++) {                    for (int y = 0; y < height; y++) {                        if (y > c.minY || y < c.maxY||x < c.minX || x > c.maxX) {                            fontTexture.SetPixel (widthCurrent+x,y,new Color(0,0,0,0));                            continue;                        }                        fontTexture.SetPixel (widthCurrent+x,y,new Color(color.a,color.g,color.b,t.GetPixelBilinear((c.uvTopLeft.x + (c.uvTopRight.x - c.uvTopLeft.x) * (x - c.minX) / (c.maxX - c.minX)),(c.uvTopLeft.y + (c.uvBottomLeft.y - c.uvTopLeft.y) * (y - c.maxY) / (c.minY - c.maxY))).a));                    }                }                widthCurrent += c.advance;            }        }        fontTexture.Apply ();        return fontTexture;    }
0 0
原创粉丝点击