private void DragAndDropGUI() { Event current = Event.current; Rect rect = new Rect(m_inspectorStartRegion.x, m_inspectorStartRegion.y, m_inspectorEndRegion.width, m_inspectorEndRegion.y - m_inspectorStartRegion.y); EventType type = current.type; if ((uint)(type - 9) > 1u || !rect.Contains(current.mousePosition)) { return; } DragAndDrop.visualMode = DragAndDropVisualMode.Generic; if (current.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); Material material = base.target as Material; Texture texture = material.GetTexture(ShaderUtilities.ID_MainTex); Material material2 = DragAndDrop.objectReferences[0] as Material; Texture texture2 = material2.GetTexture(ShaderUtilities.ID_MainTex); TMP_FontAsset tMP_FontAsset = null; if (material2 == null || material2 == material || material2 == null || texture2 == null) { return; } if (texture2.GetInstanceID() != texture.GetInstanceID()) { tMP_FontAsset = TMP_EditorUtility.FindMatchingFontAsset(material2); if (tMP_FontAsset == null) { return; } } GameObject[] gameObjects = Selection.gameObjects; for (int i = 0; i < gameObjects.Length; i++) { if (tMP_FontAsset != null) { TMP_Text component = gameObjects[i].GetComponent <TMP_Text>(); if ((UnityEngine.Object)(object) component != null) { Undo.RecordObject((UnityEngine.Object)(object) component, "Font Asset Change"); component.font = tMP_FontAsset; } } TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(gameObjects[i], material, material2); EditorUtility.SetDirty(gameObjects[i]); } } current.Use(); }
protected GUIContent[] GetMaterialPresets() { TMP_FontAsset fontAsset = tp.FontAsset; if (fontAsset == null) { return(null); } m_MaterialPresets = TMP_EditorUtility.FindMaterialReferences(fontAsset); m_MaterialPresetNames = new GUIContent[m_MaterialPresets.Length]; for (int i = 0; i < m_MaterialPresetNames.Length; i++) { m_MaterialPresetNames[i] = new GUIContent(m_MaterialPresets[i].name); } return(m_MaterialPresetNames); }
// 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); }
private void Font() { EditorGUI.BeginChangeCheck(); TMP_FontAsset fontAsset = (TMP_FontAsset)EditorGUILayout.ObjectField(k_FontAssetLabel, tp.FontAsset, typeof(TMP_FontAsset), true); if (EditorGUI.EndChangeCheck()) { tp.textPropertiesUndoEvent(); tp.FontAsset = fontAsset; } rect = EditorGUILayout.GetControlRect(false, 17); int v1, v2, v3, v4, v5, v6, v7; rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + 2f); EditorGUI.PrefixLabel(rect, k_FontStyleLabel); rect.x += EditorGUIUtility.labelWidth; rect.width -= EditorGUIUtility.labelWidth; rect.width = Mathf.Max(25f, rect.width / 7f); v1 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 1) == 1, k_BoldLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 1 : 0; // Bold rect.x += rect.width; v2 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 2) == 2, k_ItalicLabel, TMP_UIStyleManager.alignmentButtonMid) ? 2 : 0; // Italics rect.x += rect.width; v3 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 4) == 4, k_UnderlineLabel, TMP_UIStyleManager.alignmentButtonMid) ? 4 : 0; // Underline rect.x += rect.width; v7 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 64) == 64, k_StrikethroughLabel, TMP_UIStyleManager.alignmentButtonRight) ? 64 : 0; // Strikethrough rect.x += rect.width; int selected = 0; EditorGUI.BeginChangeCheck(); v4 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 8) == 8, k_LowercaseLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 8 : 0; // Lowercase if (EditorGUI.EndChangeCheck() && v4 > 0) { selected = v4; } rect.x += rect.width; EditorGUI.BeginChangeCheck(); v5 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 16) == 16, k_UppercaseLabel, TMP_UIStyleManager.alignmentButtonMid) ? 16 : 0; // Uppercase if (EditorGUI.EndChangeCheck() && v5 > 0) { selected = v5; } rect.x += rect.width; EditorGUI.BeginChangeCheck(); v6 = TMP_EditorUtility.EditorToggle(rect, (tp.FontStyle & 32) == 32, k_SmallcapsLabel, TMP_UIStyleManager.alignmentButtonRight) ? 32 : 0; // Smallcaps if (EditorGUI.EndChangeCheck() && v6 > 0) { selected = v6; } if (selected > 0) { v4 = selected == 8 ? 8 : 0; v5 = selected == 16 ? 16 : 0; v6 = selected == 32 ? 32 : 0; } int fontStyle = v1 + v2 + v3 + v4 + v5 + v6 + v7; if (fontStyle != tp.FontStyle) { tp.textPropertiesUndoEvent(); tp.FontStyle = fontStyle; } EditorGUI.BeginDisabledGroup(tp.AutoSize); EditorGUI.BeginChangeCheck(); float fontSize = EditorGUILayout.FloatField(k_FontSizeLabel, tp.FontSize, GUILayout.MaxWidth(EditorGUIUtility.labelWidth + 50f)); if (EditorGUI.EndChangeCheck()) { tp.textPropertiesUndoEvent(); tp.FontSize = fontSize; } EditorGUI.EndDisabledGroup(); EditorGUI.indentLevel += 1; EditorGUI.BeginChangeCheck(); bool autoSize = EditorGUILayout.Toggle(k_AutoSizeLabel, tp.AutoSize); if (EditorGUI.EndChangeCheck()) { tp.textPropertiesUndoEvent(); tp.AutoSize = autoSize; } if (tp.AutoSize) { rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight); EditorGUI.PrefixLabel(rect, k_AutoSizeOptionsLabel); int previousIndent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; rect.width = (rect.width - EditorGUIUtility.labelWidth) / 4f; rect.x += EditorGUIUtility.labelWidth; EditorGUI.BeginChangeCheck(); EditorGUIUtility.labelWidth = 24; float fontSizeMin = EditorGUI.FloatField(rect, k_MinLabel, tp.FontSizeMin); rect.x += rect.width; EditorGUIUtility.labelWidth = 27; float fontSizeMax = EditorGUI.FloatField(rect, k_MaxLabel, tp.FontSizeMax); rect.x += rect.width; EditorGUIUtility.labelWidth = 36; float characterWidthAdjustment = EditorGUI.FloatField(rect, k_WdLabel, tp.CharacterWidthAdjustment); rect.x += rect.width; EditorGUIUtility.labelWidth = 28; float lineSpacingAdjustment = EditorGUI.FloatField(rect, k_LineLabel, tp.LineSpacingAdjustment); EditorGUIUtility.labelWidth = 0; EditorGUI.indentLevel = previousIndent; if (EditorGUI.EndChangeCheck()) { tp.textPropertiesUndoEvent(); tp.FontSizeMin = fontSizeMin; tp.FontSizeMax = fontSizeMax; tp.CharacterWidthAdjustment = characterWidthAdjustment; tp.LineSpacingAdjustment = lineSpacingAdjustment; } } EditorGUI.indentLevel -= 1; EditorGUILayout.Space(); }
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, 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(tex_Path_NoExt + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset; 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.Bitmap; // Reference to the source font file //font_asset.sourceFontFile = font_TTF as Font; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, 1); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, 1); 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; 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/Bitmap"); // m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMP_EditorUtility.FindMaterialReferences(fontAsset); // Destroy Assets that will be replaced. DestroyImmediate(fontAsset.atlas, true); //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.Bitmap; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, 1); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, 1); 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; m_FontAtlas.name = tex_FileName + " Atlas"; // Special handling due to a bug in earlier versions of Unity. m_FontAtlas.hideFlags = HideFlags.None; fontAsset.material.hideFlags = HideFlags.None; AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset); // 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); } } // Save Font Asset creation settings m_SelectedFontAsset = fontAsset; m_LegacyFontAsset = null; AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); // Re-import font asset to get the new updated version. //EditorUtility.SetDirty(font_asset); 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); }