static void ForceFontAssetUpgrade(MenuCommand command) { TMP_FontAsset fontAsset = command.context as TMP_FontAsset; if (fontAsset != null) { fontAsset.UpgradeFontAsset(); TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); } }
static void ClearFontAssetData(MenuCommand command) { TMP_FontAsset fontAsset = command.context as TMP_FontAsset; if (fontAsset != null && Selection.activeObject != fontAsset) { Selection.activeObject = fontAsset; } fontAsset.ClearFontAssetData(true); TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }
public override void OnInspectorGUI() { //Debug.Log("OnInspectorGUI Called."); Event evt = Event.current; serializedObject.Update(); GUILayout.Label("<b>TextMesh Pro! Font Asset</b>", TMP_UIStyleManager.Section_Label); // TextMeshPro Font Info Panel GUILayout.Label("Face Info", TMP_UIStyleManager.Section_Label); EditorGUI.indentLevel = 1; GUI.enabled = false; // Lock UI float labelWidth = EditorGUIUtility.labelWidth = 135f; float fieldWidth = EditorGUIUtility.fieldWidth; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Name"), new GUIContent("Font Source")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("PointSize")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("LineHeight")); //GUI.enabled = false; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Baseline")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Ascender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Descender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Underline")); //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("UnderlineThickness")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SuperscriptOffset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SubscriptOffset")); SerializedProperty subSize_prop = m_fontInfo_prop.FindPropertyRelative("SubSize"); EditorGUILayout.PropertyField(subSize_prop); subSize_prop.floatValue = Mathf.Clamp(subSize_prop.floatValue, 0.25f, 1f); GUI.enabled = false; //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); //GUILayout.Label("Atlas Size"); EditorGUI.indentLevel = 1; GUILayout.Space(18); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasWidth"), new GUIContent("Width")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasHeight"), new GUIContent("Height")); GUI.enabled = true; EditorGUI.indentLevel = 0; GUILayout.Space(20); GUILayout.Label("Font Sub-Assets", TMP_UIStyleManager.Section_Label); GUI.enabled = false; EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas:")); EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material:")); GUI.enabled = true; // Font SETTINGS GUILayout.Space(10); GUILayout.Label("Face Style", TMP_UIStyleManager.Section_Label); string evt_cmd = Event.current.commandName; // Get Current Event CommandName to check for Undo Events EditorGUILayout.BeginHorizontal(); EditorGUIUtility.labelWidth = 110f; EditorGUIUtility.fieldWidth = 30f; EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal Weight")); font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; Material mat = font_material_prop.objectReferenceValue as Material; mat.SetFloat("_WeightNormal", font_normalStyle_prop.floatValue); } EditorGUIUtility.labelWidth = 90f; EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold Weight")); font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; Material mat = font_material_prop.objectReferenceValue as Material; mat.SetFloat("_WeightBold", font_boldStyle_prop.floatValue); } EditorGUIUtility.labelWidth = 100f; EditorGUILayout.PropertyField(font_boldSpacing_prop, new GUIContent("Bold Spacing")); font_boldSpacing_prop.floatValue = Mathf.Clamp(font_boldSpacing_prop.floatValue, 0, 100); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; } EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.fieldWidth = fieldWidth; EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style: ")); font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60); EditorGUILayout.PropertyField(font_tabSize_prop, new GUIContent("Tab Multiple: ")); EditorGUILayout.EndHorizontal(); GUILayout.Space(10); EditorGUI.indentLevel = 0; if (GUILayout.Button("Glyph Info \t\t\t" + (UI_PanelState.glyphInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.glyphInfoPanel = !UI_PanelState.glyphInfoPanel; } if (UI_PanelState.glyphInfoPanel) { int arraySize = m_glyphInfoList_prop.arraySize; int itemsPerPage = 15; if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_GlyphPage; i < arraySize && i < itemsPerPage * (m_GlyphPage + 1); i++) { SerializedProperty glyphInfo = m_glyphInfoList_prop.GetArrayElementAtIndex(i); EditorGUILayout.BeginVertical(TMP_UIStyleManager.Group_Label); EditorGUILayout.PropertyField(glyphInfo); EditorGUILayout.EndVertical(); } } Rect pagePos = EditorGUILayout.GetControlRect(false, 20); pagePos.width /= 2; int shiftMultiplier = evt.shift ? 10 : 1; if (m_GlyphPage > 0) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Previous Page")) { m_GlyphPage -= 1 * shiftMultiplier; } pagePos.x += pagePos.width; if (itemsPerPage * (m_GlyphPage + 1) < arraySize) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Next Page")) { m_GlyphPage += 1 * shiftMultiplier; } m_GlyphPage = Mathf.Clamp(m_GlyphPage, 0, arraySize / itemsPerPage); } // KERNING TABLE PANEL if (GUILayout.Button("Kerning Table Info\t\t\t" + (UI_PanelState.kerningInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.kerningInfoPanel = !UI_PanelState.kerningInfoPanel; } if (UI_PanelState.kerningInfoPanel) { Rect pos; SerializedProperty kerningPairs_prop = m_kerningInfo_prop.FindPropertyRelative("kerningPairs"); // int pairCount = kerningPairs_prop.arraySize; EditorGUILayout.BeginHorizontal(); GUILayout.Label("Left Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Right Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Offset Value", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label(GUIContent.none, GUILayout.Width(20)); EditorGUILayout.EndHorizontal(); GUILayout.BeginVertical(TMP_UIStyleManager.TMP_GUISkin.label); int arraySize = kerningPairs_prop.arraySize; int itemsPerPage = 25; if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_KerningPage; i < arraySize && i < itemsPerPage * (m_KerningPage + 1); i++) { SerializedProperty kerningPair_prop = kerningPairs_prop.GetArrayElementAtIndex(i); pos = EditorGUILayout.BeginHorizontal(); EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), kerningPair_prop, GUIContent.none); // Button to Delete Kerning Pair if (GUILayout.Button("-", GUILayout.ExpandWidth(false))) { m_kerningTable.RemoveKerningPair(i); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; break; } EditorGUILayout.EndHorizontal(); } } Rect pagePos = EditorGUILayout.GetControlRect(false, 20); pagePos.width /= 3; int shiftMultiplier = evt.shift ? 10 : 1; // Previous Page if (m_KerningPage > 0) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Previous Page")) { m_KerningPage -= 1 * shiftMultiplier; } // Page Counter GUI.enabled = true; pagePos.x += pagePos.width; int totalPages = (int)(arraySize / (float)itemsPerPage + 0.999f); GUI.Label(pagePos, "Page " + (m_KerningPage + 1) + " / " + totalPages, GUI.skin.button); // Next Page pagePos.x += pagePos.width; if (itemsPerPage * (m_GlyphPage + 1) < arraySize) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Next Page")) { m_KerningPage += 1 * shiftMultiplier; } m_KerningPage = Mathf.Clamp(m_KerningPage, 0, arraySize / itemsPerPage); GUILayout.EndVertical(); GUILayout.Space(10); // Add New Kerning Pair Section GUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); pos = EditorGUILayout.BeginHorizontal(); // Draw Empty Kerning Pair EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), m_kerningPair_prop); GUILayout.Label(GUIContent.none, GUILayout.Height(19)); EditorGUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Add New Kerning Pair")) { int asci_left = m_kerningPair_prop.FindPropertyRelative("AscII_Left").intValue; int asci_right = m_kerningPair_prop.FindPropertyRelative("AscII_Right").intValue; float xOffset = m_kerningPair_prop.FindPropertyRelative("XadvanceOffset").floatValue; errorCode = m_kerningTable.AddKerningPair(asci_left, asci_right, xOffset); // Sort Kerning Pairs & Reload Font Asset if new kerning pair was added. if (errorCode != -1) { m_kerningTable.SortKerningPairs(); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; } else { timeStamp = System.DateTime.Now.AddSeconds(5); } } if (errorCode == -1) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.Label); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (System.DateTime.Now > timeStamp) { errorCode = 0; } } GUILayout.EndVertical(); } if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty) { //Debug.Log("Serialized properties have changed."); TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); isAssetDirty = false; EditorUtility.SetDirty(target); //TMPro_EditorUtility.RepaintAll(); // Consider SetDirty } }
// PRAGMA MARK - Public Interface public static void Bake(Font font, bool useAutoSizing, int fontSize, int characterPadding, TMPFontPackingModes fontPackingMode, int atlasWidth, int atlasHeight, FaceStyles fontStyle, int fontStyleMod, RenderModes fontRenderMode, string charactersToBake, string outputFilePath) { int errorCode = TMPro_FontPlugin.Initialize_FontEngine(); if (errorCode != 0 && errorCode != 99) // 99 means that engine was already initialized { Debug.LogWarning("Error Code: " + errorCode + " occurred while initializing TMPro_FontPlugin."); return; } string fontPath = AssetDatabase.GetAssetPath(font); errorCode = TMPro_FontPlugin.Load_TrueType_Font(fontPath); if (errorCode != 0 && errorCode != 99) // 99 means that font was already loaded { Debug.LogWarning("Error Code: " + errorCode + " occurred while loading font: " + font + "."); return; } if (useAutoSizing) { fontSize = 72; } errorCode = TMPro_FontPlugin.FT_Size_Font(fontSize); if (errorCode != 0) { Debug.LogWarning("Error Code: " + errorCode + " occurred while sizing font: " + font + " to size: " + fontSize + "."); return; } byte[] textureBuffer = new byte[atlasWidth * atlasHeight]; int[] characterArray = charactersToBake.Select(c => (int)c).ToArray(); int characterCount = charactersToBake.Length; var fontFaceInfo = new FT_FaceInfo(); var fontGlyphInfo = new FT_GlyphInfo[characterCount]; float strokeSize = fontStyleMod; if (fontRenderMode == RenderModes.DistanceField16) { strokeSize *= 16; } else if (fontRenderMode == RenderModes.DistanceField32) { strokeSize *= 32; } errorCode = TMPro_FontPlugin.Render_Characters(textureBuffer, atlasWidth, atlasHeight, characterPadding, characterArray, characterCount, fontStyle, strokeSize, useAutoSizing, fontRenderMode, (int)fontPackingMode, ref fontFaceInfo, fontGlyphInfo); if (errorCode != 0) { Debug.LogWarning("Error Code: " + errorCode + " occurred while rendering font characters!"); return; } string outputFilename = Path.GetFileNameWithoutExtension(outputFilePath); // check if font asset already exists TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath(outputFilePath, typeof(TMP_FontAsset)) as TMP_FontAsset; if (fontAsset == null) { fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(fontAsset, outputFilePath); } // Destroy Assets that will be replaced. UnityEngine.Object.DestroyImmediate(fontAsset.atlas, allowDestroyingAssets: true); fontAsset.fontAssetType = (fontRenderMode >= RenderModes.DistanceField16) ? TMP_FontAsset.FontAssetTypes.SDF : TMP_FontAsset.FontAssetTypes.Bitmap; fontAsset.AddFaceInfo(ConvertToFaceInfo(fontFaceInfo)); fontAsset.AddGlyphInfo(ConvertToGlyphs(fontGlyphInfo)); var fontTexture = CreateFontTexture(atlasWidth, atlasHeight, textureBuffer, fontRenderMode); fontTexture.name = outputFilename + " Atlas"; fontTexture.hideFlags = HideFlags.HideInHierarchy; fontAsset.atlas = fontTexture; AssetDatabase.AddObjectToAsset(fontTexture, fontAsset); // Find all Materials referencing this font atlas. Material[] materialReferences = TMP_EditorUtility.FindMaterialReferences(fontAsset).Where(m => m != null).ToArray(); if (materialReferences == null || materialReferences.Length <= 0) { // Create new Material and add it as Sub-Asset Shader shader = Shader.Find("TMPro/Distance Field"); Material fontMaterial = new Material(shader); fontMaterial.name = outputFilename + " Material"; fontAsset.material = fontMaterial; fontMaterial.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(fontMaterial, fontAsset); materialReferences = new Material[] { fontMaterial }; } foreach (var m in materialReferences) { m.SetTexture(ShaderUtilities.ID_MainTex, fontTexture); m.SetFloat(ShaderUtilities.ID_TextureWidth, fontTexture.width); m.SetFloat(ShaderUtilities.ID_TextureHeight, fontTexture.height); m.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); m.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); m.SetFloat(ShaderUtilities.ID_GradientScale, characterPadding + 1); } AssetDatabase.SaveAssets(); // Re-import font asset to get the new updated version. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); fontAsset.ReadFontDefinition(); AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }
public override void OnInspectorGUI() { //Debug.Log("OnInspectorGUI Called."); serializedObject.Update(); GUILayout.Label("<b>TextMesh Pro! Font Asset</b>", SectionLabel); // TextMeshPro Font Info Panel GUILayout.Label("Face Info", SectionLabel); EditorGUI.indentLevel = 1; GUI.enabled = false; // Lock UI EditorGUIUtility.labelWidth = 135; //EditorGUIUtility.fieldWidth = 80; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Name"), new GUIContent("Font Source")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("PointSize")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("LineHeight")); GUI.enabled = false; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Baseline")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Ascender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Descender")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Underline")); //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("UnderlineThickness")); GUI.enabled = false; //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); //GUILayout.Label("Atlas Size"); EditorGUI.indentLevel = 1; GUILayout.Space(18); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasWidth"), new GUIContent("Width")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasHeight"), new GUIContent("Height")); GUI.enabled = true; EditorGUI.indentLevel = 0; GUILayout.Space(20); GUILayout.Label("Font Sub-Assets", SectionLabel); GUI.enabled = false; EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas:")); EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material:")); GUI.enabled = true; // Font SETTINGS GUILayout.Space(10); GUILayout.Label("Face Style", SectionLabel); string evt_cmd = Event.current.commandName; // Get Current Event CommandName to check for Undo Events EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal weight")); font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; Material mat = font_material_prop.objectReferenceValue as Material; mat.SetFloat("_WeightNormal", font_normalStyle_prop.floatValue); } //Rect rect = EditorGUILayout.GetControlRect(); EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold weight")); font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; Material mat = font_material_prop.objectReferenceValue as Material; mat.SetFloat("_WeightBold", font_boldStyle_prop.floatValue); } EditorGUILayout.EndHorizontal(); EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style: ")); font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60); GUILayout.Space(10); EditorGUI.indentLevel = 0; if (GUILayout.Button("Glyph Info \t\t\t" + (UI_PanelState.glyphInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), SectionLabel)) { UI_PanelState.glyphInfoPanel = !UI_PanelState.glyphInfoPanel; } if (UI_PanelState.glyphInfoPanel) { if (m_glyphInfoList_prop.arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = 0; i < m_glyphInfoList_prop.arraySize; i++) { SerializedProperty glyphInfo = m_glyphInfoList_prop.GetArrayElementAtIndex(i); EditorGUILayout.BeginVertical(GroupLabel); EditorGUILayout.PropertyField(glyphInfo); EditorGUILayout.EndVertical(); } } } // KERNING TABLE PANEL if (GUILayout.Button("Kerning Table Info\t\t\t" + (UI_PanelState.kerningInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), SectionLabel)) { UI_PanelState.kerningInfoPanel = !UI_PanelState.kerningInfoPanel; } if (UI_PanelState.kerningInfoPanel) { Rect pos; SerializedProperty kerningPairs_prop = m_kerningInfo_prop.FindPropertyRelative("kerningPairs"); int pairCount = kerningPairs_prop.arraySize; EditorGUILayout.BeginHorizontal(); GUILayout.Label("Left Char", mySkin.label); GUILayout.Label("Right Char", mySkin.label); GUILayout.Label("Offset Value", mySkin.label); GUILayout.Label(GUIContent.none, GUILayout.Width(20)); EditorGUILayout.EndHorizontal(); GUILayout.BeginVertical(mySkin.label); for (int i = 0; i < pairCount; i++) { SerializedProperty kerningPair_prop = kerningPairs_prop.GetArrayElementAtIndex(i); pos = EditorGUILayout.BeginHorizontal(); EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), kerningPair_prop, GUIContent.none); // Button to Delete Kerning Pair if (GUILayout.Button("-", GUILayout.ExpandWidth(false))) { m_kerningTable.RemoveKerningPair(i); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; break; } EditorGUILayout.EndHorizontal(); } GUILayout.EndVertical(); GUILayout.Space(10); // Add New Kerning Pair Section GUILayout.BeginVertical(SquareAreaBox85G); pos = EditorGUILayout.BeginHorizontal(); // Draw Empty Kerning Pair EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), m_kerningPair_prop); GUILayout.Label(GUIContent.none, GUILayout.Height(19)); EditorGUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Add New Kerning Pair")) { int asci_left = m_kerningPair_prop.FindPropertyRelative("AscII_Left").intValue; int asci_right = m_kerningPair_prop.FindPropertyRelative("AscII_Right").intValue; float xOffset = m_kerningPair_prop.FindPropertyRelative("XadvanceOffset").floatValue; errorCode = m_kerningTable.AddKerningPair(asci_left, asci_right, xOffset); // Sort Kerning Pairs & Reload Font Asset if new kerpair was added. if (errorCode != -1) { m_kerningTable.SortKerningPairs(); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; } else { timeStamp = System.DateTime.Now.AddSeconds(5); } } if (errorCode == -1) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", mySkin.label); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (System.DateTime.Now > timeStamp) { errorCode = 0; } } GUILayout.EndVertical(); } if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty) { //Debug.Log("Serialized properties have changed."); TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); isAssetDirty = false; TMPro_EditorUtility.RepaintAll(); // Consider SetDirty } }
public override void OnInspectorGUI() { Event current = Event.current; base.serializedObject.Update(); GUILayout.Label("<b>TextMesh Pro! Font Asset</b>", TMP_UIStyleManager.Section_Label); GUILayout.Label("Face Info", TMP_UIStyleManager.Section_Label); EditorGUI.indentLevel = 1; GUI.enabled = false; float labelWidth = EditorGUIUtility.labelWidth = 150f; float fieldWidth = EditorGUIUtility.fieldWidth; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Name"), new GUIContent("Font Source")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("PointSize")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Scale")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("LineHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Ascender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("CapHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Baseline")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Descender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Underline"), new GUIContent("Underline Offset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("strikethrough"), new GUIContent("Strikethrough Offset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SuperscriptOffset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SubscriptOffset")); SerializedProperty serializedProperty = m_fontInfo_prop.FindPropertyRelative("SubSize"); EditorGUILayout.PropertyField(serializedProperty, new GUIContent("Super / Subscript Size")); serializedProperty.floatValue = Mathf.Clamp(serializedProperty.floatValue, 0.25f, 1f); GUI.enabled = false; EditorGUI.indentLevel = 1; GUILayout.Space(18f); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasWidth"), new GUIContent("Width")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasHeight"), new GUIContent("Height")); GUI.enabled = true; EditorGUI.indentLevel = 0; GUILayout.Space(20f); GUILayout.Label("Font Sub-Assets", TMP_UIStyleManager.Section_Label); GUI.enabled = false; EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas:")); EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material:")); GUI.enabled = true; string commandName = Event.current.commandName; EditorGUI.indentLevel = 0; if (GUILayout.Button("Font Weights\t" + (UI_PanelState.fontWeightPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.fontWeightPanel = !UI_PanelState.fontWeightPanel; } if (UI_PanelState.fontWeightPanel) { EditorGUIUtility.labelWidth = 120f; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); EditorGUI.indentLevel = 0; GUILayout.Label("Select the Font Assets that will be used for the following font weights.", TMP_UIStyleManager.Label); GUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); GUILayout.Label("<b>Font Weight</b>", TMP_UIStyleManager.Label, GUILayout.Width(117f)); GUILayout.Label("<b>Normal Style</b>", TMP_UIStyleManager.Label); GUILayout.Label("<b>Italic Style</b>", TMP_UIStyleManager.Label); EditorGUILayout.EndHorizontal(); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(4), new GUIContent("400 - Regular")); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(7), new GUIContent("700 - Bold")); EditorGUILayout.EndVertical(); EditorGUIUtility.labelWidth = 120f; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); GUILayout.Label("Settings used to simulate a typeface when no font asset is available.", TMP_UIStyleManager.Label); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal Weight")); font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3f, 3f); if (GUI.changed || commandName == "UndoRedoPerformed") { GUI.changed = false; for (int i = 0; i < m_materialPresets.Length; i++) { m_materialPresets[i].SetFloat("_WeightNormal", font_normalStyle_prop.floatValue); } } EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold Weight"), GUILayout.MinWidth(100f)); font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3f, 3f); if (GUI.changed || commandName == "UndoRedoPerformed") { GUI.changed = false; for (int j = 0; j < m_materialPresets.Length; j++) { m_materialPresets[j].SetFloat("_WeightBold", font_boldStyle_prop.floatValue); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalSpacing_prop, new GUIContent("Spacing Offset")); font_normalSpacing_prop.floatValue = Mathf.Clamp(font_normalSpacing_prop.floatValue, -100f, 100f); if (GUI.changed || commandName == "UndoRedoPerformed") { GUI.changed = false; } EditorGUILayout.PropertyField(font_boldSpacing_prop, new GUIContent("Bold Spacing")); font_boldSpacing_prop.floatValue = Mathf.Clamp(font_boldSpacing_prop.floatValue, 0f, 100f); if (GUI.changed || commandName == "UndoRedoPerformed") { GUI.changed = false; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style: ")); font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60); EditorGUILayout.PropertyField(font_tabSize_prop, new GUIContent("Tab Multiple: ")); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } GUILayout.Space(5f); EditorGUI.indentLevel = 0; if (GUILayout.Button("Fallback Font Assets\t" + (UI_PanelState.fallbackFontAssetPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.fallbackFontAssetPanel = !UI_PanelState.fallbackFontAssetPanel; } if (UI_PanelState.fallbackFontAssetPanel) { EditorGUIUtility.labelWidth = 120f; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); EditorGUI.indentLevel = 0; GUILayout.Label("Select the Font Assets that will be searched and used as fallback when characters are missing from this font asset.", TMP_UIStyleManager.Label); GUILayout.Space(10f); m_list.DoLayoutList(); EditorGUILayout.EndVertical(); } EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.fieldWidth = fieldWidth; GUILayout.Space(5f); EditorGUI.indentLevel = 0; if (GUILayout.Button("Glyph Info\t" + (UI_PanelState.glyphInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.glyphInfoPanel = !UI_PanelState.glyphInfoPanel; } if (UI_PanelState.glyphInfoPanel) { int num2 = m_glyphInfoList_prop.arraySize; int num3 = 15; EditorGUILayout.BeginVertical(TMP_UIStyleManager.Group_Label, GUILayout.ExpandWidth(true)); EditorGUILayout.BeginHorizontal(); EditorGUIUtility.labelWidth = 110f; EditorGUI.BeginChangeCheck(); string text = EditorGUILayout.TextField("Glyph Search", m_searchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_isSearchDirty) { if (!string.IsNullOrEmpty(text)) { m_searchPattern = text; SearchGlyphTable(m_searchPattern, ref m_searchList); } m_isSearchDirty = false; } string str = string.IsNullOrEmpty(m_searchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, str)) { GUIUtility.keyboardControl = 0; m_searchPattern = string.Empty; } EditorGUILayout.EndHorizontal(); if (!string.IsNullOrEmpty(m_searchPattern)) { num2 = m_searchList.Count; } DisplayGlyphPageNavigation(num2, num3); EditorGUILayout.EndVertical(); if (num2 > 0) { for (int k = num3 * m_GlyphPage; k < num2 && k < num3 * (m_GlyphPage + 1); k++) { Rect rect = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int num4 = k; if (!string.IsNullOrEmpty(m_searchPattern)) { num4 = m_searchList[k]; } SerializedProperty arrayElementAtIndex = m_glyphInfoList_prop.GetArrayElementAtIndex(num4); EditorGUI.BeginDisabledGroup(k != m_selectedElement); EditorGUILayout.BeginVertical(TMP_UIStyleManager.Group_Label); EditorGUILayout.PropertyField(arrayElementAtIndex); EditorGUILayout.EndVertical(); EditorGUI.EndDisabledGroup(); Rect rect2 = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); Rect rect3 = new Rect(rect.x, rect.y, rect2.width, rect2.y - rect.y); if (DoSelectionCheck(rect3)) { m_selectedElement = k; m_AddGlyphWarning.isEnabled = false; m_unicodeHexLabel = "<i>Unicode Hex ID</i>"; GUIUtility.keyboardControl = 0; } if (m_selectedElement != k) { continue; } TMP_EditorUtility.DrawBox(rect3, 2f, new Color32(40, 192, byte.MaxValue, byte.MaxValue)); Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); float num5 = controlRect.width * 0.6f; float num6 = num5 / 3f; Rect position = new Rect(controlRect.x + controlRect.width * 0.4f, controlRect.y, num6, controlRect.height); GUI.enabled = !string.IsNullOrEmpty(m_dstGlyphID); if (GUI.Button(position, new GUIContent("Copy to"))) { GUIUtility.keyboardControl = 0; int dstGlyphID = TMP_TextUtilities.StringToInt(m_dstGlyphID); if (!AddNewGlyph(num4, dstGlyphID)) { m_AddGlyphWarning.isEnabled = true; m_AddGlyphWarning.expirationTime = EditorApplication.timeSinceStartup + 1.0; } m_dstGlyphID = string.Empty; m_isSearchDirty = true; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); } GUI.enabled = true; position.x += num6; GUI.SetNextControlName("GlyphID_Input"); m_dstGlyphID = EditorGUI.TextField(position, m_dstGlyphID); EditorGUI.LabelField(position, new GUIContent(m_unicodeHexLabel, "The Unicode (Hex) ID of the duplicated Glyph"), TMP_UIStyleManager.Label); if (GUI.GetNameOfFocusedControl() == "GlyphID_Input") { m_unicodeHexLabel = string.Empty; char character = Event.current.character; if ((character < '0' || character > '9') && (character < 'a' || character > 'f') && (character < 'A' || character > 'F')) { Event.current.character = '\0'; } } else { m_unicodeHexLabel = "<i>Unicode Hex ID</i>"; } position.x += num6; if (GUI.Button(position, "Remove")) { GUIUtility.keyboardControl = 0; RemoveGlyphFromList(num4); m_selectedElement = -1; m_isSearchDirty = true; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); return; } if (m_AddGlyphWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddGlyphWarning.expirationTime) { EditorGUILayout.HelpBox("The Destination Glyph ID already exists", MessageType.Warning); } } } DisplayGlyphPageNavigation(num2, num3); } GUILayout.Space(5f); if (GUILayout.Button("Kerning Table Info\t" + (UI_PanelState.kerningInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.kerningInfoPanel = !UI_PanelState.kerningInfoPanel; } if (UI_PanelState.kerningInfoPanel) { SerializedProperty serializedProperty2 = m_kerningInfo_prop.FindPropertyRelative("kerningPairs"); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Left Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Right Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Offset Value", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label(GUIContent.none, GUILayout.Width(20f)); EditorGUILayout.EndHorizontal(); GUILayout.BeginVertical(TMP_UIStyleManager.TMP_GUISkin.label); int arraySize = serializedProperty2.arraySize; int num7 = 25; Rect rect4; if (arraySize > 0) { for (int l = num7 * m_KerningPage; l < arraySize && l < num7 * (m_KerningPage + 1); l++) { SerializedProperty arrayElementAtIndex2 = serializedProperty2.GetArrayElementAtIndex(l); rect4 = EditorGUILayout.BeginHorizontal(); EditorGUI.PropertyField(new Rect(rect4.x, rect4.y, rect4.width - 20f, rect4.height), arrayElementAtIndex2, GUIContent.none); if (GUILayout.Button("-", GUILayout.ExpandWidth(false))) { m_kerningTable.RemoveKerningPair(l); m_fontAsset.ReadFontDefinition(); base.serializedObject.Update(); isAssetDirty = true; break; } EditorGUILayout.EndHorizontal(); } } Rect controlRect2 = EditorGUILayout.GetControlRect(false, 20f); controlRect2.width /= 3f; int num8 = (!current.shift) ? 1 : 10; if (m_KerningPage > 0) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(controlRect2, "Previous Page")) { m_KerningPage -= num8; } GUI.enabled = true; controlRect2.x += controlRect2.width; int num9 = (int)((float)arraySize / (float)num7 + 0.999f); GUI.Label(controlRect2, "Page " + (m_KerningPage + 1) + " / " + num9, GUI.skin.button); controlRect2.x += controlRect2.width; if (num7 * (m_GlyphPage + 1) < arraySize) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(controlRect2, "Next Page")) { m_KerningPage += num8; } m_KerningPage = Mathf.Clamp(m_KerningPage, 0, arraySize / num7); GUILayout.EndVertical(); GUILayout.Space(10f); GUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); rect4 = EditorGUILayout.BeginHorizontal(); EditorGUI.PropertyField(new Rect(rect4.x, rect4.y, rect4.width - 20f, rect4.height), m_kerningPair_prop); GUILayout.Label(GUIContent.none, GUILayout.Height(19f)); EditorGUILayout.EndHorizontal(); GUILayout.Space(5f); if (GUILayout.Button("Add New Kerning Pair")) { int intValue = m_kerningPair_prop.FindPropertyRelative("AscII_Left").intValue; int intValue2 = m_kerningPair_prop.FindPropertyRelative("AscII_Right").intValue; float floatValue = m_kerningPair_prop.FindPropertyRelative("XadvanceOffset").floatValue; errorCode = m_kerningTable.AddKerningPair(intValue, intValue2, floatValue); if (errorCode != -1) { m_kerningTable.SortKerningPairs(); m_fontAsset.ReadFontDefinition(); base.serializedObject.Update(); isAssetDirty = true; } else { timeStamp = DateTime.Now.AddSeconds(5.0); } } if (errorCode == -1) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.Label); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (DateTime.Now > timeStamp) { errorCode = 0; } } GUILayout.EndVertical(); } if (base.serializedObject.ApplyModifiedProperties() || commandName == "UndoRedoPerformed" || isAssetDirty) { TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); isAssetDirty = false; EditorUtility.SetDirty(base.target); } GUI.enabled = true; if (current.type == EventType.MouseDown && current.button == 0) { m_selectedElement = -1; } }
public override void OnInspectorGUI() { // Check Warnings //Debug.Log("OnInspectorGUI Called."); Event currentEvent = Event.current; serializedObject.Update(); GUILayout.Label("<b>TextMesh Pro! Font Asset</b>", TMP_UIStyleManager.Section_Label); // TextMeshPro Font Info Panel GUILayout.Label("Face Info", TMP_UIStyleManager.Section_Label); EditorGUI.indentLevel = 1; GUI.enabled = false; // Lock UI float labelWidth = EditorGUIUtility.labelWidth = 150f; float fieldWidth = EditorGUIUtility.fieldWidth; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Name"), new GUIContent("Font Source")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("PointSize")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Scale")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("LineHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Ascender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("CapHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Baseline")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Descender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Underline")); //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("UnderlineThickness")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SuperscriptOffset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SubscriptOffset")); SerializedProperty subSize_prop = m_fontInfo_prop.FindPropertyRelative("SubSize"); EditorGUILayout.PropertyField(subSize_prop, new GUIContent("Super / Subscript Size")); subSize_prop.floatValue = Mathf.Clamp(subSize_prop.floatValue, 0.25f, 1f); GUI.enabled = false; //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); //GUILayout.Label("Atlas Size"); EditorGUI.indentLevel = 1; GUILayout.Space(18); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasWidth"), new GUIContent("Width")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasHeight"), new GUIContent("Height")); GUI.enabled = true; EditorGUI.indentLevel = 0; GUILayout.Space(20); GUILayout.Label("Font Sub-Assets", TMP_UIStyleManager.Section_Label); GUI.enabled = false; EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas:")); EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material:")); GUI.enabled = true; string evt_cmd = Event.current.commandName; // Get Current Event CommandName to check for Undo Events // FONT SETTINGS EditorGUI.indentLevel = 0; if (GUILayout.Button("Font Weights\t" + (UI_PanelState.fontWeightPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.fontWeightPanel = !UI_PanelState.fontWeightPanel; } if (UI_PanelState.fontWeightPanel) { EditorGUIUtility.labelWidth = 120; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); EditorGUI.indentLevel = 0; GUILayout.Label("Select the Font Assets that will be used for the following font weights.", TMP_UIStyleManager.Label); GUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); GUILayout.Label("<b>Font Weight</b>", TMP_UIStyleManager.Label, GUILayout.Width(117)); GUILayout.Label("<b>Normal Style</b>", TMP_UIStyleManager.Label); GUILayout.Label("<b>Italic Style</b>", TMP_UIStyleManager.Label); EditorGUILayout.EndHorizontal(); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(1), new GUIContent("100 - Thin")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(2), new GUIContent("200 - Extra-Light")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(3), new GUIContent("300 - Light")); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(4), new GUIContent("400 - Regular")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(5), new GUIContent("500 - Medium")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(6), new GUIContent("600 - Demi-Bold")); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(7), new GUIContent("700 - Bold")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(8), new GUIContent("800 - Heavy")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(9), new GUIContent("900 - Black")); EditorGUILayout.EndVertical(); //EditorGUI.indentLevel = 1; EditorGUIUtility.labelWidth = 120f; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); GUILayout.Label("Settings used to simulate a typeface when no font asset is available.", TMP_UIStyleManager.Label); GUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal Weight")); font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; // Modify the material property on matching material presets. for (int i = 0; i < m_materialPresets.Length; i++) { m_materialPresets[i].SetFloat("_WeightNormal", font_normalStyle_prop.floatValue); } } EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold Weight"), GUILayout.MinWidth(100)); font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; // Modify the material property on matching material presets. for (int i = 0; i < m_materialPresets.Length; i++) { m_materialPresets[i].SetFloat("_WeightBold", font_boldStyle_prop.floatValue); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalSpacing_prop, new GUIContent("Spacing Offset")); font_normalSpacing_prop.floatValue = Mathf.Clamp(font_normalSpacing_prop.floatValue, -100, 100); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; } EditorGUILayout.PropertyField(font_boldSpacing_prop, new GUIContent("Bold Spacing")); font_boldSpacing_prop.floatValue = Mathf.Clamp(font_boldSpacing_prop.floatValue, 0, 100); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style: ")); font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60); EditorGUILayout.PropertyField(font_tabSize_prop, new GUIContent("Tab Multiple: ")); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } GUILayout.Space(5); // FALLBACK FONT ASSETS EditorGUI.indentLevel = 0; if (GUILayout.Button("Fallback Font Assets\t" + (UI_PanelState.fallbackFontAssetPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.fallbackFontAssetPanel = !UI_PanelState.fallbackFontAssetPanel; } if (UI_PanelState.fallbackFontAssetPanel) { EditorGUIUtility.labelWidth = 120; EditorGUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); EditorGUI.indentLevel = 0; GUILayout.Label("Select the Font Assets that will be searched and used as fallback when characters are missing from this font asset.", TMP_UIStyleManager.Label); GUILayout.Space(10f); m_list.DoLayoutList(); EditorGUILayout.EndVertical(); } // GLYPH INFO TABLE EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.fieldWidth = fieldWidth; GUILayout.Space(5); EditorGUI.indentLevel = 0; if (GUILayout.Button("Glyph Info\t" + (UI_PanelState.glyphInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.glyphInfoPanel = !UI_PanelState.glyphInfoPanel; } if (UI_PanelState.glyphInfoPanel) { int arraySize = m_glyphInfoList_prop.arraySize; int itemsPerPage = 15; // Display Glyph Management Tools EditorGUILayout.BeginVertical(TMP_UIStyleManager.Group_Label, GUILayout.ExpandWidth(true)); { // Search Bar implementation #region DISPLAY SEARCH BAR EditorGUILayout.BeginHorizontal(); { EditorGUIUtility.labelWidth = 110f; EditorGUI.BeginChangeCheck(); string searchPattern = EditorGUILayout.TextField("Glyph Search", m_searchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_isSearchDirty) { if (string.IsNullOrEmpty(searchPattern) == false) { //GUIUtility.keyboardControl = 0; m_searchPattern = searchPattern.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim(); // Search Glyph Table for potential matches SearchGlyphTable(m_searchPattern, ref m_searchList); } m_isSearchDirty = false; } string styleName = string.IsNullOrEmpty(m_searchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, styleName)) { GUIUtility.keyboardControl = 0; m_searchPattern = string.Empty; } } EditorGUILayout.EndHorizontal(); #endregion // Display Page Navigation if (!string.IsNullOrEmpty(m_searchPattern)) { arraySize = m_searchList.Count; } DisplayGlyphPageNavigation(arraySize, itemsPerPage); } EditorGUILayout.EndVertical(); // Display Glyph Table Elements #region Glyph Table if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_GlyphPage; i < arraySize && i < itemsPerPage * (m_GlyphPage + 1); i++) { // Define the start of the selection region of the element. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int elementIndex = i; if (!string.IsNullOrEmpty(m_searchPattern)) { elementIndex = m_searchList[i]; } SerializedProperty glyphInfo = m_glyphInfoList_prop.GetArrayElementAtIndex(elementIndex); EditorGUI.BeginDisabledGroup(i != m_selectedElement); { EditorGUILayout.BeginVertical(TMP_UIStyleManager.Group_Label); EditorGUILayout.PropertyField(glyphInfo); EditorGUILayout.EndVertical(); } EditorGUI.EndDisabledGroup(); // Define the end of the selection region of the element. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); // Check for Item selection Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y); if (DoSelectionCheck(selectionArea)) { m_selectedElement = i; m_AddGlyphWarning.isEnabled = false; m_unicodeHexLabel = k_placeholderUnicodeHex; GUIUtility.keyboardControl = 0; } // Draw Selection Highlight and Glyph Options if (m_selectedElement == i) { TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255)); // Draw Glyph management options Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); float optionAreaWidth = controlRect.width * 0.6f; float btnWidth = optionAreaWidth / 3; Rect position = new Rect(controlRect.x + controlRect.width * .4f, controlRect.y, btnWidth, controlRect.height); // Copy Selected Glyph to Target Glyph ID GUI.enabled = !string.IsNullOrEmpty(m_dstGlyphID); if (GUI.Button(position, new GUIContent("Copy to"))) { GUIUtility.keyboardControl = 0; // Convert Hex Value to Decimal int dstGlyphID = TMP_TextUtilities.StringToInt(m_dstGlyphID); //Add new glyph at target Unicode hex id. if (!AddNewGlyph(elementIndex, dstGlyphID)) { m_AddGlyphWarning.isEnabled = true; m_AddGlyphWarning.expirationTime = EditorApplication.timeSinceStartup + 1; } m_dstGlyphID = string.Empty; m_isSearchDirty = true; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); } // Target Glyph ID GUI.enabled = true; position.x += btnWidth; GUI.SetNextControlName("GlyphID_Input"); m_dstGlyphID = EditorGUI.TextField(position, m_dstGlyphID); // Placeholder text EditorGUI.LabelField(position, new GUIContent(m_unicodeHexLabel, "The Unicode (Hex) ID of the duplicated Glyph"), TMP_UIStyleManager.Label); // Only filter the input when the destination glyph ID text field has focus. if (GUI.GetNameOfFocusedControl() == "GlyphID_Input") { m_unicodeHexLabel = string.Empty; //Filter out unwanted characters. char chr = Event.current.character; if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F')) { Event.current.character = '\0'; } } else { m_unicodeHexLabel = k_placeholderUnicodeHex; } // Remove Glyph position.x += btnWidth; if (GUI.Button(position, "Remove")) { GUIUtility.keyboardControl = 0; RemoveGlyphFromList(elementIndex); m_selectedElement = -1; m_isSearchDirty = true; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); return; } if (m_AddGlyphWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddGlyphWarning.expirationTime) { EditorGUILayout.HelpBox("The Destination Glyph ID already exists", MessageType.Warning); } } } } DisplayGlyphPageNavigation(arraySize, itemsPerPage); } #endregion // KERNING TABLE PANEL #region Kerning Table GUILayout.Space(5); if (GUILayout.Button("Kerning Table Info\t" + (UI_PanelState.kerningInfoPanel ? uiStateLabel[1] : uiStateLabel[0]), TMP_UIStyleManager.Section_Label)) { UI_PanelState.kerningInfoPanel = !UI_PanelState.kerningInfoPanel; } if (UI_PanelState.kerningInfoPanel) { Rect pos; SerializedProperty kerningPairs_prop = m_kerningInfo_prop.FindPropertyRelative("kerningPairs"); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Left Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Right Char", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label("Offset Value", TMP_UIStyleManager.TMP_GUISkin.label); GUILayout.Label(GUIContent.none, GUILayout.Width(20)); EditorGUILayout.EndHorizontal(); GUILayout.BeginVertical(TMP_UIStyleManager.TMP_GUISkin.label); int arraySize = kerningPairs_prop.arraySize; int itemsPerPage = 25; if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_KerningPage; i < arraySize && i < itemsPerPage * (m_KerningPage + 1); i++) { SerializedProperty kerningPair_prop = kerningPairs_prop.GetArrayElementAtIndex(i); pos = EditorGUILayout.BeginHorizontal(); EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), kerningPair_prop, GUIContent.none); // Button to Delete Kerning Pair if (GUILayout.Button("-", GUILayout.ExpandWidth(false))) { m_kerningTable.RemoveKerningPair(i); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; break; } EditorGUILayout.EndHorizontal(); } } Rect pagePos = EditorGUILayout.GetControlRect(false, 20); pagePos.width /= 3; int shiftMultiplier = currentEvent.shift ? 10 : 1; // Previous Page if (m_KerningPage > 0) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Previous Page")) { m_KerningPage -= 1 * shiftMultiplier; } // Page Counter GUI.enabled = true; pagePos.x += pagePos.width; int totalPages = (int)(arraySize / (float)itemsPerPage + 0.999f); GUI.Label(pagePos, "Page " + (m_KerningPage + 1) + " / " + totalPages, GUI.skin.button); // Next Page pagePos.x += pagePos.width; if (itemsPerPage * (m_GlyphPage + 1) < arraySize) { GUI.enabled = true; } else { GUI.enabled = false; } if (GUI.Button(pagePos, "Next Page")) { m_KerningPage += 1 * shiftMultiplier; } m_KerningPage = Mathf.Clamp(m_KerningPage, 0, arraySize / itemsPerPage); GUILayout.EndVertical(); GUILayout.Space(10); // Add New Kerning Pair Section GUILayout.BeginVertical(TMP_UIStyleManager.SquareAreaBox85G); pos = EditorGUILayout.BeginHorizontal(); // Draw Empty Kerning Pair EditorGUI.PropertyField(new Rect(pos.x, pos.y, pos.width - 20f, pos.height), m_kerningPair_prop); GUILayout.Label(GUIContent.none, GUILayout.Height(19)); EditorGUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Add New Kerning Pair")) { int asci_left = m_kerningPair_prop.FindPropertyRelative("AscII_Left").intValue; int asci_right = m_kerningPair_prop.FindPropertyRelative("AscII_Right").intValue; float xOffset = m_kerningPair_prop.FindPropertyRelative("XadvanceOffset").floatValue; errorCode = m_kerningTable.AddKerningPair(asci_left, asci_right, xOffset); // Sort Kerning Pairs & Reload Font Asset if new kerning pair was added. if (errorCode != -1) { m_kerningTable.SortKerningPairs(); m_fontAsset.ReadFontDefinition(); // Reload Font Definition. serializedObject.Update(); // Get an updated version of the SerializedObject. isAssetDirty = true; } else { timeStamp = System.DateTime.Now.AddSeconds(5); } } if (errorCode == -1) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.Label); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (System.DateTime.Now > timeStamp) { errorCode = 0; } } GUILayout.EndVertical(); } #endregion if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty) { //Debug.Log("Serialized properties have changed."); TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); isAssetDirty = false; EditorUtility.SetDirty(target); //TMPro_EditorUtility.RepaintAll(); // Consider SetDirty } // Clear selection if mouse event was not consumed. GUI.enabled = true; if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0) { m_selectedElement = -1; } }
public override void OnInspectorGUI() { // Check Warnings //Debug.Log("OnInspectorGUI Called."); Event currentEvent = Event.current; serializedObject.Update(); // TextMeshPro Font Info Panel Rect rect = EditorGUILayout.GetControlRect(); GUI.Label(rect, "Face Info", EditorStyles.boldLabel); rect.x += rect.width - 130f; rect.width = 130f; if (GUI.Button(rect, "Update Atlas Texture")) { TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(target as TMP_FontAsset); } EditorGUI.indentLevel = 1; GUI.enabled = false; // Lock UI float labelWidth = EditorGUIUtility.labelWidth; float fieldWidth = EditorGUIUtility.fieldWidth; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Name"), new GUIContent("Font Source")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("PointSize")); GUI.enabled = true; EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Scale")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("LineHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Ascender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("CapHeight")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Baseline")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Descender")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Underline"), new GUIContent("Underline Offset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("strikethrough"), new GUIContent("Strikethrough Offset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SuperscriptOffset")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("SubscriptOffset")); SerializedProperty subSize_prop = m_fontInfo_prop.FindPropertyRelative("SubSize"); EditorGUILayout.PropertyField(subSize_prop, new GUIContent("Super / Subscript Size")); subSize_prop.floatValue = Mathf.Clamp(subSize_prop.floatValue, 0.25f, 1f); GUI.enabled = false; //EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); //GUILayout.label("Atlas Size"); EditorGUI.indentLevel = 1; EditorGUILayout.Space(); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("Padding")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasWidth"), new GUIContent("Width")); EditorGUILayout.PropertyField(m_fontInfo_prop.FindPropertyRelative("AtlasHeight"), new GUIContent("Height")); GUI.enabled = true; EditorGUILayout.Space(); EditorGUI.indentLevel = 0; UI_PanelState.fontSubAssetsPanel = EditorGUILayout.Foldout(UI_PanelState.fontSubAssetsPanel, new GUIContent("Font Sub-Assets"), true, TMP_UIStyleManager.boldFoldout); if (UI_PanelState.fontSubAssetsPanel) { GUI.enabled = false; EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas")); EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material")); GUI.enabled = true; EditorGUILayout.Space(); } string evt_cmd = Event.current.commandName; // Get Current Event CommandName to check for Undo Events // FONT SETTINGS EditorGUI.indentLevel = 0; UI_PanelState.fontWeightPanel = EditorGUILayout.Foldout(UI_PanelState.fontWeightPanel, new GUIContent("Font Weights", "The Font Assets that will be used for different font weights and the settings used to simulate a typeface when no asset is available."), true, TMP_UIStyleManager.boldFoldout); if (UI_PanelState.fontWeightPanel) { EditorGUIUtility.labelWidth *= 0.75f; EditorGUIUtility.fieldWidth *= 0.25f; EditorGUILayout.BeginVertical(); EditorGUI.indentLevel = 1; rect = EditorGUILayout.GetControlRect(true); rect.x += EditorGUIUtility.labelWidth; rect.width = (rect.width - EditorGUIUtility.labelWidth) / 2f; GUI.Label(rect, "Normal Style", EditorStyles.boldLabel); rect.x += rect.width; GUI.Label(rect, "Italic Style", EditorStyles.boldLabel); EditorGUI.indentLevel = 1; //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(1), new GUIContent("100 - Thin")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(2), new GUIContent("200 - Extra-Light")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(3), new GUIContent("300 - Light")); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(4), new GUIContent("400 - Regular")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(5), new GUIContent("500 - Medium")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(6), new GUIContent("600 - Demi-Bold")); EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(7), new GUIContent("700 - Bold")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(8), new GUIContent("800 - Heavy")); //EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(9), new GUIContent("900 - Black")); EditorGUILayout.EndVertical(); EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal Weight")); font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; // Modify the material property on matching material presets. for (int i = 0; i < m_materialPresets.Length; i++) { m_materialPresets[i].SetFloat("_WeightNormal", font_normalStyle_prop.floatValue); } } EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold Weight")); font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3.0f, 3.0f); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; // Modify the material property on matching material presets. for (int i = 0; i < m_materialPresets.Length; i++) { m_materialPresets[i].SetFloat("_WeightBold", font_boldStyle_prop.floatValue); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_normalSpacing_prop, new GUIContent("Spacing Offset")); font_normalSpacing_prop.floatValue = Mathf.Clamp(font_normalSpacing_prop.floatValue, -100, 100); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; } EditorGUILayout.PropertyField(font_boldSpacing_prop, new GUIContent("Bold Spacing")); font_boldSpacing_prop.floatValue = Mathf.Clamp(font_boldSpacing_prop.floatValue, 0, 100); if (GUI.changed || evt_cmd == k_UndoRedo) { GUI.changed = false; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style")); font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60); EditorGUILayout.PropertyField(font_tabSize_prop, new GUIContent("Tab Multiple")); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); EditorGUILayout.Space(); } EditorGUIUtility.labelWidth = 0; EditorGUIUtility.fieldWidth = 0; // FALLBACK FONT ASSETS EditorGUI.indentLevel = 0; UI_PanelState.fallbackFontAssetPanel = EditorGUILayout.Foldout(UI_PanelState.fallbackFontAssetPanel, new GUIContent("Fallback Font Assets", "Select the Font Assets that will be searched and used as fallback when characters are missing from this font asset."), true, TMP_UIStyleManager.boldFoldout); if (UI_PanelState.fallbackFontAssetPanel) { EditorGUIUtility.labelWidth = 120; EditorGUI.indentLevel = 0; m_list.DoLayoutList(); EditorGUILayout.Space(); } // GLYPH INFO TABLE #region Glyph Table EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.fieldWidth = fieldWidth; EditorGUI.indentLevel = 0; UI_PanelState.glyphInfoPanel = EditorGUILayout.Foldout(UI_PanelState.glyphInfoPanel, new GUIContent("Glyph Table"), true, TMP_UIStyleManager.boldFoldout); if (UI_PanelState.glyphInfoPanel) { int arraySize = m_glyphInfoList_prop.arraySize; int itemsPerPage = 15; // Display Glyph Management Tools EditorGUILayout.BeginVertical(EditorStyles.helpBox); { // Search Bar implementation #region DISPLAY SEARCH BAR EditorGUILayout.BeginHorizontal(); { EditorGUIUtility.labelWidth = 130f; EditorGUI.BeginChangeCheck(); string searchPattern = EditorGUILayout.TextField("Glyph Search", m_GlyphSearchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_isSearchDirty) { if (string.IsNullOrEmpty(searchPattern) == false) { m_GlyphSearchPattern = searchPattern; // Search Glyph Table for potential matches SearchGlyphTable(m_GlyphSearchPattern, ref m_GlyphSearchList); } else { m_GlyphSearchPattern = null; } m_isSearchDirty = false; } string styleName = string.IsNullOrEmpty(m_GlyphSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, styleName)) { GUIUtility.keyboardControl = 0; m_GlyphSearchPattern = string.Empty; } } EditorGUILayout.EndHorizontal(); #endregion // Display Page Navigation if (!string.IsNullOrEmpty(m_GlyphSearchPattern)) { arraySize = m_GlyphSearchList.Count; } DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage); } EditorGUILayout.EndVertical(); // Display Glyph Table Elements if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_CurrentGlyphPage; i < arraySize && i < itemsPerPage * (m_CurrentGlyphPage + 1); i++) { // Define the start of the selection region of the element. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int elementIndex = i; if (!string.IsNullOrEmpty(m_GlyphSearchPattern)) { elementIndex = m_GlyphSearchList[i]; } SerializedProperty glyphInfo = m_glyphInfoList_prop.GetArrayElementAtIndex(elementIndex); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUI.BeginDisabledGroup(i != m_SelectedGlyphRecord); { EditorGUILayout.PropertyField(glyphInfo); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); // Define the end of the selection region of the element. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); // Check for Item selection Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y); if (DoSelectionCheck(selectionArea)) { if (m_SelectedGlyphRecord == i) { m_SelectedGlyphRecord = -1; } else { m_SelectedGlyphRecord = i; m_AddGlyphWarning.isEnabled = false; m_unicodeHexLabel = k_placeholderUnicodeHex; GUIUtility.keyboardControl = 0; } } // Draw Selection Highlight and Glyph Options if (m_SelectedGlyphRecord == i) { TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255)); // Draw Glyph management options Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); float optionAreaWidth = controlRect.width * 0.6f; float btnWidth = optionAreaWidth / 3; Rect position = new Rect(controlRect.x + controlRect.width * .4f, controlRect.y, btnWidth, controlRect.height); // Copy Selected Glyph to Target Glyph ID GUI.enabled = !string.IsNullOrEmpty(m_dstGlyphID); if (GUI.Button(position, new GUIContent("Copy to"))) { GUIUtility.keyboardControl = 0; // Convert Hex Value to Decimal int dstGlyphID = TMP_TextUtilities.StringToInt(m_dstGlyphID); //Add new glyph at target Unicode hex id. if (!AddNewGlyph(elementIndex, dstGlyphID)) { m_AddGlyphWarning.isEnabled = true; m_AddGlyphWarning.expirationTime = EditorApplication.timeSinceStartup + 1; } m_dstGlyphID = string.Empty; m_isSearchDirty = true; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); } // Target Glyph ID GUI.enabled = true; position.x += btnWidth; GUI.SetNextControlName("GlyphID_Input"); m_dstGlyphID = EditorGUI.TextField(position, m_dstGlyphID); // Placeholder text EditorGUI.LabelField(position, new GUIContent(m_unicodeHexLabel, "The Unicode (Hex) ID of the duplicated Glyph"), TMP_UIStyleManager.label); // Only filter the input when the destination glyph ID text field has focus. if (GUI.GetNameOfFocusedControl() == "GlyphID_Input") { m_unicodeHexLabel = string.Empty; //Filter out unwanted characters. char chr = Event.current.character; if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F')) { Event.current.character = '\0'; } } else { m_unicodeHexLabel = k_placeholderUnicodeHex; } // Remove Glyph position.x += btnWidth; if (GUI.Button(position, "Remove")) { GUIUtility.keyboardControl = 0; RemoveGlyphFromList(elementIndex); isAssetDirty = true; m_SelectedGlyphRecord = -1; m_isSearchDirty = true; break; } if (m_AddGlyphWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddGlyphWarning.expirationTime) { EditorGUILayout.HelpBox("The Destination Glyph ID already exists", MessageType.Warning); } } } } DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage); EditorGUILayout.Space(); } #endregion // KERNING TABLE PANEL #region Kerning Table EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.fieldWidth = fieldWidth; EditorGUI.indentLevel = 0; UI_PanelState.kerningInfoPanel = EditorGUILayout.Foldout(UI_PanelState.kerningInfoPanel, new GUIContent("Glyph Adjustment Table"), true, TMP_UIStyleManager.boldFoldout); if (UI_PanelState.kerningInfoPanel) { int arraySize = m_kerningPairs_prop.arraySize; int itemsPerPage = 20; // Display Kerning Pair Management Tools EditorGUILayout.BeginVertical(EditorStyles.helpBox); { // Search Bar implementation #region DISPLAY SEARCH BAR EditorGUILayout.BeginHorizontal(); { EditorGUIUtility.labelWidth = 150f; EditorGUI.BeginChangeCheck(); string searchPattern = EditorGUILayout.TextField("Adjustment Pair Search", m_KerningTableSearchPattern, "SearchTextField"); if (EditorGUI.EndChangeCheck() || m_isSearchDirty) { if (string.IsNullOrEmpty(searchPattern) == false) { m_KerningTableSearchPattern = searchPattern; // Search Glyph Table for potential matches SearchKerningTable(m_KerningTableSearchPattern, ref m_KerningTableSearchList); } else { m_KerningTableSearchPattern = null; } m_isSearchDirty = false; } string styleName = string.IsNullOrEmpty(m_KerningTableSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton"; if (GUILayout.Button(GUIContent.none, styleName)) { GUIUtility.keyboardControl = 0; m_KerningTableSearchPattern = string.Empty; } } EditorGUILayout.EndHorizontal(); #endregion // Display Page Navigation if (!string.IsNullOrEmpty(m_KerningTableSearchPattern)) { arraySize = m_KerningTableSearchList.Count; } DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage); } EditorGUILayout.EndVertical(); //Rect pos; //pos = EditorGUILayout.GetControlRect(false, 20); //pos.x += 5; //EditorGUI.LabelField(pos, "First Glyph", TMP_UIStyleManager.TMP_GUISkin.label); //pos.x += 100; //EditorGUI.LabelField(pos, "Adjustment Values", TMP_UIStyleManager.TMP_GUISkin.label); //pos.x = pos.width / 2 + 5; //EditorGUI.LabelField(pos, "Second Glyph", TMP_UIStyleManager.TMP_GUISkin.label); //pos.x += 100; //EditorGUI.LabelField(pos, "Adjustment Values", TMP_UIStyleManager.TMP_GUISkin.label); if (arraySize > 0) { // Display each GlyphInfo entry using the GlyphInfo property drawer. for (int i = itemsPerPage * m_CurrentKerningPage; i < arraySize && i < itemsPerPage * (m_CurrentKerningPage + 1); i++) { // Define the start of the selection region of the element. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); int elementIndex = i; if (!string.IsNullOrEmpty(m_KerningTableSearchPattern)) { elementIndex = m_KerningTableSearchList[i]; } SerializedProperty kerningInfo = m_kerningPairs_prop.GetArrayElementAtIndex(elementIndex); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUI.BeginDisabledGroup(i != m_SelectedAdjustmentRecord); { EditorGUILayout.PropertyField(kerningInfo, new GUIContent("Selectable")); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); // Define the end of the selection region of the element. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)); // Check for Item selection Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y); if (DoSelectionCheck(selectionArea)) { if (m_SelectedAdjustmentRecord == i) { m_SelectedAdjustmentRecord = -1; } else { m_SelectedAdjustmentRecord = i; GUIUtility.keyboardControl = 0; } } // Draw Selection Highlight and Kerning Pair Options if (m_SelectedAdjustmentRecord == i) { TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255)); // Draw Glyph management options Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f); float optionAreaWidth = controlRect.width; float btnWidth = optionAreaWidth / 4; Rect position = new Rect(controlRect.x + controlRect.width - btnWidth, controlRect.y, btnWidth, controlRect.height); // Remove Kerning pair GUI.enabled = true; if (GUI.Button(position, "Remove")) { GUIUtility.keyboardControl = 0; m_kerningTable.RemoveKerningPair(i); m_fontAsset.ReadFontDefinition(); isAssetDirty = true; m_SelectedAdjustmentRecord = -1; m_isSearchDirty = true; break; } } } } DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage); GUILayout.Space(5); // Add new kerning pair EditorGUILayout.BeginVertical(EditorStyles.helpBox); { EditorGUILayout.PropertyField(m_kerningPair_prop); } EditorGUILayout.EndVertical(); if (GUILayout.Button("Add New Kerning Pair")) { int firstGlyph = m_kerningPair_prop.FindPropertyRelative("m_FirstGlyph").intValue; int secondGlyph = m_kerningPair_prop.FindPropertyRelative("m_SecondGlyph").intValue; GlyphValueRecord firstGlyphAdjustments = GetGlyphAdjustments(m_kerningPair_prop.FindPropertyRelative("m_FirstGlyphAdjustments")); GlyphValueRecord secondGlyphAdjustments = GetGlyphAdjustments(m_kerningPair_prop.FindPropertyRelative("m_SecondGlyphAdjustments")); errorCode = m_kerningTable.AddGlyphPairAdjustmentRecord((uint)firstGlyph, firstGlyphAdjustments, (uint)secondGlyph, secondGlyphAdjustments); // Sort Kerning Pairs & Reload Font Asset if new kerning pair was added. if (errorCode != -1) { m_kerningTable.SortKerningPairs(); m_fontAsset.ReadFontDefinition(); serializedObject.ApplyModifiedProperties(); isAssetDirty = true; m_isSearchDirty = true; } else { timeStamp = System.DateTime.Now.AddSeconds(5); } // Clear Add Kerning Pair Panel // TODO } if (errorCode == -1) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.label); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (System.DateTime.Now > timeStamp) { errorCode = 0; } } } #endregion if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty) { TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset); isAssetDirty = false; EditorUtility.SetDirty(target); } // Clear selection if mouse event was not consumed. GUI.enabled = true; if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0) { m_SelectedAdjustmentRecord = -1; } }
private void Save_SDF_FontAsset(string filePath) { string relativeAssetPath = filePath; string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. var fontAsset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(tex_Path_NoExt + ".asset"); if (fontAsset == null) { //Debug.Log("Creating TextMeshPro font asset!"); fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset"); //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; // If using the C# SDF creation mode, we need the scale down factor. int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_fontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_fontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_font); KerningTable kerningTable = GetKerningTable(fontFilePath, ( int )face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_fontAtlas; if (!m_fontAtlas.name.EndsWith(" Atlas")) { m_fontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_fontAtlas, fontAsset); } // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_fontAtlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_fontAtlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_fontAtlas.height); int spread = Padding + 1; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); } else { // Find all Materials referencing this font atlas. Material[] material_references = FindMaterialReferences(fontAsset); if (fontAsset.atlas) { // Destroy Assets that will be replaced. GameObject.DestroyImmediate(fontAsset.atlas, true); } //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_fontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_fontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_font); KerningTable kerningTable = GetKerningTable(fontFilePath, ( int )face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_fontAtlas; if (!m_fontAtlas.name.EndsWith(" Atlas")) { m_fontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_fontAtlas, fontAsset); } // Special handling due to a bug in earlier versions of Unity. m_fontAtlas.hideFlags = HideFlags.None; fontAsset.material.hideFlags = HideFlags.None; // Assign new font atlas texture to the existing material. fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_fontAtlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_fontAtlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_fontAtlas.height); int spread = Padding + 1; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); } } fontAsset.ReadFontDefinition(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); // Re-import font asset to get the new updated version. AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }
void Save_SDF_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string relativeAssetPath = filePath.Substring(dataPath.Length - 6); string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont; if (font_asset == null) { //Debug.Log("Creating TextMeshPro font asset!"); font_asset = ScriptableObject.CreateInstance <TextMeshProFont>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(font_asset, tex_Path_NoExt + ".asset"); if (m_destination_Atlas != null) { m_font_Atlas = m_destination_Atlas; } // If using the C# SDF creation mode, we need the scaledown factor. int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, scaleDownFactor); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Line Breaking Rules //LineBreakingTable lineBreakingTable = new LineBreakingTable(); // // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TMPro/Distance Field"); Material tmp_material = new Material(default_Shader); //tmp_material.shaderKeywords = new string[] { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" }; tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle); int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. font_asset.material = tmp_material; tmp_material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(tmp_material, font_asset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material); // Destroy Assets that will be replaced. DestroyImmediate(font_asset.atlas, true); int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, scaleDownFactor); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, font_asset.atlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle); int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. } } // Saving File for Debug //var pngData = destination_Atlas.EncodeToPNG(); //File.WriteAllBytes("Assets/Textures/Debug Distance Field.png", pngData); //font_asset.fontCreationSettings = SaveFontCreationSettings(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(font_asset)); // Re-import font asset to get the new updated version. font_asset.ReadFontDefinition(); AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, font_asset); }
void Save_Normal_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string relativeAssetPath = filePath.Substring(dataPath.Length - 6); string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont; if (font_asset == null) { //Debug.Log("Creating TextMeshPro font asset!"); font_asset = ScriptableObject.CreateInstance <TextMeshProFont>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(font_asset, tex_Path_NoExt + ".asset"); // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, 1); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TMPro/Bitmap"); Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); font_asset.material = tmp_material; tmp_material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(tmp_material, font_asset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material); // Destroy Assets that will be replaced. DestroyImmediate(font_asset.atlas, true); // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, 1); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, font_asset.atlas); } } AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(font_asset)); // Re-import font asset to get the new updated version. //EditorUtility.SetDirty(font_asset); font_asset.ReadFontDefinition(); AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, font_asset); }
public void Load() { // Bobbin refresh BobbinCore.DoRefresh(); Debug.Log("Complete Bobbin Refresh"); // Bake text foreach (Location location in locations) { int locationIndex = location.index; foreach (TMP_FontAsset fontAsset in location.fontAssets) { if (fontAsset == null) { Debug.LogError("<color=blue>[Error] </color> Font asset doesn't set in list"); continue; } Debug.Log($"Bake [ {location.name} ]( {fontAsset.name} )"); // Step 1 : Parsing with CsvReader StringBuilder strBuilder = new StringBuilder(); using (var streamRdr = new StreamReader(stringPath)) { var csvReader = new CsvReader(streamRdr, ","); while (csvReader.Read()) { if (!string.IsNullOrWhiteSpace(csvReader[locationIndex])) { strBuilder.Append(csvReader[locationIndex]); } } } // Step 2 : Delete duplicates string characterSequence = strBuilder.ToString(); uint[] characterSet = null; List <uint> characters = new List <uint>(); for (int i = 0; i < characterSequence.Length; i++) { uint unicode = characterSequence[i]; // Handle surrogate pairs if (i < characterSequence.Length - 1 && char.IsHighSurrogate((char)unicode) && char.IsLowSurrogate(characterSequence[i + 1])) { unicode = (uint)char.ConvertToUtf32(characterSequence[i], characterSequence[i + 1]); i += 1; } // Check to make sure we don't include duplicates if (characters.FindIndex(item => item == unicode) == -1) { characters.Add(unicode); } } characterSet = characters.ToArray(); Debug.Log("Total count : " + characterSet.Length); // Step 4 : Generate atlas uint[] missingString = null; fontAsset.ClearFontAssetData(); KeyValuePair <TMP_FontAsset, uint[]> value = new KeyValuePair <TMP_FontAsset, uint[]>(fontAsset, characterSet); TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(value); StringBuilder missingStrBuilder = new StringBuilder(); if (missingString != null) { foreach (uint unicode in missingString) { missingStrBuilder.Append(Convert.ToChar(unicode)); missingStrBuilder.Append(" "); } Debug.LogError($"<color=red>[Fatal error] </color>Missing string : {missingStrBuilder} "); } fontAsset.atlasPopulationMode = AtlasPopulationMode.Static; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); EditorUtility.SetDirty(fontAsset); } } }
private void Save_SDF_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath, StringComparison.InvariantCultureIgnoreCase) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string path = filePath.Substring(dataPath.Length - 6); string directoryName = Path.GetDirectoryName(path); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); string str = directoryName + "/" + fileNameWithoutExtension; TMP_FontAsset tMP_FontAsset = AssetDatabase.LoadAssetAtPath(str + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset; if (tMP_FontAsset == null) { tMP_FontAsset = ScriptableObject.CreateInstance <WeaverCore.Assets.TMPro.TMP_FontAsset>(); AssetDatabase.CreateAsset(tMP_FontAsset, str + ".asset"); tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleFactor = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor; FaceInfo faceInfo = GetFaceInfo(m_font_faceInfo, scaleFactor); tMP_FontAsset.AddFaceInfo(faceInfo); TMP_Glyph[] glyphInfo = GetGlyphInfo(m_font_glyphInfo, scaleFactor); tMP_FontAsset.AddGlyphInfo(glyphInfo); if (includeKerningPairs) { string assetPath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(assetPath, (int)faceInfo.PointSize); tMP_FontAsset.AddKerningInfo(kerningTable); } tMP_FontAsset.atlas = m_font_Atlas; m_font_Atlas.name = fileNameWithoutExtension + " Atlas"; AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset); Shader shader = Shader.Find("TextMeshPro/Distance Field"); Material material = new Material(shader); material.name = fileNameWithoutExtension + " Material"; material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); int num = font_padding + 1; material.SetFloat(ShaderUtilities.ID_GradientScale, num); material.SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle); material.SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle); tMP_FontAsset.material = material; AssetDatabase.AddObjectToAsset(material, tMP_FontAsset); } else { Material[] array = TMP_EditorUtility.FindMaterialReferences(tMP_FontAsset); UnityEngine.Object.DestroyImmediate(tMP_FontAsset.atlas, true); tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleFactor2 = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor; FaceInfo faceInfo2 = GetFaceInfo(m_font_faceInfo, scaleFactor2); tMP_FontAsset.AddFaceInfo(faceInfo2); TMP_Glyph[] glyphInfo2 = GetGlyphInfo(m_font_glyphInfo, scaleFactor2); tMP_FontAsset.AddGlyphInfo(glyphInfo2); if (includeKerningPairs) { string assetPath2 = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable2 = GetKerningTable(assetPath2, (int)faceInfo2.PointSize); tMP_FontAsset.AddKerningInfo(kerningTable2); } tMP_FontAsset.atlas = m_font_Atlas; m_font_Atlas.name = fileNameWithoutExtension + " Atlas"; m_font_Atlas.hideFlags = HideFlags.None; tMP_FontAsset.material.hideFlags = HideFlags.None; AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset); tMP_FontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, tMP_FontAsset.atlas); for (int i = 0; i < array.Length; i++) { array[i].SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); array[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); array[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); int num2 = font_padding + 1; array[i].SetFloat(ShaderUtilities.ID_GradientScale, num2); array[i].SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle); array[i].SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle); } } AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tMP_FontAsset)); tMP_FontAsset.ReadFontDefinition(); AssetDatabase.Refresh(); m_font_Atlas = null; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, tMP_FontAsset); }
void Save_SDF_FontAsset(string filePath) { //filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. //string dataPath = Application.dataPath; //if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1) //{ // Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); // return; //} string relativeAssetPath = filePath; string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(tex_Path_NoExt + ".asset"); if (fontAsset == null) { //Debug.Log("Creating TextMeshPro font asset!"); fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset"); // Reference to the source font file //font_asset.sourceFontFile = font_TTF as Font; //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; //if (m_destination_Atlas != null) // m_font_Atlas = m_destination_Atlas; // If using the C# SDF creation mode, we need the scale down factor. int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (m_IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Line Breaking Rules //LineBreakingTable lineBreakingTable = new LineBreakingTable(); // // Add Font Atlas as Sub-Asset fontAsset.atlas = m_FontAtlas; if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里 { m_FontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset); } // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height); int spread = m_Padding + 1; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMProFontCustomizedCreater.FindMaterialReferences(fontAsset); if (fontAsset.atlas) // 有可能被其他资产删除了 { // Destroy Assets that will be replaced. DestroyImmediate(fontAsset.atlas, true); } //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (m_IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_FontAtlas; if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里 { m_FontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset); } // Special handling due to a bug in earlier versions of Unity. m_FontAtlas.hideFlags = HideFlags.None; fontAsset.material.hideFlags = HideFlags.None; // Assign new font atlas texture to the existing material. fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height); int spread = m_Padding + 1; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); } } // Saving File for Debug //var pngData = m_FontAtlas.EncodeToPNG(); //File.WriteAllBytes("Assets/Debug Distance Field.png", pngData); // Save Font Asset creation settings m_SelectedFontAsset = fontAsset; m_LegacyFontAsset = null; // 提到这里才能保存完整 fontAsset.ReadFontDefinition(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); // Re-import font asset to get the new updated version. //fontAsset.ReadFontDefinition(); AssetDatabase.Refresh(); //m_FontAtlas = null; 贴图不要删除 // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }