|
|
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
-
- public class MyFont : Editor
- {
- [MenuItem("Tools/MakeFont/导出BMFont " + "%#e")]
- static void CreateBmFont()
- {
- Object[] objs = Selection.objects;
- if (objs.Length != 1)
- {
- Debug.LogError("请选中BMFont导出的fnt数据文件");
- return;
- }
- if (objs[0].GetType() != typeof(TextAsset))
- {
- Debug.LogError("请选中BMFont导出的fnt数据文件");
- return;
- }
- var item = objs[0];
- string AssetFile = AssetDatabase.GetAssetPath(item);
-
- string PathNameNotExt = AssetFile.Remove(AssetFile.LastIndexOf('.'));
- string FileNameNotExt = PathNameNotExt.Remove(0, PathNameNotExt.LastIndexOf('/') + 1);
-
- string TextPathName = PathNameNotExt + ".txt";
- string MatPathName = PathNameNotExt + ".mat";
- string FontPathName = PathNameNotExt + ".fontsettings";
- string PngName = PathNameNotExt + "_0.png";
- //Texture tex = Resources.Load(PngName) as Texture;
- Texture tex = (Texture)AssetDatabase.LoadAssetAtPath(PngName, typeof(Texture));
- if (!tex)
- {
- Debug.LogError("不存在 "+ PngName+" 文件");
- return;
- }
-
- Material mat = (Material)AssetDatabase.LoadAssetAtPath(MatPathName, typeof(Material));
- if (mat == null)
- {
- //创建材质球
- mat = new Material(Shader.Find("Legacy Shaders/Transparent/Diffuse"));
- mat.SetTexture("_MainTex", tex);
- AssetDatabase.CreateAsset(mat, MatPathName);
- }
- else
- {
- mat.shader = Shader.Find("Legacy Shaders/Transparent/Diffuse");
- mat.SetTexture("_MainTex", tex);
- }
-
- Font font = (Font)AssetDatabase.LoadAssetAtPath(FontPathName, typeof(Font));
-
- if (font == null)
- {
- font = new Font(FileNameNotExt);
-
- AssetDatabase.CreateAsset(font, FontPathName);
- }
-
- TextAsset m_data = (TextAsset)objs[0];
- BMFont mbFont = new BMFont();
- BMFontReader.Load(mbFont, m_data.name, m_data.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 = -(float)bmInfo.height / (float)mbFont.texHeight;
- info.vert.x = (float)bmInfo.offsetX;
- info.vert.y = (float)bmInfo.offsetY;
- info.vert.width = (float)bmInfo.width;
- info.vert.height = (float)bmInfo.height;
- info.width = (float)bmInfo.advance;
- characterInfo[i] = info;
- }
- font.characterInfo = characterInfo;
- font.material = mat;
-
- // 通知编辑器有资源被修改了
- EditorUtility.SetDirty(font);
- // 保存所有修改
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
-
- Debug.Log("OK");
-
- //ShowNotification(new GUIContent("OK"));
- }
-
- [MenuItem("Tools/MakeFont/Make(废弃了)")]
- static void Make()
- {
- Object[] objs = Selection.objects;
- if (objs.Length != 2)
- {
- Debug.LogError("请选中Custom Font文件与BMFont导出的fnt数据文件");
- return;
- }
- Font m_myFont;
- TextAsset m_data;
- if (objs[0].GetType() == typeof(TextAsset) && objs[1].GetType() == typeof(Font))
- {
- Debug.Log("text");
- m_data = (TextAsset)objs[0];
- m_myFont = (Font)objs[1];
- Debug.Log("FontName:" + m_myFont.name + "\nData:" + m_data.name);
- }
- else if (objs[1].GetType() == typeof(TextAsset) && objs[0].GetType() == typeof(Font))
- {
- m_data = (TextAsset)objs[1];
- m_myFont = (Font)objs[0];
- Debug.Log("FontName:" + m_myFont.name + "\nData:" + m_data.name);
- }
- else
- {
- Debug.LogError("请选中Custom Font文件与BMFont导出的fnt数据文件");
- Debug.Log("FontName:null" + "\nData:null");
- return;
- }
-
- BMFont mbFont = new BMFont();
- BMFontReader.Load(mbFont, m_data.name, m_data.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 = -(float)bmInfo.height / (float)mbFont.texHeight;
- info.vert.x = (float)bmInfo.offsetX;
- info.vert.y = (float)bmInfo.offsetY;
- info.vert.width = (float)bmInfo.width;
- info.vert.height = (float)bmInfo.height;
- info.width = (float)bmInfo.advance;
- characterInfo[i] = info;
- }
- m_myFont.characterInfo = characterInfo;
-
- Debug.Log("OK");
- }
- }
|