public override void DrawEditor() { fontSize = Mathf.Clamp(EditorGUILayout.IntField("Font Size", fontSize), 1, 500); fontRenderingMode = (FontRenderingMode)EditorGUILayout.EnumPopup("Rendering Mode", fontRenderingMode); fontTextureCase = (FontTextureCase)EditorGUILayout.EnumPopup("Character", fontTextureCase); if (fontTextureCase == FontTextureCase.Dynamic) { includeFontData = EditorGUILayout.Toggle("Incl. Font Data", includeFontData); } else if (fontTextureCase == FontTextureCase.CustomSet) { EditorGUILayout.PrefixLabel("Custom Chars"); EditorGUI.BeginChangeCheck(); customCharacters = EditorGUILayout.TextArea(customCharacters, GUILayout.MinHeight(32f)); if (EditorGUI.EndChangeCheck()) { customCharacters = new string(customCharacters.Distinct().ToArray()).Replace("\n", string.Empty).Replace("\r", string.Empty); } } }
/// <summary> /// Create Soft Font, Soft Font Material, Soft Font Texture from SoftEffect Settings, /// </summary> /// <param name="font"></param> private void CreateSoftFont(Font font, bool createNewFolder) // http://answers.unity3d.com/questions/485695/truetypefontimportergenerateeditablefont-does-not.html { // Mkey.Utils.Measure("<<<<<<<<<<<<Summary CreateSoftFontTexture>>>>>>>>>>>>>>>: ", () => { gpuWorker = new GPUWorker(eShader); string dirPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(font)); //1) load source font asset string path = AssetDatabase.GetAssetPath(font); Font f = font; if (SoftEffects.debuglog) { Debug.Log("Path to Source font: " + path); } font = (Font)AssetDatabase.LoadMainAssetAtPath(path); if (f && !font) { Debug.LogError("Can't use embedded font : " + f.name); return; } //2) Remove old Editable font if (SoftFont && !createNewFolder) { if (SoftEffects.debuglog) { Debug.Log("EditableFont folder: " + Path.GetDirectoryName(AssetDatabase.GetAssetPath(SoftFont))); } if (SoftEffects.debuglog) { Debug.Log("Remove old EditableFont: " + SoftFont.name); } if (SoftFont.material) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(SoftFont.material.mainTexture)); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(SoftFont.material)); } AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(SoftFont)); AssetDatabase.Refresh(); } //3) reimport source font as editable TrueTypeFontImporter fontImporter = AssetImporter.GetAtPath(path) as TrueTypeFontImporter; //source settings int sourceSize = fontImporter.fontSize; int sourcePadding = fontImporter.characterPadding; int sourceSpacing = fontImporter.characterSpacing; FontTextureCase sourceCase = fontImporter.fontTextureCase; string chars = fontImporter.customCharacters; fontImporter.fontSize = GetComponent <Text>().fontSize; fontImporter.characterPadding = Mathf.Clamp(faceOptions.extPixels, 1, 100); fontImporter.characterSpacing = 0; // Mkey.Utils.Measure("Summary PreCreateFontTexture: ", () => { // Mkey.Utils.Measure("Reimport font: ", () => { if (SoftFontTextureCase == FontTextureCase.CustomSet) { if (customCharacters.Length == 0 || customCharacters == " ") { Debug.LogError("Custom Characters string is empty. Set default string."); customCharacters = GetComponent <Text>().text; if (customCharacters.Length == 0 || customCharacters == " ") { customCharacters = "New txt"; } } fontImporter.customCharacters = customCharacters; } else if (SoftFontTextureCase == FontTextureCase.Dynamic) { SoftFontTextureCase = FontTextureCase.ASCII; } fontImporter.fontTextureCase = SoftFontTextureCase; fontImporter.SaveAndReimport(); // }); // Mkey.Utils.Measure("GenerateEditableFont: ", () => { SoftFont = fontImporter.GenerateEditableFont(path); // }); int maxSize = Mathf.Max(font.material.mainTexture.width, font.material.mainTexture.height); // Mkey.Utils.Measure("RenameAsset: ", () => { AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(SoftFont), font.name + key); AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(SoftFont.material), font.name + key + "_edit"); AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(SoftFont.material.mainTexture), font.name + key); AssetDatabase.Refresh(); // }); Shader softShader = Shader.Find("SoftEffects/SoftEditShader"); SoftFont.material.shader = softShader; SoftMaterial = SoftFont.material; if (SoftEffects.debuglog) { Debug.Log("Editable texture size: " + SoftFont.material.mainTexture.width + " x " + SoftFont.material.mainTexture.height); } // Mkey.Utils.Measure("Reimport texture: ", () => { //5) Reimport EditableFont texture as readable SoftFont.material.mainTexture.ReimportTexture(true, maxSize); if (SoftEffects.debuglog) { Debug.Log("Editable texture size after reimport: " + SoftFont.material.mainTexture.width + " x " + SoftFont.material.mainTexture.height); } // }); // }); //5) Generate new Texture for editable font // Mkey.Utils.Measure("faceOptions.RenderFontTexture: ", () => { faceOptions.RenderFontTexture(gpuWorker, SoftFont, cb); //}); // Mkey.Utils.Measure("AfterCreateFontTexture: ", () => { faceOptions.CreateTextureFromRender_ARGB32(true, dirPath + "/" + font.name + key + "_edit" + ".png"); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(SoftFont.material.mainTexture)); // Remove old texture Texture2D t = (Texture2D)AssetDatabase.LoadMainAssetAtPath(dirPath + "/" + font.name + key + "_edit" + ".png"); // load new texture asset t.ReimportTexture(true); //6 extend verts and uvs // SoftFont.ExtendVertsAndUvs(faceOptions.extPixels); faceOptions.mainTexture = t; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // 9) remove editable font to unique folder string targetFolder = AssetDatabase.GUIDToAssetPath(FolderGUID); string fontPath = AssetDatabase.GetAssetPath(SoftFont); string materialPath = AssetDatabase.GetAssetPath(SoftFont.material); string texturePath = AssetDatabase.GetAssetPath(t); if (SoftEffects.debuglog) { Debug.Log("Move file: " + fontPath + " to : " + targetFolder + "/" + Path.GetFileName(fontPath)); } AssetDatabase.MoveAsset(fontPath, targetFolder + "/" + Path.GetFileName(fontPath));// FileUtil.MoveFileOrDirectory(fontPath, targetFolder + "/"+ Path.GetFileName(fontPath)); AssetDatabase.MoveAsset(materialPath, targetFolder + "/" + Path.GetFileName(materialPath)); AssetDatabase.MoveAsset(texturePath, targetFolder + "/" + Path.GetFileName(texturePath)); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); faceOptions.IsCombinedDirty = true; RenderNewTextures(gpuWorker, true); /**/ EditorGUIUtility.PingObject(SoftFont); EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene()); //revert source settings fontImporter = AssetImporter.GetAtPath(path) as TrueTypeFontImporter; fontImporter.fontSize = sourceSize; fontImporter.fontTextureCase = sourceCase; // fontImporter.characterPadding = sourcePadding; // fontImporter.characterSpacing = sourceSpacing; fontImporter.characterPadding = 1; fontImporter.characterSpacing = 0; if (sourceCase == FontTextureCase.CustomSet) { fontImporter.customCharacters = chars; } fontImporter.SaveAndReimport(); // }); // }); }
// レイアウトを描画する private void OnGUI() { string tPath = null; string[] tId = AssetDatabase.FindAssets("FontFilter"); if (tId != null) { int i, l = tId.Length; for (i = 0; i < l; i++) { tPath = AssetDatabase.GUIDToAssetPath(tId[i]); if (Directory.Exists(tPath) == true) { break; } } if (i >= l) { tPath = null; } } if (string.IsNullOrEmpty(tPath) == true) { EditorGUILayout.HelpBox("状態が異常です", MessageType.Warning); return; } tPath = tPath + "/Resources/uGUIHelper"; if (Directory.Exists(tPath) == false) { EditorGUILayout.HelpBox("保存フォルダが存在しません", MessageType.Warning); return; } FontFilter tFF = null; tPath = tPath + "/FontFilter.asset"; if (File.Exists(tPath) == false) { // ファイルが存在しない tFF = ScriptableObject.CreateInstance <FontFilter>(); tFF.name = "FontFilter"; AssetDatabase.CreateAsset(tFF, tPath); AssetDatabase.Refresh(); } else { // ファイルが存在する tFF = AssetDatabase.LoadAssetAtPath <FontFilter>(tPath); } Selection.activeObject = tFF; //---------------------------------------------------------- bool tDirty = false; // フォント Font tFont = EditorGUILayout.ObjectField("Font", m_Font, typeof(Font), false) as Font; if (tFont != m_Font) { m_Font = tFont; } if (m_Font != null) { if (GUILayout.Button("Create Or Update") == true) { tPath = AssetDatabase.GetAssetPath(m_Font.GetInstanceID()); TrueTypeFontImporter tFontData = ( TrueTypeFontImporter )AssetImporter.GetAtPath(tPath); FontTextureCase tOldFontTextureCase = tFontData.fontTextureCase; if (tFontData.fontTextureCase != FontTextureCase.Unicode) { tFontData.fontTextureCase = FontTextureCase.Unicode; tFontData.SaveAndReimport(); } //-------------------------------------------------------------------- if (tFF.flag == null || tFF.flag.Length < 8192) { tFF.flag = new byte[8192]; } int v = 0; byte f; CharacterInfo tCI; bool e; char c; int i, j, l = 65536; for (i = 0; i < l; i = i + 8) { f = 0; for (j = 0; j < 8; j++) { c = ( char )(i + j); e = true; if (c != ' ' && c != ' ') { if (m_Font.HasCharacter(c) == true) { if (m_Font.GetCharacterInfo(c, out tCI, 16) == true) { if (tCI.advance <= 0) { e = false; } } else { e = false; } } else { e = false; } } if (e == true) { f = ( byte )(f | (1 << j)); v++; } } tFF.flag[i >> 3] = f; } //-------------------------------------------------------------------- if (tOldFontTextureCase != FontTextureCase.Unicode) { tFontData.fontTextureCase = tOldFontTextureCase; tFontData.SaveAndReimport(); } //-------------------------------------------------------------------- tDirty = true; EditorUtility.DisplayDialog("Font Filter", "Completed !! -> " + v, "OK"); } } //---------------------------------- // 更新判定 if (tDirty == true) { EditorUtility.SetDirty(tFF); // 更新実行 // AssetDatabase.Refresh() ; } }