static void CreateFont(UIFont font, int create, Material mat) { if (create == 1) { // New dynamic font //font.atlas = null; //font.dynamicFont = NGUISettings.trueTypeFont; //font.dynamicFontStyle = NGUISettings.fontStyle; Debug.LogError("Creating UIFont for dynamic fonts is no longer needed. Reference the font directly on your label."); } else { // New bitmap font font.dynamicFont = null; BMFontReader.Load(font.bmFont, NGUITools.GetHierarchy(font.gameObject), NGUISettings.fontData.bytes); if (create == 2) { font.atlas = null; font.material = mat; } else if (create == 3) { font.spriteName = NGUISettings.fontTexture.name; font.atlas = NGUISettings.atlas; } } }
static void CreateFont(UIFont font, int create, Material mat) { if (create == 1) { // New dynamic font font.atlas = null; font.dynamicFont = NGUISettings.dynamicFont; font.dynamicFontSize = NGUISettings.dynamicFontSize; font.dynamicFontStyle = NGUISettings.dynamicFontStyle; } else { // New bitmap font font.dynamicFont = null; BMFontReader.Load(font.bmFont, NGUITools.GetHierarchy(font.gameObject), NGUISettings.fontData.bytes); if (create == 2) { font.atlas = null; font.material = mat; } else if (create == 3) { font.spriteName = NGUISettings.fontTexture.name; font.atlas = NGUISettings.atlas; } } }
public static void BatchCreateArtistFont() { string dirName = ""; string fntname = SelectObjectPathInfo(ref dirName).Split('.')[0]; Debug.Log(fntname); Debug.Log(dirName); string fntFileName = dirName + fntname + ".fnt"; Font CustomFont = new Font(); { AssetDatabase.CreateAsset(CustomFont, dirName + fntname + ".fontsettings"); AssetDatabase.SaveAssets(); } TextAsset BMFontText = null; { BMFontText = AssetDatabase.LoadAssetAtPath(fntFileName, typeof(TextAsset)) as TextAsset; } BMFont mbFont = new BMFont(); BMFontReader.Load(mbFont, BMFontText.name, BMFontText.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.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } CustomFont.characterInfo = characterInfo; string textureFilename = dirName + mbFont.spriteName + ".png"; Material mat = null; { Shader shader = Shader.Find("Transparent/Diffuse"); mat = new Material(shader); Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); AssetDatabase.CreateAsset(mat, dirName + fntname + ".mat"); AssetDatabase.SaveAssets(); } CustomFont.material = mat; EditorUtility.SetDirty(CustomFont); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
void CreateFont() { BMFontReader.Load(mbFont, textFnt.name, textFnt.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; } bmFont.characterInfo = characterInfo; //AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(bmFont)); EditorUtility.SetDirty(bmFont); AssetDatabase.SaveAssets(); Debug.Log(AssetDatabase.GetAssetPath(bmFont)); //AssetDatabase.WriteImportSettingsIfDirty(AssetDatabase.GetAssetPath(bmFont)); AssetDatabase.Refresh(); }
public static void CreateBmFont() { NewFont(); BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; info.uv.x = (float)bmInfo.x / (float)bmFont.texWidth; info.uv.y = 1 - (float)bmInfo.y / (float)bmFont.texHeight; info.uv.width = (float)bmInfo.width / (float)bmFont.texWidth; info.uv.height = -1f * (float)bmInfo.height / (float)bmFont.texHeight; info.vert.x = 0; info.vert.y = -(float)bmInfo.height; info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; EditorUtility.SetDirty(targetFont); Debug.Log("create font <" + targetFont.name + "> success"); }
public static void CreateBitmap(PsdLayerToNGUI.Data data) { // Create a new game object for the font var name = data.fontTexture.name; var go = new GameObject(name); var bitmapFont = go.AddComponent <UIFont>(); NGUISettings.ambigiousFont = bitmapFont; NGUISettings.fontData = data.fontData; NGUISettings.fontTexture = data.fontTexture; { BMFontReader.Load(bitmapFont.bmFont, NGUITools.GetHierarchy(bitmapFont.gameObject), NGUISettings.fontData.bytes); bitmapFont.spriteName = NGUISettings.fontTexture.name; bitmapFont.atlas = NGUISettings.atlas; } // Update the prefab #if UNITY_3_4 EditorUtility.ReplacePrefab(go, NGUIFontCreator.fontPrefab); #else PrefabUtility.ReplacePrefab(go, NGUIFontCreator.fontPrefab); #endif GameObject.DestroyImmediate(go); AssetDatabase.Refresh(); // Select the atlas go = AssetDatabase.LoadAssetAtPath(fontPrefabPath, typeof(GameObject)) as GameObject; data.fontPrefab = go.GetComponent <UIFont>(); data.fontPrefab.spriteName = name; NGUISettings.ambigiousFont = data.fontPrefab; }
public static void BatchCreateArtistFont() { string dirName = ""; string fntname = EditorUtils.SelectObjectPathInfo(ref dirName).Split('.')[0]; Debug.Log(fntname); Debug.Log(dirName); string fntFileName = dirName + fntname + ".fnt"; Font CustomFont = new Font(); { AssetDatabase.CreateAsset(CustomFont, dirName + fntname + ".fontsettings"); AssetDatabase.SaveAssets(); } TextAsset BMFontText = null; { BMFontText = AssetDatabase.LoadAssetAtPath(fntFileName, typeof(TextAsset)) as TextAsset; } BMFont mbFont = new BMFont(); BMFontReader.Load(mbFont, BMFontText.name, BMFontText.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.uvTopLeft = new Vector2((float)bmInfo.x / (float)mbFont.texWidth, (float)(bmInfo.y + bmInfo.height) / (float)mbFont.texHeight); info.uvTopRight = new Vector2((float)(bmInfo.x + bmInfo.width) / (float)mbFont.texWidth, (float)(bmInfo.y + bmInfo.height) / (float)mbFont.texHeight); info.uvBottomLeft = new Vector2((float)bmInfo.x / (float)mbFont.texWidth, (float)bmInfo.y / (float)mbFont.texHeight); info.uvBottomRight = new Vector2((float)(bmInfo.x + bmInfo.width) / (float)mbFont.texWidth, (float)(bmInfo.y) / (float)mbFont.texHeight); info.minX = bmInfo.offsetX; info.minY = bmInfo.offsetY; info.maxX = bmInfo.width; info.maxY = bmInfo.height; info.advance = bmInfo.advance; characterInfo[i] = info; } CustomFont.characterInfo = characterInfo; string textureFilename = dirName + mbFont.spriteName + ".png"; Material mat = null; { Shader shader = Shader.Find("UI/Unlit/Text"); mat = new Material(shader); Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); AssetDatabase.CreateAsset(mat, dirName + fntname + ".mat"); AssetDatabase.SaveAssets(); } CustomFont.material = mat; }
public static BMFont getBMFontInfo(TextAsset fntSettings) { BMFont bmFont = new BMFont(); // 读取fnt字体信息 BMFontReader.Load(bmFont, fntSettings.name, fntSettings.bytes); return(bmFont); }
public static void GenerateBMFont() { Font selectedFont = null; TextAsset selectedData = null; BMFont bmFont = new BMFont(); Object[] selectedObjects = Selection.objects; Object selectedObject = null; for (int selectedObjectIndex = 0; selectedObjectIndex < selectedObjects.Length; selectedObjectIndex++) { selectedObject = selectedObjects[selectedObjectIndex]; if (selectedObject is Font) { selectedFont = selectedObject as Font; } else if (selectedObject is TextAsset) { selectedData = selectedObject as TextAsset; } } if (selectedFont == null || selectedData == null) { string fontNullInfo = selectedFont == null ? "Font is null." : string.Empty; string dataNullInfo = selectedData == null ? "Data is null." : string.Empty; Debugger.Log(fontNullInfo + dataNullInfo); return; } else { Debugger.Log(string.Format("Selected Font:{0}, Selected Data:{1}", selectedFont.name, selectedData.name)); BMFontReader.Load(bmFont, selectedData.name, selectedData.bytes); CharacterInfo[] characterInfos = new CharacterInfo[bmFont.glyphs.Count]; int characterInfoLength = bmFont.glyphs.Count; for (int index = 0; index < characterInfoLength; index++) { BMGlyph bmGlyph = bmFont.glyphs[index]; CharacterInfo characterInfo = new CharacterInfo(); characterInfo.index = bmGlyph.index; characterInfo.uv.x = (float)bmGlyph.x / (float)bmFont.texWidth; characterInfo.uv.y = 1 - (float)bmGlyph.y / (float)bmFont.texHeight; characterInfo.uv.width = (float)bmGlyph.width / (float)bmFont.texWidth; characterInfo.uv.height = -1f * (float)bmGlyph.height / (float)bmFont.texHeight; characterInfo.vert.x = bmGlyph.offsetX; characterInfo.vert.y = bmGlyph.offsetY; characterInfo.vert.width = bmGlyph.width; characterInfo.vert.height = bmGlyph.height; characterInfo.width = bmGlyph.advance; characterInfos[index] = characterInfo; } selectedFont.characterInfo = characterInfos; } }
public static void BatchCreateArtistFont() { string fntFilePath = AssetDatabase.GetAssetPath(Selection.activeObject); if (string.IsNullOrEmpty(fntFilePath) || !fntFilePath.Contains(".fnt")) { Debug.LogError("请选择字体文件,后缀为.fnt的文件"); return; } string fntName = Path.GetFileNameWithoutExtension(fntFilePath); string dirName = Path.GetDirectoryName(fntFilePath); Font CustomFont = new Font(); AssetDatabase.CreateAsset(CustomFont, Path.Combine(dirName, fntName + ".fontsettings")); CustomFont = AssetDatabase.LoadAssetAtPath <Font>(Path.Combine(dirName, fntName + ".fontsettings")); TextAsset BMFontText = AssetDatabase.LoadAssetAtPath(fntFilePath, typeof(TextAsset)) as TextAsset; BMFont mbFont = new BMFont(); BMFontReader.Load(mbFont, BMFontText.name, BMFontText.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 - (float)bmInfo.height / 2; info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } CustomFont.characterInfo = characterInfo; string textureFilename = Path.Combine(dirName, mbFont.spriteName + ".png"); Shader shader = Shader.Find("GUI/Custom Text Shader"); Material mat = new Material(shader); Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); AssetDatabase.CreateAsset(mat, Path.Combine(dirName, fntName + ".mat")); CustomFont.material = mat; EditorUtility.SetDirty(CustomFont); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
void OnWizardCreate() { string fontName = fntTex.name; string path = AssetDatabase.GetAssetPath(fntTex); path = Path.GetDirectoryName(path); var matPath = Path.Combine(path, fontName + ".mat"); var fntMat = AssetDatabase.LoadAssetAtPath <Material>(matPath); if (fntMat == null) { fntMat = new Material(Shader.Find("UI/Default")); fntMat.mainTexture = fntTex; AssetDatabase.CreateAsset(fntMat, matPath); } var fontPath = Path.Combine(path, fontName + ".fontsettings"); var font = AssetDatabase.LoadAssetAtPath <Font>(fontPath); if (font == null) { font = new Font(fontName); AssetDatabase.CreateAsset(font, fontPath); } font.material = fntMat; var bmFont = new BMFont(); BMFontReader.Load(bmFont, fntTxt.name, fntTxt.bytes); var charInfos = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; ++i) { BMGlyph glyph = bmFont.glyphs[i]; //var rect = new Rect(glyph.x, glyph.y, glyph.width, glyph.height); var uvRect = new Rect(glyph.x / (float)bmFont.texWidth, 1 - glyph.y / (float)bmFont.texHeight, glyph.width / (float)bmFont.texWidth, -1 * glyph.height / (float)bmFont.texHeight); var info = new CharacterInfo(); info.index = glyph.index; info.glyphWidth = glyph.width; info.glyphHeight = glyph.height; info.advance = glyph.advance; info.uvTopLeft = uvRect.min;// new Vector2(uvRect.xMin, uvRect.yMin); //info.uvBottomRight = new Vector2(uvRect.xMax, uvRect.yMin); //info.uvTopLeft = new Vector2(uvRect.xMin, uvRect.yMax); info.uvBottomRight = uvRect.max;// new Vector2(uvRect.xMax, uvRect.yMax); charInfos[i] = info; } font.characterInfo = charInfos; //EditorUtility.DisplayDialog("提示", string.Format("功能未实现\nNot Implemented\n実装されていない\nKeine umsetzung"), "确定"); EditorUtility.DisplayDialog("提示", string.Format("字体创建完成:{0}。\n注意:需要手动修改一下字体配置,否则退出后不会保存!", fontPath), "确定"); }
void OnGUI() { //改成拖动文件夹 targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font; fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset; fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; int width = bmInfo.width; int height = bmInfo.height; int texWidth = bmFont.texWidth; int texHeight = bmFont.texHeight; float uvX = 1f * bmInfo.x / texWidth; float uvY = 1 - (1f * bmInfo.y / texHeight); //UV的坐标轴是以左上为0点 float uvWidth = 1f * width / texWidth; float uvHeight = -1f * height / texHeight; info.uvBottomLeft = new Vector2(uvX, uvY); info.uvBottomRight = new Vector2(uvX + uvWidth, uvY); info.uvTopLeft = new Vector2(uvX, uvY + uvHeight); info.uvTopRight = new Vector2(uvX + uvWidth, uvY + uvHeight); info.minX = bmInfo.offsetX; info.minY = bmInfo.offsetY + height / 2; info.maxX = bmInfo.offsetX + width; //todo test info.maxY = bmInfo.offsetY + height; info.glyphWidth = width; info.glyphHeight = -height; // 不知道为什么要用负的,可能跟unity纹理uv有关 info.advance = bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; fontMaterial.shader = Shader.Find("UI/Default"); Debug.Log("create font <" + targetFont.name + "> success"); Close(); } }
/// <summary> /// 第二步:给对象添加组件、给材质球关联着色器及纹理同时关联tp产生的坐标信息文件 /// </summary> /// <param name="go"></param> /// <param name="path"></param> /// <param name="mat"></param> /// <returns></returns> private static UIFont SetAtlasInfo(GameObject go, string path, Material mat) { if (AssetDatabase.LoadAssetAtPath(path.Replace(".png", ".fnt"), typeof(TextAsset))) { UIFont uiFont = go.GetComponent <UIFont>(); uiFont.material = mat; TextAsset data = AssetDatabase.LoadAssetAtPath(path.Replace(".png", ".fnt"), typeof(TextAsset)) as TextAsset; BMFontReader.Load(uiFont.bmFont, NGUITools.GetHierarchy(uiFont.gameObject), data.bytes); uiFont.MarkAsChanged(); return(uiFont); } return(null); }
public static void DoImportBitmapFont(string fntPath) { if (!IsFnt(fntPath)) { return; } try { EditorUtility.DisplayProgressBar("Build Bitmap Font", string.Format("正在进行 {0}", fntPath), 1); string pngPath = Path.ChangeExtension(fntPath, "png"); string prefabPath = Path.ChangeExtension(fntPath, "prefab"); string matPath = Path.ChangeExtension(fntPath, "mat"); GameObject prefabAsset = GEditorNGUILoader.LoadAtlasPrefab(prefabPath); Material matAsset = GEditorNGUILoader.LoadTransparentMaterial(matPath); UIFont mUIFont = prefabAsset.GetComponent <UIFont>(); if (mUIFont == null) { mUIFont = prefabAsset.AddComponent <UIFont>(); } if (matAsset == null || prefabAsset == null) { Debug.LogError(string.Format("Build Bitmap Font -> AssetPath: {0} Material或者预制体为空", fntPath)); return; } // 配置图集参数 Texture2D pngAsset = GEditorNGUILoader.LoadTexture2D(pngPath); TextAsset data = AssetDatabase.LoadAssetAtPath <TextAsset>(fntPath) as TextAsset; if (pngAsset == null || data == null) { Debug.LogError(string.Format("Build Bitmap Font -> AssetPath: {0} 配置文件缺失", fntPath)); return; } BMFontReader.Load(mUIFont.bmFont, NGUITools.GetHierarchy(mUIFont.gameObject), data.bytes); matAsset.SetTexture("_MainTex", pngAsset); mUIFont.material = matAsset; mUIFont.MarkAsChanged(); EditorUtility.SetDirty(mUIFont); AssetDatabase.SaveAssets(); } catch (System.Exception ex) { EditorUtility.DisplayDialog("异常", ex.ToString(), "确定"); } finally { EditorUtility.ClearProgressBar(); } }
void OnGUI() { targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font; fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material; fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { // AssetDatabase.OpenAsset(targetFont); // AssetDatabase.StartAssetEditing (); BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; info.uv.x = (float)bmInfo.x / (float)bmFont.texWidth; info.uv.y = 1 - (float)bmInfo.y / (float)bmFont.texHeight; info.uv.width = (float)bmInfo.width / (float)bmFont.texWidth; info.uv.height = -1f * (float)bmInfo.height / (float)bmFont.texHeight; info.vert.x = 0; info.vert.y = -(float)bmInfo.height; info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; fontMaterial.shader = Shader.Find("UI/Default Font"); Debug.Log("create font <" + targetFont.name + "> success"); // AssetDatabase.StopAssetEditing(); EditorUtility.SetDirty(targetFont); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); Close(); } }
void OnGUI() { targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font; fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset; fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; info.uv.x = (float)bmInfo.x / (float)bmFont.texWidth; info.uv.y = 1 - (float)bmInfo.y / (float)bmFont.texHeight; info.uv.width = (float)bmInfo.width / (float)bmFont.texWidth; info.uv.height = -1f * (float)bmInfo.height / (float)bmFont.texHeight; info.vert.x = 0; info.vert.y = -(float)bmInfo.height; info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; fontMaterial.shader = Shader.Find("UI/Default");//这一行很关键,如果用standard的shader,放到Android手机上,第一次加载会很慢 Debug.Log("create font <" + targetFont.name + "> success"); Font newFont = Instantiate(targetFont); string path = AssetDatabase.GetAssetPath(targetFont); AssetDatabase.DeleteAsset(path); AssetDatabase.Refresh(); AssetDatabase.CreateAsset(newFont, path); AssetDatabase.SaveAssets(); Close(); } }
void OnGUI() { targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font; fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset; fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.glyphHeight = bmFont.texHeight; info.glyphWidth = bmFont.texWidth; info.index = bmInfo.index; info.uvTopLeft = new Vector2((float)bmInfo.x / bmFont.texWidth, 1 - (float)bmInfo.y / bmFont.texHeight); info.uvTopRight = new Vector2((float)(bmInfo.x + bmInfo.width) / bmFont.texWidth, 1 - (float)bmInfo.y / bmFont.texHeight); info.uvBottomLeft = new Vector2((float)bmInfo.x / bmFont.texWidth, 1 - (float)(bmInfo.y + bmInfo.height) / bmFont.texHeight); info.uvBottomRight = new Vector2((float)(bmInfo.x + bmInfo.width) / bmFont.texWidth, 1 - (float)(bmInfo.y + bmInfo.height) / bmFont.texHeight); info.minX = 0; info.minY = -bmInfo.height; info.maxX = bmInfo.width; info.maxY = 0; info.advance = bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; fontMaterial.shader = Shader.Find("UI/Default Font"); AssetDatabase.Refresh(); Debug.Log("create font <" + targetFont.name + "> success"); Close(); } }
void Start() { 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.width = (float)bmInfo.advance; Rect r = new Rect(); r.x = (float)bmInfo.x / (float)mbFont.texWidth; r.y = (float)bmInfo.y / (float)mbFont.texHeight; r.width = (float)bmInfo.width / (float)mbFont.texWidth; r.height = (float)bmInfo.height / (float)mbFont.texHeight; r.y = 1f - r.y - r.height; info.uv = r; r = new Rect(); r.x = (float)bmInfo.offsetX; r.y = (float)bmInfo.offsetY; r.width = (float)bmInfo.width; r.height = (float)(float)bmInfo.height; r.y = -r.y; r.height = -r.height; info.vert = r; characterInfo[i] = info; /* * Debug.Log ("bmInfo.x:" + bmInfo.x + " mbFont.texWidth:" + mbFont.texWidth); * Debug.Log ("bmInfo.y:" + bmInfo.y + " mbFont.texHeight:" + mbFont.texHeight); * Debug.Log ("bmInfo.width:" + bmInfo.width + " mbFont.texWidth:" + mbFont.texWidth); * Debug.Log ("bmInfo.height:" + bmInfo.height + " mbFont.texHeight:" + mbFont.texHeight); * Debug.Log ("bmInfo.offsetX:" + bmInfo.offsetX); * Debug.Log ("bmInfo.advance:" + bmInfo.advance);*/ } m_myFont.characterInfo = characterInfo; m_myFont.material = m_mat; }
/// <summary> /// Create the specified font. /// </summary> static void ImportFont(UIFont font, Create create, Material mat) { // New bitmap font font.dynamicFont = null; BMFontReader.Load(font.bmFont, NGUITools.GetHierarchy(font.gameObject), NGUISettings.fontData.bytes); if (NGUISettings.atlas == null) { font.atlas = null; font.material = mat; } else { font.spriteName = NGUISettings.fontTexture.name; font.atlas = NGUISettings.atlas; } NGUISettings.fontSize = font.defaultSize; }
void OnGUI() { //改成拖动文件夹 targetFont = EditorGUILayout.ObjectField("Target Font", targetFont, typeof(Font), false) as Font; fntData = EditorGUILayout.ObjectField("Fnt Data", fntData, typeof(TextAsset), false) as TextAsset; fontMaterial = EditorGUILayout.ObjectField("Font Material", fontMaterial, typeof(Material), false) as Material; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { BMFontReader.Load(bmFont, fntData.name, fntData.bytes); // 借用NGUI封装的读取类 CharacterInfo[] characterInfo = new CharacterInfo[bmFont.glyphs.Count]; for (int i = 0; i < bmFont.glyphs.Count; i++) { BMGlyph bmInfo = bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; info.uv.x = (float)bmInfo.x / (float)bmFont.texWidth; info.uv.y = 1 - (float)bmInfo.y / (float)bmFont.texHeight; info.uv.width = (float)bmInfo.width / (float)bmFont.texWidth; info.uv.height = -1f * (float)bmInfo.height / (float)bmFont.texHeight; info.vert.x = 0; info.vert.y = (-(float)bmInfo.height) / 2; //居中 info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; if (fontMaterial) { fontMaterial.mainTexture = fontTexture; } targetFont.material = fontMaterial; fontMaterial.shader = Shader.Find("UI/Default"); Debug.Log("create font <" + targetFont.name + "> success"); Close(); } }
void Start() { 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 = -1f * (float)bmInfo.height / (float)mbFont.texHeight; info.vert.x = (float)bmInfo.offsetX * scale; //info.vert.y = (float)bmInfo.offsetY+(float)bmInfo.height-mbFont.charSize; info.vert.y = -((float)bmInfo.offsetY + (float)bmInfo.height) * scale; info.vert.width = (float)bmInfo.width * scale; info.vert.height = (float)bmInfo.height * scale; info.width = (float)bmInfo.advance * scale; characterInfo[i] = info; } m_myFont.characterInfo = characterInfo; }
void Start() { 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.y = 0f;//自定义字库UV从下往上,所以这里不需要偏移,填0即可。 info.vert.width = (float)bmInfo.width; info.vert.height = (float)bmInfo.height; info.width = (float)bmInfo.advance; characterInfo[i] = info; } m_myFont.characterInfo = characterInfo; }
/// <summary> /// Draw the UI for this tool. /// </summary> void OnGUI() { Object fnt = NGUISettings.ambigiousFont; UIFont uiFont = (fnt as UIFont); NGUIEditorTools.SetLabelWidth(80f); GUILayout.Space(3f); NGUIEditorTools.DrawHeader("Input", true); NGUIEditorTools.BeginContents(false); GUILayout.BeginHorizontal(); mType = (FontType)EditorGUILayout.EnumPopup("Type", mType, GUILayout.MinWidth(200f)); NGUIEditorTools.DrawPadding(); GUILayout.EndHorizontal(); Create create = Create.None; if (mType == FontType.ImportedBitmap) { NGUISettings.fontData = EditorGUILayout.ObjectField("Font Data", NGUISettings.fontData, typeof(TextAsset), false) as TextAsset; NGUISettings.fontTexture = EditorGUILayout.ObjectField("Texture", NGUISettings.fontTexture, typeof(Texture2D), false, GUILayout.Width(140f)) as Texture2D; NGUIEditorTools.EndContents(); // Draw the atlas selection only if we have the font data and texture specified, just to make it easier EditorGUI.BeginDisabledGroup(NGUISettings.fontData == null || NGUISettings.fontTexture == null); { NGUIEditorTools.DrawHeader("Output", true); NGUIEditorTools.BeginContents(false); ComponentSelector.Draw <UIAtlas>(NGUISettings.atlas, OnSelectAtlas, false); NGUIEditorTools.EndContents(); } EditorGUI.EndDisabledGroup(); if (NGUISettings.fontData == null) { EditorGUILayout.HelpBox("To create a font from a previously exported FNT file, you need to use BMFont on " + "Windows or your choice of Glyph Designer or the less expensive bmGlyph on the Mac.\n\n" + "Either of these tools will create a FNT file for you that you will drag & drop into the field above.", MessageType.Info); } else if (NGUISettings.fontTexture == null) { EditorGUILayout.HelpBox("When exporting your font, you should get two files: the FNT, and the texture. Only one texture can be used per font.", MessageType.Info); } else if (NGUISettings.atlas == null) { EditorGUILayout.HelpBox("You can create a font that doesn't use a texture atlas. This will mean that the text " + "labels using this font will generate an extra draw call.\n\nIf you do specify an atlas, the font's texture will be added to it automatically.", MessageType.Info); } EditorGUI.BeginDisabledGroup(NGUISettings.fontData == null || NGUISettings.fontTexture == null); { GUILayout.BeginHorizontal(); GUILayout.Space(20f); if (GUILayout.Button("Create the Font")) { create = Create.Import; } GUILayout.Space(20f); GUILayout.EndHorizontal(); } EditorGUI.EndDisabledGroup(); } else { GUILayout.BeginHorizontal(); if (NGUIEditorTools.DrawPrefixButton("Source")) { ComponentSelector.Show <Font>(OnUnityFont, new string[] { ".ttf", ".otf" }); } Font ttf = EditorGUILayout.ObjectField(NGUISettings.ambigiousFont as Font, typeof(Font), false) as Font; GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); { NGUISettings.fontSize = EditorGUILayout.IntField("Size", NGUISettings.fontSize, GUILayout.Width(120f)); if (mType == FontType.Dynamic) { NGUISettings.fontStyle = (FontStyle)EditorGUILayout.EnumPopup(NGUISettings.fontStyle); NGUIEditorTools.DrawPadding(); } } GUILayout.EndHorizontal(); // Choose the font style if there are multiple faces present if (mType == FontType.GeneratedBitmap) { if (!FreeType.isPresent) { string filename = (Application.platform == RuntimePlatform.WindowsEditor) ? "FreeType.dll" : "FreeType.dylib"; EditorGUILayout.HelpBox("Assets/NGUI/Editor/" + filename + " is missing", MessageType.Error); GUILayout.BeginHorizontal(); GUILayout.Space(20f); if (GUILayout.Button("Find " + filename)) { string path = EditorUtility.OpenFilePanel("Find " + filename, NGUISettings.currentPath, (Application.platform == RuntimePlatform.WindowsEditor) ? "dll" : "dylib"); if (!string.IsNullOrEmpty(path)) { if (System.IO.Path.GetFileName(path) == filename) { NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path); NGUISettings.pathToFreeType = path; } else { Debug.LogError("The library must be named '" + filename + "'"); } } } GUILayout.Space(20f); GUILayout.EndHorizontal(); } else if (ttf != null) { string[] faces = FreeType.GetFaces(ttf); if (faces != null) { if (mFaceIndex >= faces.Length) { mFaceIndex = 0; } if (faces.Length > 1) { GUILayout.Label("Style", EditorStyles.boldLabel); for (int i = 0; i < faces.Length; ++i) { GUILayout.BeginHorizontal(); GUILayout.Space(10f); if (DrawOption(i == mFaceIndex, " " + faces[i])) { mFaceIndex = i; } GUILayout.EndHorizontal(); } } } GUILayout.Label("Characters", EditorStyles.boldLabel); CharacterMap cm = characterMap; GUILayout.BeginHorizontal(GUILayout.Width(100f)); GUILayout.BeginVertical(); GUI.changed = false; if (DrawOption(cm == CharacterMap.Numeric, " Numeric")) { cm = CharacterMap.Numeric; } if (DrawOption(cm == CharacterMap.Ascii, " ASCII")) { cm = CharacterMap.Ascii; } if (DrawOption(cm == CharacterMap.Latin, " Latin")) { cm = CharacterMap.Latin; } if (DrawOption(cm == CharacterMap.Custom, " Custom")) { cm = CharacterMap.Custom; } if (GUI.changed) { characterMap = cm; } GUILayout.EndVertical(); EditorGUI.BeginDisabledGroup(cm != CharacterMap.Custom); { if (cm != CharacterMap.Custom) { string chars = ""; if (cm == CharacterMap.Ascii) { for (int i = 33; i < 127; ++i) { chars += System.Convert.ToChar(i); } } else if (cm == CharacterMap.Numeric) { chars = "01234567890"; } else if (cm == CharacterMap.Latin) { for (int i = 33; i < 127; ++i) { chars += System.Convert.ToChar(i); } for (int i = 161; i < 256; ++i) { chars += System.Convert.ToChar(i); } } NGUISettings.charsToInclude = chars; } GUI.changed = false; string text = NGUISettings.charsToInclude; if (cm == CharacterMap.Custom) { text = EditorGUILayout.TextArea(text, GUI.skin.textArea, GUILayout.Height(80f), GUILayout.Width(Screen.width - 100f)); } else { GUILayout.Label(text, GUI.skin.textArea, GUILayout.Height(80f), GUILayout.Width(Screen.width - 100f)); } if (GUI.changed) { string final = ""; for (int i = 0; i < text.Length; ++i) { char c = text[i]; if (c < 33) { continue; } string s = c.ToString(); if (!final.Contains(s)) { final += s; } } if (final.Length > 0) { char[] chars = final.ToCharArray(); System.Array.Sort(chars); final = new string(chars); } else { final = ""; } NGUISettings.charsToInclude = final; } } EditorGUI.EndDisabledGroup(); GUILayout.EndHorizontal(); } } NGUIEditorTools.EndContents(); if (mType == FontType.Dynamic) { EditorGUI.BeginDisabledGroup(ttf == null); GUILayout.BeginHorizontal(); GUILayout.Space(20f); if (GUILayout.Button("Create the Font")) { create = Create.Dynamic; } GUILayout.Space(20f); GUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); #if UNITY_3_5 EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error); #else // Helpful info if (ttf == null) { EditorGUILayout.HelpBox("You don't have to create a UIFont to use dynamic fonts. You can just reference the Unity Font directly on the label.", MessageType.Info); } EditorGUILayout.HelpBox("Please note that dynamic fonts can't be made a part of an atlas, and using dynamic fonts will result in at least one extra draw call.", MessageType.Warning); #endif } else { bool isBuiltIn = (ttf != null) && string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(ttf)); // Draw the atlas selection only if we have the font data and texture specified, just to make it easier EditorGUI.BeginDisabledGroup(ttf == null || isBuiltIn || !FreeType.isPresent); { NGUIEditorTools.DrawHeader("Output", true); NGUIEditorTools.BeginContents(false); ComponentSelector.Draw <UIAtlas>(NGUISettings.atlas, OnSelectAtlas, false); NGUIEditorTools.EndContents(); if (ttf == null) { EditorGUILayout.HelpBox("You can create a bitmap font by specifying a dynamic font to use as the source.", MessageType.Info); } else if (isBuiltIn) { EditorGUILayout.HelpBox("You chose an embedded font. You can't create a bitmap font from an embedded resource.", MessageType.Warning); } else if (NGUISettings.atlas == null) { EditorGUILayout.HelpBox("You can create a font that doesn't use a texture atlas. This will mean that the text " + "labels using this font will generate an extra draw call.\n\nIf you do specify an atlas, the font's texture will be added to it automatically.", MessageType.Info); } GUILayout.BeginHorizontal(); GUILayout.Space(20f); if (GUILayout.Button("Create the Font")) { create = Create.Bitmap; } GUILayout.Space(20f); GUILayout.EndHorizontal(); } EditorGUI.EndDisabledGroup(); } } if (create == Create.None) { return; } // Open the "Save As" file dialog #if UNITY_3_5 string prefabPath = EditorUtility.SaveFilePanel("Save As", NGUISettings.currentPath, "New Font.prefab", "prefab"); #else string prefabPath = EditorUtility.SaveFilePanelInProject("Save As", "New Font.prefab", "prefab", "Save font as...", NGUISettings.currentPath); #endif if (string.IsNullOrEmpty(prefabPath)) { return; } NGUISettings.currentPath = System.IO.Path.GetDirectoryName(prefabPath); // Load the font's prefab GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; Object prefab = null; string fontName; // Font doesn't exist yet if (go == null || go.GetComponent <UIFont>() == null) { // Create a new prefab for the atlas prefab = PrefabUtility.CreateEmptyPrefab(prefabPath); fontName = prefabPath.Replace(".prefab", ""); fontName = fontName.Substring(prefabPath.LastIndexOfAny(new char[] { '/', '\\' }) + 1); // Create a new game object for the font go = new GameObject(fontName); uiFont = go.AddComponent <UIFont>(); } else { uiFont = go.GetComponent <UIFont>(); fontName = go.name; } if (create == Create.Dynamic) { uiFont.atlas = null; uiFont.dynamicFont = NGUISettings.dynamicFont; uiFont.dynamicFontStyle = NGUISettings.fontStyle; uiFont.defaultSize = NGUISettings.fontSize; } else if (create == Create.Import) { Material mat = null; if (NGUISettings.atlas != null) { // Add the font's texture to the atlas UIAtlasMaker.AddOrUpdate(NGUISettings.atlas, NGUISettings.fontTexture); } else { // Create a material for the font string matPath = prefabPath.Replace(".prefab", ".mat"); mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; // If the material doesn't exist, create it if (mat == null) { Shader shader = Shader.Find("Unlit/Transparent Colored"); mat = new Material(shader); // Save the material AssetDatabase.CreateAsset(mat, matPath); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); // Load the material so it's usable mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; } mat.mainTexture = NGUISettings.fontTexture; } uiFont.dynamicFont = null; BMFontReader.Load(uiFont.bmFont, NGUITools.GetHierarchy(uiFont.gameObject), NGUISettings.fontData.bytes); if (NGUISettings.atlas == null) { uiFont.atlas = null; uiFont.material = mat; } else { uiFont.spriteName = NGUISettings.fontTexture.name; uiFont.atlas = NGUISettings.atlas; } NGUISettings.fontSize = uiFont.defaultSize; } else if (create == Create.Bitmap) { // Create the bitmap font BMFont bmFont; Texture2D tex; if (FreeType.CreateFont( NGUISettings.dynamicFont, NGUISettings.fontSize, mFaceIndex, NGUISettings.charsToInclude, out bmFont, out tex)) { uiFont.bmFont = bmFont; tex.name = fontName; if (NGUISettings.atlas != null) { // Add this texture to the atlas and destroy it UIAtlasMaker.AddOrUpdate(NGUISettings.atlas, tex); NGUITools.DestroyImmediate(tex); NGUISettings.fontTexture = null; tex = null; uiFont.atlas = NGUISettings.atlas; uiFont.spriteName = fontName; } else { string texPath = prefabPath.Replace(".prefab", ".png"); string matPath = prefabPath.Replace(".prefab", ".mat"); byte[] png = tex.EncodeToPNG(); FileStream fs = File.OpenWrite(texPath); fs.Write(png, 0, png.Length); fs.Close(); // See if the material already exists Material mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; // If the material doesn't exist, create it if (mat == null) { Shader shader = Shader.Find("Unlit/Transparent Colored"); mat = new Material(shader); // Save the material AssetDatabase.CreateAsset(mat, matPath); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); // Load the material so it's usable mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; } else { AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); } // Re-load the texture tex = AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)) as Texture2D; // Assign the texture mat.mainTexture = tex; NGUISettings.fontTexture = tex; uiFont.atlas = null; uiFont.material = mat; } } else { return; } } if (prefab != null) { // Update the prefab PrefabUtility.ReplacePrefab(go, prefab); DestroyImmediate(go); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); // Select the atlas go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; uiFont = go.GetComponent <UIFont>(); } if (uiFont != null) { NGUISettings.ambigiousFont = uiFont; } MarkAsChanged(); Selection.activeGameObject = go; }
public override void OnInspectorGUI() { mFont = target as UIFont; NGUIEditorTools.SetLabelWidth(80f); GUILayout.Space(6f); if (mFont.replacement != null) { mType = FontType.Reference; mReplacement = mFont.replacement; } else if (mFont.dynamicFont != null) { mType = FontType.Dynamic; } GUI.changed = false; GUILayout.BeginHorizontal(); mType = (FontType)EditorGUILayout.EnumPopup("Font Type", mType); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (GUI.changed) { if (mType == FontType.Bitmap) { OnSelectFont(null); } if (mType != FontType.Dynamic && mFont.dynamicFont != null) { mFont.dynamicFont = null; } } if (mType == FontType.Reference) { ComponentSelector.Draw <UIFont>(mFont.replacement, OnSelectFont, true); GUILayout.Space(6f); EditorGUILayout.HelpBox("You can have one font simply point to " + "another one. This is useful if you want to be " + "able to quickly replace the contents of one " + "font with another one, for example for " + "swapping an SD font with an HD one, or " + "replacing an English font with a Chinese " + "one. All the labels referencing this font " + "will update their references to the new one.", MessageType.Info); if (mReplacement != mFont && mFont.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Font Change", mFont); mFont.replacement = mReplacement; NGUITools.SetDirty(mFont); } return; } else if (mType == FontType.Dynamic) { #if UNITY_3_5 EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error); #else Font fnt = EditorGUILayout.ObjectField("TTF Font", mFont.dynamicFont, typeof(Font), false) as Font; if (fnt != mFont.dynamicFont) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.dynamicFont = fnt; } Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } GUILayout.BeginHorizontal(); int size = EditorGUILayout.IntField("Default Size", mFont.defaultSize, GUILayout.Width(120f)); FontStyle style = (FontStyle)EditorGUILayout.EnumPopup(mFont.dynamicFontStyle); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (size != mFont.defaultSize) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.defaultSize = size; } if (style != mFont.dynamicFontStyle) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.dynamicFontStyle = style; } #endif } else { ComponentSelector.Draw <UIAtlas>(mFont.atlas, OnSelectAtlas, true); if (mFont.atlas != null) { if (mFont.bmFont.isValid) { NGUIEditorTools.DrawAdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false); } EditorGUILayout.Space(); } else { // No atlas specified -- set the material and texture rectangle directly Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } } // For updating the font's data when importing from an external source, such as the texture packer bool resetWidthHeight = false; if (mFont.atlas != null || mFont.material != null) { TextAsset data = EditorGUILayout.ObjectField("Import Data", null, typeof(TextAsset), false) as TextAsset; if (data != null) { NGUIEditorTools.RegisterUndo("Import Font Data", mFont); BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes); mFont.MarkAsChanged(); resetWidthHeight = true; ClientLog.Instance.Log("Imported " + mFont.bmFont.glyphCount + " characters"); } } if (mFont.bmFont.isValid) { Texture2D tex = mFont.texture; if (tex != null && mFont.atlas == null) { // Pixels are easier to work with than UVs Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false); // Automatically set the width and height of the rectangle to be the original font texture's dimensions if (resetWidthHeight) { pixels.width = mFont.texWidth; pixels.height = mFont.texHeight; } // Font sprite rectangle pixels = EditorGUILayout.RectField("Pixel Rect", pixels); // Convert the pixel coordinates back to UV coordinates Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height); if (mFont.uvRect != uvRect) { NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont); mFont.uvRect = uvRect; } //NGUIEditorTools.DrawSeparator(); EditorGUILayout.Space(); } } } // Dynamic fonts don't support emoticons if (!mFont.isDynamic && mFont.bmFont.isValid) { if (mFont.atlas != null) { if (NGUIEditorTools.DrawHeader("Symbols and Emoticons")) { NGUIEditorTools.BeginContents(); List <BMSymbol> symbols = mFont.symbols; for (int i = 0; i < symbols.Count;) { BMSymbol sym = symbols[i]; GUILayout.BeginHorizontal(); GUILayout.Label(sym.sequence, GUILayout.Width(40f)); if (NGUIEditorTools.DrawSpriteField(mFont.atlas, sym.spriteName, ChangeSymbolSprite, GUILayout.MinWidth(100f))) { mSelectedSymbol = sym; } if (GUILayout.Button("Edit", GUILayout.Width(40f))) { if (mFont.atlas != null) { NGUISettings.atlas = mFont.atlas; NGUISettings.selectedSprite = sym.spriteName; NGUIEditorTools.Select(mFont.atlas.gameObject); } } GUI.backgroundColor = Color.red; if (GUILayout.Button("X", GUILayout.Width(22f))) { NGUIEditorTools.RegisterUndo("Remove symbol", mFont); mSymbolSequence = sym.sequence; mSymbolSprite = sym.spriteName; symbols.Remove(sym); mFont.MarkAsChanged(); } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); GUILayout.Space(4f); ++i; } if (symbols.Count > 0) { GUILayout.Space(6f); } GUILayout.BeginHorizontal(); mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f)); NGUIEditorTools.DrawSpriteField(mFont.atlas, mSymbolSprite, SelectSymbolSprite); bool isValid = !string.IsNullOrEmpty(mSymbolSequence) && !string.IsNullOrEmpty(mSymbolSprite); GUI.backgroundColor = isValid ? Color.green : Color.grey; if (GUILayout.Button("Add", GUILayout.Width(40f)) && isValid) { NGUIEditorTools.RegisterUndo("Add symbol", mFont); mFont.AddSymbol(mSymbolSequence, mSymbolSprite); mFont.MarkAsChanged(); mSymbolSequence = ""; mSymbolSprite = ""; } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); if (symbols.Count == 0) { EditorGUILayout.HelpBox("Want to add an emoticon to your font? In the field above type ':)', choose a sprite, then hit the Add button.", MessageType.Info); } else { GUILayout.Space(4f); } NGUIEditorTools.EndContents(); } } } if (mFont.bmFont != null && mFont.bmFont.isValid) { if (NGUIEditorTools.DrawHeader("Modify")) { NGUIEditorTools.BeginContents(); UISpriteData sd = mFont.sprite; bool disable = (sd != null && (sd.paddingLeft != 0 || sd.paddingBottom != 0)); EditorGUI.BeginDisabledGroup(disable || mFont.packedFontShader); EditorGUILayout.BeginHorizontal(); GUILayout.Space(20f); EditorGUILayout.BeginVertical(); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); NGUISettings.foregroundColor = EditorGUILayout.ColorField("Foreground", NGUISettings.foregroundColor); NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor); GUILayout.EndVertical(); mCurve = EditorGUILayout.CurveField("", mCurve, GUILayout.Width(40f), GUILayout.Height(40f)); GUILayout.EndHorizontal(); if (GUILayout.Button("Add a Shadow")) { ApplyEffect(Effect.Shadow, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Add a Soft Outline")) { ApplyEffect(Effect.Outline, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Rebalance Colors")) { ApplyEffect(Effect.Rebalance, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Alpha")) { ApplyEffect(Effect.AlphaCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Foreground")) { ApplyEffect(Effect.ForegroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Background")) { ApplyEffect(Effect.BackgroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } GUILayout.Space(10f); if (GUILayout.Button("Add Transparent Border (+1)")) { ApplyEffect(Effect.Border, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Remove Border (-1)")) { ApplyEffect(Effect.Crop, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } EditorGUILayout.EndVertical(); GUILayout.Space(20f); EditorGUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); if (disable) { GUILayout.Space(3f); EditorGUILayout.HelpBox("The sprite used by this font has been trimmed and is not suitable for modification. " + "Try re-adding this sprite with 'Trim Alpha' disabled.", MessageType.Warning); } NGUIEditorTools.EndContents(); } } // The font must be valid at this point for the rest of the options to show up if (mFont.isDynamic || mFont.bmFont.isValid) { if (mFont.atlas == null) { mView = View.Font; mUseShader = false; } } // Preview option if (!mFont.isDynamic && mFont.atlas != null) { GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Preview", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)); } GUILayout.EndHorizontal(); } }
//public static void BatchCreateArtistFont() //{ // string dirName = ""; // string fntname = EditorUtils.SelectObjectPathInfo(ref dirName).Split('.')[0]; // Debug.Log(fntname); // Debug.Log(dirName); // string fntFileName = dirName + fntname + ".fnt"; // Font CustomFont = new Font(); // { // AssetDatabase.CreateAsset(CustomFont, dirName + fntname + ".fontsettings"); // AssetDatabase.SaveAssets(); // } // TextAsset BMFontText = null; // { // BMFontText = AssetDatabase.LoadAssetAtPath(fntFileName, typeof(TextAsset)) as TextAsset; // } // BMFont mbFont = new BMFont(); // BMFontReader.Load(mbFont, BMFontText.name, BMFontText.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; // float uvx = 1f * bmInfo.x / mbFont.texWidth; // float uvy = 1 - (1f * bmInfo.y / mbFont.texHeight); // float uvw = 1f * bmInfo.width / mbFont.texWidth; // float uvh = -1f * bmInfo.height / mbFont.texHeight; // info.uvBottomLeft = new Vector2(uvx, uvy); // info.uvBottomRight = new Vector2(uvx + uvw, uvy); // info.uvTopLeft = new Vector2(uvx, uvy + uvh); // info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh); // info.minX = bmInfo.offsetX; // info.minY = bmInfo.offsetY + bmInfo.height / 2; // 这样调出来的效果是ok的,原理未知 // info.glyphWidth = bmInfo.width; // info.glyphHeight = -bmInfo.height; // 同上,不知道为什么要用负的,可能跟unity纹理uv有关 // info.advance = bmInfo.advance; // characterInfo[i] = info; // } // CustomFont.characterInfo = characterInfo; // string textureFilename = dirName + mbFont.spriteName + ".png"; // Material mat = null; // { // Shader shader = Shader.Find("Transparent/Diffuse"); // mat = new Material(shader); // Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; // mat.SetTexture("_MainTex", tex); // AssetDatabase.CreateAsset(mat, dirName + fntname + ".mat"); // AssetDatabase.SaveAssets(); // } // CustomFont.material = mat; // EditorUtility.SetDirty(CustomFont); // AssetDatabase.SaveAssets(); // EditorUtility.DisplayDialog("完成", $"字体文件{BMFontText.name}生成完成!", "OK"); //} internal static void BatchCreateArtistFont(string path) { string dirName = Path.GetDirectoryName(path); string fntname = Path.GetFileNameWithoutExtension(path); string fntFileName = path; Debug.Log(fntname); Debug.Log(dirName); Debug.Log(fntFileName); Font CustomFont = new Font(); { AssetDatabase.CreateAsset(CustomFont, Path.Combine(dirName, fntname + ".fontsettings")); AssetDatabase.SaveAssets(); } TextAsset BMFontText = null; { BMFontText = AssetDatabase.LoadAssetAtPath(fntFileName, typeof(TextAsset)) as TextAsset; } BMFont mbFont = new BMFont(); //BMFontReader.Load(mbFont, BMFontText.name, BMFontText.bytes); // 借用NGUI封装的读取类 BMFontReader.Load(mbFont, BMFontText.name, File.ReadAllText(Path.GetFullPath(fntFileName))); // 借用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; float uvx = 1f * bmInfo.x / mbFont.texWidth; float uvy = 1 - (1f * bmInfo.y / mbFont.texHeight); float uvw = 1f * bmInfo.width / mbFont.texWidth; float uvh = -1f * bmInfo.height / mbFont.texHeight; info.uvBottomLeft = new Vector2(uvx, uvy); info.uvBottomRight = new Vector2(uvx + uvw, uvy); info.uvTopLeft = new Vector2(uvx, uvy + uvh); info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh); info.minX = bmInfo.offsetX; info.minY = bmInfo.offsetY + bmInfo.height / 2; // 这样调出来的效果是ok的,原理未知 info.glyphWidth = bmInfo.width; info.glyphHeight = -bmInfo.height; // 同上,不知道为什么要用负的,可能跟unity纹理uv有关 info.advance = bmInfo.advance; characterInfo[i] = info; } CustomFont.characterInfo = characterInfo; Debug.LogError(mbFont.spriteName); string textureFilename = Path.Combine(dirName, mbFont.spriteName + ".png"); Material mat = null; { Shader shader = Shader.Find("Transparent/Diffuse"); mat = new Material(shader); Texture tex = AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture)) as Texture; mat.SetTexture("_MainTex", tex); AssetDatabase.CreateAsset(mat, Path.Combine(dirName, fntname + ".mat")); AssetDatabase.SaveAssets(); } CustomFont.material = mat; EditorUtility.SetDirty(CustomFont); AssetDatabase.SaveAssets(); EditorUtility.DisplayDialog("完成", $"字体文件{BMFontText.name}生成完成!", "OK"); }
void OnGUI() { fontData = EditorGUILayout.ObjectField("Font Data", fontData, typeof(TextAsset), false) as TextAsset; fontTexture = EditorGUILayout.ObjectField("Font Texture", fontTexture, typeof(Texture2D), false) as Texture2D; if (GUILayout.Button("Create BMFont")) { if (null == fontData || null == fontTexture) { EditorUtility.DisplayDialog("Error", "两个数据均不能为空", "OK"); return; } string localPath = AssetDatabase.GetAssetPath(fontData); string directoryName = Path.GetDirectoryName(localPath); string fileName = Path.GetFileNameWithoutExtension(localPath); string filePath = directoryName + "/" + fileName + ".mat"; fontMaterial = new Material(Shader.Find("UI/Default")); fontMaterial.mainTexture = fontTexture; fontSavePath = filePath; AssetDatabase.CreateAsset(fontMaterial, fontSavePath); fontMaterial = AssetDatabase.LoadAssetAtPath <Material>(fontSavePath); BMFontReader.Load(m_bmFont, fontData.name, fontData.bytes); CharacterInfo[] characterInfo = new CharacterInfo[m_bmFont.glyphs.Count]; filePath = directoryName + "/" + fileName + ".fontsettings"; targetFont = new Font(fileName); targetFont.material = fontMaterial; for (int i = 0; i < m_bmFont.glyphs.Count; i++) { BMGlyph bmInfo = m_bmFont.glyphs[i]; CharacterInfo info = new CharacterInfo(); info.index = bmInfo.index; float uvx = (float)(bmInfo.x) / m_bmFont.texWidth; float uvy = 1.0f * (m_bmFont.texHeight - bmInfo.y - bmInfo.height) / m_bmFont.texHeight; float uvw = (float)bmInfo.width / m_bmFont.texWidth; float uvh = 1.0f * bmInfo.height / m_bmFont.texHeight; info.uvBottomLeft = new Vector2(uvx, uvy); info.uvBottomRight = new Vector2(uvx + uvw, uvy); info.uvTopLeft = new Vector2(uvx, uvy + uvh); info.uvTopRight = new Vector2(uvx + uvw, uvy + uvh); info.minX = bmInfo.offsetX; info.minY = bmInfo.offsetY - bmInfo.height; info.glyphWidth = bmInfo.width; info.glyphHeight = bmInfo.height; info.advance = bmInfo.advance; characterInfo[i] = info; } targetFont.characterInfo = characterInfo; AssetDatabase.CreateAsset(targetFont, filePath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //Close(); } }
override public void OnInspectorGUI() { mFont = target as UIFont; EditorGUIUtility.LookLikeControls(80f); NGUIEditorTools.DrawSeparator(); //Lindean if (mFont.dynamicFont != null) { mType = FontType.Dynamic; } if (mFont.replacement != null) { mType = FontType.Reference; mReplacement = mFont.replacement; } else if (mFont.dynamicFont_1 != null) { mType = FontType.Dynamic; } GUILayout.BeginHorizontal(); FontType fontType = (FontType)EditorGUILayout.EnumPopup("Font Type", mType); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (mType != fontType) { if (fontType == FontType.Normal) { OnSelectFont(null); } else { mType = fontType; } //Lindean if (mType != FontType.Dynamic && mFont.dynamicFont_1 != null) { mFont.dynamicFont_1 = null; } } //Lindean if (mType == FontType.Dynamic) { //Lindean - Draw settings for dynamic font bool changed = false; Font f = EditorGUILayout.ObjectField("Font", mFont.dynamicFont, typeof(Font), false) as Font; if (f != mFont.dynamicFont) { mFont.dynamicFont = f; changed = true; } Material mat = EditorGUILayout.ObjectField("Material", mFont.dynamicFontMaterial_1, typeof(Material), false) as Material; if (mat != mFont.dynamicFontMaterial_1) { mFont.dynamicFontMaterial_1 = mat; changed = true; } if (mFont.dynamicFontMaterial_1 == null) { GUILayout.Label("Warning: no coloring or clipping when using default font material"); } int i = EditorGUILayout.IntField("Size", mFont.dynamicFontSize); if (i != mFont.dynamicFontSize) { mFont.dynamicFontSize = i; changed = true; } FontStyle style = (FontStyle)EditorGUILayout.EnumPopup("Style", mFont.dynamicFontStyle); if (style != mFont.dynamicFontStyle) { mFont.dynamicFontStyle = style; changed = true; } if (changed) { //force access to material property as it refreshes the texture assignment Debug.Log("font changed..."); Material fontMat = mFont.material; if (fontMat.mainTexture == null) { Debug.Log("font material texture issue..."); } UIFont.OnFontRebuilt(mFont); } NGUIEditorTools.DrawSeparator(); // Font spacing GUILayout.BeginHorizontal(); { EditorGUIUtility.LookLikeControls(0f); GUILayout.Label("Spacing", GUILayout.Width(60f)); GUILayout.Label("X", GUILayout.Width(12f)); int x = EditorGUILayout.IntField(mFont.horizontalSpacing); GUILayout.Label("Y", GUILayout.Width(12f)); int y = EditorGUILayout.IntField(mFont.verticalSpacing); EditorGUIUtility.LookLikeControls(80f); if (mFont.horizontalSpacing != x || mFont.verticalSpacing != y) { NGUIEditorTools.RegisterUndo("Font Spacing", mFont); mFont.horizontalSpacing = x; mFont.verticalSpacing = y; } } GUILayout.EndHorizontal(); } //Lindean if (mType == FontType.Reference) { ComponentSelector.Draw <UIFont>(mFont.replacement, OnSelectFont); NGUIEditorTools.DrawSeparator(); GUILayout.Label("You can have one font simply point to\n" + "another one. This is useful if you want to be\n" + "able to quickly replace the contents of one\n" + "font with another one, for example for\n" + "swapping an SD font with an HD one, or\n" + "replacing an English font with a Chinese\n" + "one. All the labels referencing this font\n" + "will update their references to the new one."); if (mReplacement != mFont && mFont.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Font Change", mFont); mFont.replacement = mReplacement; UnityEditor.EditorUtility.SetDirty(mFont); } return; } else if (mType == FontType.Dynamic) { #if UNITY_3_5 EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error); #else NGUIEditorTools.DrawSeparator(); Font fnt = EditorGUILayout.ObjectField("TTF Font", mFont.dynamicFont_1, typeof(Font), false) as Font; if (fnt != mFont.dynamicFont_1) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.dynamicFont_1 = fnt; } GUILayout.BeginHorizontal(); int size = EditorGUILayout.IntField("Size", mFont.dynamicFontSize_1, GUILayout.Width(120f)); FontStyle style = (FontStyle)EditorGUILayout.EnumPopup(mFont.dynamicFontStyle_1); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (size != mFont.dynamicFontSize_1) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.dynamicFontSize_1 = size; } if (style != mFont.dynamicFontStyle_1) { NGUIEditorTools.RegisterUndo("Font change", mFont); mFont.dynamicFontStyle_1 = style; } Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } #endif } else { NGUIEditorTools.DrawSeparator(); ComponentSelector.Draw <UIAtlas>(mFont.atlas, OnSelectAtlas); if (mFont.atlas != null) { if (mFont.bmFont.isValid) { NGUIEditorTools.AdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false); } EditorGUILayout.Space(); } else { // No atlas specified -- set the material and texture rectangle directly Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } } // For updating the font's data when importing from an external source, such as the texture packer bool resetWidthHeight = false; if (mFont.atlas != null || mFont.material != null) { TextAsset data = EditorGUILayout.ObjectField("Import Data", null, typeof(TextAsset), false) as TextAsset; if (data != null) { NGUIEditorTools.RegisterUndo("Import Font Data", mFont); BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes); mFont.MarkAsDirty(); resetWidthHeight = true; Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters"); } } if (mFont.bmFont.isValid) { Color green = new Color(0.4f, 1f, 0f, 1f); Texture2D tex = mFont.texture; if (tex != null) { if (mFont.atlas == null) { // Pixels are easier to work with than UVs Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false); // Automatically set the width and height of the rectangle to be the original font texture's dimensions if (resetWidthHeight) { pixels.width = mFont.texWidth; pixels.height = mFont.texHeight; } // Font sprite rectangle GUI.backgroundColor = green; pixels = EditorGUILayout.RectField("Pixel Rect", pixels); GUI.backgroundColor = Color.white; // Create a button that can make the coordinates pixel-perfect on click GUILayout.BeginHorizontal(); { Rect corrected = NGUIMath.MakePixelPerfect(pixels); if (corrected == pixels) { GUI.color = Color.grey; GUILayout.Button("Make Pixel-Perfect"); GUI.color = Color.white; } else if (GUILayout.Button("Make Pixel-Perfect")) { pixels = corrected; GUI.changed = true; } } GUILayout.EndHorizontal(); // Convert the pixel coordinates back to UV coordinates Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height); if (mFont.uvRect != uvRect) { NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont); mFont.uvRect = uvRect; } //NGUIEditorTools.DrawSeparator(); EditorGUILayout.Space(); } } } } // The font must be valid at this point for the rest of the options to show up if (mFont.isDynamic || mFont.bmFont.isValid) { // Font spacing GUILayout.BeginHorizontal(); { EditorGUIUtility.LookLikeControls(0f); GUILayout.Label("Spacing", GUILayout.Width(60f)); GUILayout.Label("X", GUILayout.Width(12f)); int x = EditorGUILayout.IntField(mFont.horizontalSpacing); GUILayout.Label("Y", GUILayout.Width(12f)); int y = EditorGUILayout.IntField(mFont.verticalSpacing); GUILayout.Space(18f); EditorGUIUtility.LookLikeControls(80f); if (mFont.horizontalSpacing != x || mFont.verticalSpacing != y) { NGUIEditorTools.RegisterUndo("Font Spacing", mFont); mFont.horizontalSpacing = x; mFont.verticalSpacing = y; } } GUILayout.EndHorizontal(); if (mFont.atlas == null) { mView = View.Font; mUseShader = false; float pixelSize = EditorGUILayout.FloatField("Pixel Size", mFont.pixelSize, GUILayout.Width(120f)); if (pixelSize != mFont.pixelSize) { NGUIEditorTools.RegisterUndo("Font Change", mFont); mFont.pixelSize = pixelSize; } } EditorGUILayout.Space(); } // Preview option if (!mFont.isDynamic && mFont.atlas != null) { GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Preview", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)); } GUILayout.EndHorizontal(); } // Dynamic fonts don't support emoticons if (!mFont.isDynamic && mFont.bmFont.isValid) { if (mFont.atlas != null) { NGUIEditorTools.DrawHeader("Symbols and Emoticons"); List <BMSymbol> symbols = mFont.symbols; for (int i = 0; i < symbols.Count;) { BMSymbol sym = symbols[i]; GUILayout.BeginHorizontal(); GUILayout.Label(sym.sequence, GUILayout.Width(40f)); if (NGUIEditorTools.SimpleSpriteField(mFont.atlas, sym.spriteName, ChangeSymbolSprite)) { mSelectedSymbol = sym; } if (GUILayout.Button("Edit", GUILayout.Width(40f))) { if (mFont.atlas != null) { EditorPrefs.SetString("NGUI Selected Sprite", sym.spriteName); NGUIEditorTools.Select(mFont.atlas.gameObject); } } GUI.backgroundColor = Color.red; if (GUILayout.Button("X", GUILayout.Width(22f))) { NGUIEditorTools.RegisterUndo("Remove symbol", mFont); mSymbolSequence = sym.sequence; mSymbolSprite = sym.spriteName; symbols.Remove(sym); mFont.MarkAsDirty(); } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); GUILayout.Space(4f); ++i; } if (symbols.Count > 0) { NGUIEditorTools.DrawSeparator(); } GUILayout.BeginHorizontal(); mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f)); NGUIEditorTools.SimpleSpriteField(mFont.atlas, mSymbolSprite, SelectSymbolSprite); bool isValid = !string.IsNullOrEmpty(mSymbolSequence) && !string.IsNullOrEmpty(mSymbolSprite); GUI.backgroundColor = isValid ? Color.green : Color.grey; if (GUILayout.Button("Add", GUILayout.Width(40f)) && isValid) { NGUIEditorTools.RegisterUndo("Add symbol", mFont); mFont.AddSymbol(mSymbolSequence, mSymbolSprite); mFont.MarkAsDirty(); mSymbolSequence = ""; mSymbolSprite = ""; } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); if (symbols.Count == 0) { EditorGUILayout.HelpBox("Want to add an emoticon to your font? In the field above type ':)', choose a sprite, then hit the Add button.", MessageType.Info); } else { GUILayout.Space(4f); } } } }
/// <summary> /// Draw the UI for this tool. /// </summary> void OnGUI() { #pragma warning disable 0618 string prefabPath = ""; string matPath = ""; if (UISettings.font != null && UISettings.font.name == UISettings.fontName) { prefabPath = AssetDatabase.GetAssetPath(UISettings.font.gameObject.GetInstanceID()); if (UISettings.font.material != null) { matPath = AssetDatabase.GetAssetPath(UISettings.font.material.GetInstanceID()); } } // Assume default values if needed if (string.IsNullOrEmpty(UISettings.fontName)) { UISettings.fontName = "New Font"; } if (string.IsNullOrEmpty(prefabPath)) { prefabPath = NGUIEditorTools.GetSelectionFolder() + UISettings.fontName + ".prefab"; } if (string.IsNullOrEmpty(matPath)) { matPath = NGUIEditorTools.GetSelectionFolder() + UISettings.fontName + ".mat"; } EditorGUIUtility.LookLikeControls(80f); NGUIEditorTools.DrawHeader("Input"); UISettings.fontData = EditorGUILayout.ObjectField("Font Data", UISettings.fontData, typeof(TextAsset), false) as TextAsset; UISettings.fontTexture = EditorGUILayout.ObjectField("Texture", UISettings.fontTexture, typeof(Texture2D), false) as Texture2D; // Draw the atlas selection only if we have the font data and texture specified, just to make it easier if (UISettings.fontData != null && UISettings.fontTexture != null) { NGUIEditorTools.DrawHeader("Output"); GUILayout.BeginHorizontal(); GUILayout.Label("Font Name", GUILayout.Width(76f)); GUI.backgroundColor = Color.white; UISettings.fontName = GUILayout.TextField(UISettings.fontName); GUILayout.EndHorizontal(); ComponentSelector.Draw <UIFont>("...or select", UISettings.font, OnSelectFont); ComponentSelector.Draw <UIAtlas>(UISettings.atlas, OnSelectAtlas); } NGUIEditorTools.DrawSeparator(); // Helpful info if (UISettings.fontData == null) { GUILayout.Label( "The font creation mostly takes place outside\n" + "of Unity. You can use BMFont on Windows\n" + "or your choice of Glyph Designer or the\n" + "less expensive bmGlyph on the Mac.\n\n" + "Either of those tools will create a TXT for\n" + "you that you will drag & drop into the\n" + "field above."); } else if (UISettings.fontTexture == null) { GUILayout.Label( "When exporting your font, you should get\n" + "two files: the TXT, and the texture. Only\n" + "one texture can be used per font."); } else if (UISettings.atlas == null) { GUILayout.Label( "You can create a font that doesn't use a\n" + "texture atlas. This will mean that the text\n" + "labels using this font will generate an extra\n" + "draw call, and will need to be sorted by\n" + "adjusting the Z instead of the Depth.\n\n" + "If you do specify an atlas, the font's texture\n" + "will be added to it automatically."); NGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUI.backgroundColor = Color.red; bool create = GUILayout.Button("Create a Font without an Atlas", GUILayout.Width(200f)); GUI.backgroundColor = Color.white; GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (create) { GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; if (go == null || EditorUtility.DisplayDialog("Are you sure?", "Are you sure you want to replace the contents of the " + UISettings.fontName + " font with the currently selected values? This action can't be undone.", "Yes", "No")) { // Try to load the material Material mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; // If the material doesn't exist, create it if (mat == null) { Shader shader = Shader.Find("Unlit/Transparent Colored"); mat = new Material(shader); // Save the material AssetDatabase.CreateAsset(mat, matPath); AssetDatabase.Refresh(); // Load the material so it's usable mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material; } mat.mainTexture = UISettings.fontTexture; if (go == null || go.GetComponent <UIFont>() == null) { // Create a new prefab for the atlas #if UNITY_3_4 Object prefab = EditorUtility.CreateEmptyPrefab(prefabPath); #else Object prefab = PrefabUtility.CreateEmptyPrefab(prefabPath); #endif // Create a new game object for the font go = new GameObject(UISettings.fontName); UISettings.font = go.AddComponent <UIFont>(); UISettings.font.material = mat; BMFontReader.Load(UISettings.font.bmFont, NGUITools.GetHierarchy(UISettings.font.gameObject), UISettings.fontData.bytes); // Update the prefab #if UNITY_3_4 EditorUtility.ReplacePrefab(go, prefab); #else PrefabUtility.ReplacePrefab(go, prefab); #endif DestroyImmediate(go); AssetDatabase.Refresh(); // Select the atlas go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; } UISettings.font = go.GetComponent <UIFont>(); MarkAsChanged(); } } } else { GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; bool create = false; GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (go != null) { if (go.GetComponent <UIFont>() != null) { GUI.backgroundColor = Color.red; create = GUILayout.Button("Replace the Font", GUILayout.Width(140f)); } else { GUI.backgroundColor = Color.grey; GUILayout.Button("Rename Your Font", GUILayout.Width(140f)); } } else { GUI.backgroundColor = Color.green; create = GUILayout.Button("Create the Font", GUILayout.Width(140f)); } GUI.backgroundColor = Color.white; GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (create) { if (go == null || EditorUtility.DisplayDialog("Are you sure?", "Are you sure you want to replace the contents of the " + UISettings.fontName + " font with the currently selected values? This action can't be undone.", "Yes", "No")) { UIAtlasMaker.AddOrUpdate(UISettings.atlas, UISettings.fontTexture); if (go == null || go.GetComponent <UIFont>() == null) { // Create a new prefab for the atlas #if UNITY_3_4 Object prefab = EditorUtility.CreateEmptyPrefab(prefabPath); #else Object prefab = PrefabUtility.CreateEmptyPrefab(prefabPath); #endif // Create a new game object for the font go = new GameObject(UISettings.fontName); UISettings.font = go.AddComponent <UIFont>(); UISettings.font.atlas = UISettings.atlas; UISettings.font.spriteName = UISettings.fontTexture.name; BMFontReader.Load(UISettings.font.bmFont, NGUITools.GetHierarchy(UISettings.font.gameObject), UISettings.fontData.bytes); // Update the prefab #if UNITY_3_4 EditorUtility.ReplacePrefab(go, prefab); #else PrefabUtility.ReplacePrefab(go, prefab); #endif DestroyImmediate(go); AssetDatabase.Refresh(); // Select the atlas go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; } else if (UISettings.fontData != null) { BMFontReader.Load(UISettings.font.bmFont, NGUITools.GetHierarchy(UISettings.font.gameObject), UISettings.fontData.bytes); EditorUtility.SetDirty(UISettings.font); UISettings.font.MarkAsDirty(); } UISettings.font = go.GetComponent <UIFont>(); UISettings.font.spriteName = UISettings.fontTexture.name; UISettings.font.atlas = UISettings.atlas; MarkAsChanged(); } } } #pragma warning restore 0618 }
public override void OnInspectorGUI() { NGUIEditorTools.SetLabelWidth(80f); GUILayout.Space(6f); var type = mFont.type; if (type == NGUIFontType.Reference) { mReplacement = mFont.replacement; } GUI.changed = false; GUILayout.BeginHorizontal(); type = (NGUIFontType)EditorGUILayout.EnumPopup("Font Type", type); GUILayout.EndHorizontal(); if (GUI.changed) { NGUIEditorTools.RegisterUndo("Font Change", mFont as Object); mFont.type = type; NGUITools.SetDirty(mFont as Object); } if (type == NGUIFontType.Reference) { ComponentSelector.Draw(mFont.replacement, OnSelectFont, true); GUILayout.Space(6f); EditorGUILayout.HelpBox("Reference fonts will have their font data come from another one. " + "This allows you to have your labels reference a font that has all the emoticons, that also points to a secondary font containing the actual glyph data " + "that can be easily swapped at run-time with a font containing glyphs of another language.", MessageType.Info); if (mReplacement != mFont && mFont.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Font Change", mFont as Object); mFont.replacement = mReplacement; NGUITools.SetDirty(mFont as Object); } } if (type == NGUIFontType.Dynamic) { #if UNITY_3_5 EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error); #else Font fnt = EditorGUILayout.ObjectField("TTF Font", mFont.dynamicFont, typeof(Font), false) as Font; if (fnt != mFont.dynamicFont) { NGUIEditorTools.RegisterUndo("Font change", mFont as Object); mFont.dynamicFont = fnt; } var mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont as Object); mFont.material = mat; } #endif } if (type == NGUIFontType.Bitmap) { ComponentSelector.Draw(mFont.atlas, OnSelectAtlas, true); if (mFont.atlas != null) { if (mFont.bmFont.isValid) { NGUIEditorTools.DrawAdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false); } } else { // No atlas specified -- set the material and texture rectangle directly var mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont as Object); mFont.material = mat; } } } if (type != NGUIFontType.Reference) { GUI.changed = false; //EditorGUI.BeginDisabledGroup(mFont.isDynamic); var size = EditorGUILayout.IntField("Default Size", mFont.defaultSize, GUILayout.Width(120f)); //EditorGUI.EndDisabledGroup(); GUILayout.BeginHorizontal(); int space = EditorGUILayout.IntField("Space Width", mFont.spaceWidth, GUILayout.Width(120f)); if (space == 0) { GUILayout.Label("(default)"); } GUILayout.EndHorizontal(); if (GUI.changed) { NGUIEditorTools.RegisterUndo("Font change", mFont as Object); mFont.spaceWidth = space; mFont.defaultSize = size; var labels = NGUITools.FindActive <UILabel>(); foreach (UILabel lbl in labels) { if (lbl.font == null) { continue; } var font = lbl.font; if (NGUITools.CheckIfRelated(font, mFont)) { lbl.font = null; lbl.font = font; NGUITools.SetDirty(lbl); } } } EditorGUILayout.Space(); } if (type == NGUIFontType.Dynamic || type == NGUIFontType.Reference) { var font = mFont as NGUIFont; if (font != null) { if (NGUIEditorTools.DrawHeader("Symbol Atlas")) { NGUIEditorTools.BeginContents(); ComponentSelector.Draw(font.symbolAtlas, OnSelectSymbolAtlas, true); NGUIEditorTools.EndContents(); } EditorGUILayout.Space(); } } // For updating the font's data when importing from an external source, such as the texture packer bool resetWidthHeight = false; if (type == NGUIFontType.Bitmap && (mFont.atlas != null || mFont.material != null)) { if (NGUIEditorTools.DrawHeader("Import")) { NGUIEditorTools.BeginContents(); var data = EditorGUILayout.ObjectField("Import Data", null, typeof(TextAsset), false) as TextAsset; if (data != null) { NGUIEditorTools.RegisterUndo("Import Font Data", mFont as Object); BMFontReader.Load(mFont.bmFont, (mFont as Object).name, data.bytes); mFont.MarkAsChanged(); resetWidthHeight = true; Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters"); } NGUIEditorTools.EndContents(); } EditorGUILayout.Space(); } if (mFont is UIFont && mFont.bmFont.isValid) { EditorGUILayout.HelpBox("Legacy font type should be upgraded in order to maintain compatibility with Unity 2018 and newer.", MessageType.Warning, true); if (GUILayout.Button("Upgrade")) { var path = EditorUtility.SaveFilePanelInProject("Save As", (mFont as Object).name + ".asset", "asset", "Save font as...", NGUISettings.currentPath); if (!string.IsNullOrEmpty(path)) { NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path); var asset = ScriptableObject.CreateInstance <NGUIFont>(); asset.bmFont = mFont.bmFont; asset.symbols = mFont.symbols; asset.atlas = mFont.atlas; asset.spriteName = mFont.spriteName; asset.uvRect = mFont.uvRect; asset.defaultSize = mFont.defaultSize; var fontName = path.Replace(".asset", ""); fontName = fontName.Substring(path.LastIndexOfAny(new char[] { '/', '\\' }) + 1); asset.name = fontName; var existing = AssetDatabase.LoadMainAssetAtPath(path); if (existing != null) { EditorUtility.CopySerialized(asset, existing); } else { AssetDatabase.CreateAsset(asset, path); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); asset = AssetDatabase.LoadAssetAtPath <NGUIFont>(path); NGUISettings.ambigiousFont = asset; Selection.activeObject = asset; if (asset != null) { mFont.replacement = asset; mFont.MarkAsChanged(); } } } } if (type == NGUIFontType.Bitmap && mFont.bmFont.isValid) { Texture2D tex = mFont.texture; if (tex != null && mFont.atlas == null) { // Pixels are easier to work with than UVs Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false); // Automatically set the width and height of the rectangle to be the original font texture's dimensions if (resetWidthHeight) { pixels.width = mFont.texWidth; pixels.height = mFont.texHeight; } // Font sprite rectangle pixels = EditorGUILayout.RectField("Pixel Rect", pixels); // Convert the pixel coordinates back to UV coordinates Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height); if (mFont.uvRect != uvRect) { NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont as Object); mFont.uvRect = uvRect; } //NGUIEditorTools.DrawSeparator(); EditorGUILayout.Space(); } } var nguiFont = mFont as NGUIFont; var symbolAtlas = (nguiFont != null) ? nguiFont.symbolAtlas : null; if (symbolAtlas == null && type != NGUIFontType.Reference) { symbolAtlas = mFont.atlas; } if (symbolAtlas != null && NGUIEditorTools.DrawHeader("Symbols and Emoticons")) { NGUIEditorTools.BeginContents(); var symbols = mFont.symbols; for (int i = 0; i < symbols.Count;) { var sym = symbols[i]; GUILayout.BeginHorizontal(); GUILayout.Label(sym.sequence, GUILayout.Width(40f)); if (NGUIEditorTools.DrawSpriteField(symbolAtlas, sym.spriteName, ChangeSymbolSprite, GUILayout.MinWidth(100f))) { mSelectedSymbol = sym; } var col = GUILayout.Toggle(sym.colored, new GUIContent(""), GUILayout.Width(16f)); if (col != sym.colored) { sym.colored = col; mFont.MarkAsChanged(); } if (GUILayout.Button("Edit", GUILayout.Width(40f)) && symbolAtlas != null) { NGUISettings.atlas = symbolAtlas; NGUISettings.selectedSprite = sym.spriteName; NGUIEditorTools.Select(symbolAtlas as Object); } GUI.backgroundColor = Color.red; if (GUILayout.Button("X", GUILayout.Width(22f))) { NGUIEditorTools.RegisterUndo("Remove symbol", mFont as Object); mSymbolSequence = sym.sequence; mSymbolSprite = sym.spriteName; symbols.Remove(sym); mFont.MarkAsChanged(); } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); //GUILayout.Space(4f); ++i; } if (symbols.Count > 0) { if (Event.current.type == EventType.Repaint) { Texture2D tex = NGUIEditorTools.blankTexture; Rect rect = GUILayoutUtility.GetLastRect(); GUI.color = new Color(0f, 0f, 0f, 0.25f); GUI.DrawTexture(new Rect(20f, rect.yMin + 28f, Screen.width - 30f, 1f), tex); GUI.color = Color.white; } GUILayout.Space(10f); } GUILayout.BeginHorizontal(); mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f)); NGUIEditorTools.DrawSpriteField(symbolAtlas, mSymbolSprite, SelectSymbolSprite); bool isValid = !string.IsNullOrEmpty(mSymbolSequence) && !string.IsNullOrEmpty(mSymbolSprite); GUI.backgroundColor = isValid ? Color.green : Color.grey; if (GUILayout.Button("Add", GUILayout.Width(40f)) && isValid) { NGUIEditorTools.RegisterUndo("Add symbol", mFont as Object); var sym = mFont.AddSymbol(mSymbolSequence, mSymbolSprite); sym.colored = mSymbolColored; mFont.MarkAsChanged(); mSymbolSequence = ""; mSymbolSprite = ""; } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); NGUIEditorTools.SetLabelWidth(42f); GUILayout.BeginHorizontal(); mSymbolColored = EditorGUILayout.Toggle("Tinted", mSymbolColored, GUILayout.Width(60f)); GUILayout.Label("- affected by label's color by default"); GUILayout.EndHorizontal(); NGUIEditorTools.SetLabelWidth(80f); if (symbols.Count == 0) { EditorGUILayout.HelpBox("Want to add an emoticon to your font? In the field above type ':)', choose a sprite, then hit the Add button.", MessageType.Info); } else { GUILayout.Space(4f); } if (nguiFont != null) { if (Event.current.type == EventType.Repaint) { Texture2D tex = NGUIEditorTools.blankTexture; Rect rect = GUILayoutUtility.GetLastRect(); GUI.color = new Color(0f, 0f, 0f, 0.25f); GUI.DrawTexture(new Rect(20f, rect.yMin + 6f, Screen.width - 30f, 1f), tex); GUI.color = Color.white; } GUILayout.Space(6f); GUI.changed = false; GUILayout.BeginHorizontal(); GUILayout.Label("Scale", GUILayout.Width(40f)); var scale = EditorGUILayout.FloatField(nguiFont.symbolScale, GUILayout.Width(50f)); GUILayout.Label("- scaling multiplier"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("Offset", GUILayout.Width(40f)); var offset = EditorGUILayout.IntField(nguiFont.symbolOffset, GUILayout.Width(50f)); GUILayout.Label("- vertical offset"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("Height", GUILayout.Width(40f)); var height = EditorGUILayout.IntField(nguiFont.symbolMaxHeight, GUILayout.Width(50f)); GUILayout.Label("- maximum sprite height"); GUILayout.EndHorizontal(); if (GUI.changed) { NGUIEditorTools.RegisterUndo("Change symbol values", mFont as Object); nguiFont.symbolScale = scale; nguiFont.symbolOffset = offset; nguiFont.symbolMaxHeight = height; nguiFont.MarkAsChanged(); } } NGUIEditorTools.EndContents(); } if (type == NGUIFontType.Bitmap && mFont.bmFont != null && mFont.bmFont.isValid) { if (NGUIEditorTools.DrawHeader("Modify")) { NGUIEditorTools.BeginContents(); UISpriteData sd = mFont.sprite; bool disable = (sd != null && (sd.paddingLeft != 0 || sd.paddingBottom != 0)); EditorGUI.BeginDisabledGroup(disable || mFont.packedFontShader); EditorGUILayout.BeginHorizontal(); GUILayout.Space(20f); EditorGUILayout.BeginVertical(); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); NGUISettings.foregroundColor = EditorGUILayout.ColorField("Foreground", NGUISettings.foregroundColor); NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor); GUILayout.EndVertical(); mCurve = EditorGUILayout.CurveField("", mCurve, GUILayout.Width(40f), GUILayout.Height(40f)); GUILayout.EndHorizontal(); if (GUILayout.Button("Add a Shadow")) { ApplyEffect(Effect.Shadow, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Add a Soft Outline")) { ApplyEffect(Effect.Outline, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Rebalance Colors")) { ApplyEffect(Effect.Rebalance, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Alpha")) { ApplyEffect(Effect.AlphaCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Foreground")) { ApplyEffect(Effect.ForegroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Apply Curve to Background")) { ApplyEffect(Effect.BackgroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } GUILayout.Space(10f); if (GUILayout.Button("Add Transparent Border (+1)")) { ApplyEffect(Effect.Border, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } if (GUILayout.Button("Remove Border (-1)")) { ApplyEffect(Effect.Crop, NGUISettings.foregroundColor, NGUISettings.backgroundColor); } EditorGUILayout.EndVertical(); GUILayout.Space(20f); EditorGUILayout.EndHorizontal(); EditorGUI.EndDisabledGroup(); if (disable) { GUILayout.Space(3f); EditorGUILayout.HelpBox("The sprite used by this font has been trimmed and is not suitable for modification. " + "Try re-adding this sprite with 'Trim Alpha' disabled.", MessageType.Warning); } NGUIEditorTools.EndContents(); } } if (mFont.atlas == null) { mView = View.Font; mUseShader = false; } // Preview option if (mFont.atlas != null) { GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Preview", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)); } GUILayout.EndHorizontal(); } }
override public void OnInspectorGUI() { mFont = target as UIFont; EditorGUIUtility.LookLikeControls(80f); NGUIEditorTools.DrawSeparator(); if (mFont.replacement != null) { mType = FontType.Reference; mReplacement = mFont.replacement; } FontType after = (FontType)EditorGUILayout.EnumPopup("Font Type", mType); if (mType != after) { if (after == FontType.Normal) { OnSelectFont(null); } else { mType = FontType.Reference; } } if (mType == FontType.Reference) { ComponentSelector.Draw <UIFont>(mFont.replacement, OnSelectFont); NGUIEditorTools.DrawSeparator(); GUILayout.Label("You can have one font simply point to\n" + "another one. This is useful if you want to be\n" + "able to quickly replace the contents of one\n" + "font with another one, for example for\n" + "swapping an SD font with an HD one, or\n" + "replacing an English font with a Chinese\n" + "one. All the labels referencing this font\n" + "will update their references to the new one."); if (mReplacement != mFont && mFont.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Font Change", mFont); mFont.replacement = mReplacement; UnityEditor.EditorUtility.SetDirty(mFont); } return; } NGUIEditorTools.DrawSeparator(); ComponentSelector.Draw <UIAtlas>(mFont.atlas, OnSelectAtlas); if (mFont.atlas != null) { if (mFont.bmFont.LegacyCheck()) { Debug.Log(mFont.name + " uses a legacy font data structure. Upgrading, please save."); EditorUtility.SetDirty(mFont); } if (mFont.bmFont.isValid) { NGUIEditorTools.AdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false); } } else { // No atlas specified -- set the material and texture rectangle directly Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } } bool resetWidthHeight = false; if (mFont.atlas != null || mFont.material != null) { TextAsset data = EditorGUILayout.ObjectField("Import Font", null, typeof(TextAsset), false) as TextAsset; if (data != null) { NGUIEditorTools.RegisterUndo("Import Font Data", mFont); BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes); mFont.MarkAsDirty(); resetWidthHeight = true; Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters"); } } if (mFont.bmFont.isValid) { Color green = new Color(0.4f, 1f, 0f, 1f); Texture2D tex = mFont.texture; if (tex != null) { if (mFont.atlas == null) { // Pixels are easier to work with than UVs Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false); // Automatically set the width and height of the rectangle to be the original font texture's dimensions if (resetWidthHeight) { pixels.width = mFont.texWidth; pixels.height = mFont.texHeight; } // Font sprite rectangle GUI.backgroundColor = green; pixels = EditorGUILayout.RectField("Pixel Rect", pixels); GUI.backgroundColor = Color.white; // Create a button that can make the coordinates pixel-perfect on click GUILayout.BeginHorizontal(); { GUILayout.Label("Correction", GUILayout.Width(75f)); Rect corrected = NGUIMath.MakePixelPerfect(pixels); if (corrected == pixels) { GUI.color = Color.grey; GUILayout.Button("Make Pixel-Perfect"); GUI.color = Color.white; } else if (GUILayout.Button("Make Pixel-Perfect")) { pixels = corrected; GUI.changed = true; } } GUILayout.EndHorizontal(); // Convert the pixel coordinates back to UV coordinates Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height); if (mFont.uvRect != uvRect) { NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont); mFont.uvRect = uvRect; } } // Font spacing GUILayout.BeginHorizontal(); { EditorGUIUtility.LookLikeControls(0f); GUILayout.Label("Spacing", GUILayout.Width(60f)); GUILayout.Label("X", GUILayout.Width(12f)); int x = EditorGUILayout.IntField(mFont.horizontalSpacing); GUILayout.Label("Y", GUILayout.Width(12f)); int y = EditorGUILayout.IntField(mFont.verticalSpacing); EditorGUIUtility.LookLikeControls(80f); if (mFont.horizontalSpacing != x || mFont.verticalSpacing != y) { NGUIEditorTools.RegisterUndo("Font Spacing", mFont); mFont.horizontalSpacing = x; mFont.verticalSpacing = y; } } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Preview", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)); } GUILayout.EndHorizontal(); } } }