TMP_FontAsset_CreationMenu.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine.TextCore;
  8. using UnityEngine.TextCore.LowLevel;
  9. using TMPro;
  10. namespace TMPro
  11. {
  12. public static class TMP_FontAsset_CreationMenu
  13. {
  14. /*
  15. [MenuItem("Assets/Create/TextMeshPro/Font Asset Fallback", false, 105)]
  16. public static void CreateFallbackFontAsset()
  17. {
  18. Object target = Selection.activeObject;
  19. // Make sure the selection is a font file
  20. if (target == null || target.GetType() != typeof(TMP_FontAsset))
  21. {
  22. Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
  23. return;
  24. }
  25. TMP_FontAsset sourceFontAsset = (TMP_FontAsset)target;
  26. string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
  27. string folderPath = Path.GetDirectoryName(sourceFontFilePath);
  28. string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
  29. string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " - Fallback.asset");
  30. //// Create new TM Font Asset.
  31. TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
  32. AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
  33. fontAsset.version = "1.1.0";
  34. fontAsset.faceInfo = sourceFontAsset.faceInfo;
  35. fontAsset.m_SourceFontFileGUID = sourceFontAsset.m_SourceFontFileGUID;
  36. fontAsset.m_SourceFontFile_EditorRef = sourceFontAsset.m_SourceFontFile_EditorRef;
  37. fontAsset.atlasPopulationMode = TMP_FontAsset.AtlasPopulationMode.Dynamic;
  38. int atlasWidth = fontAsset.atlasWidth = sourceFontAsset.atlasWidth;
  39. int atlasHeight = fontAsset.atlasHeight = sourceFontAsset.atlasHeight;
  40. int atlasPadding = fontAsset.atlasPadding = sourceFontAsset.atlasPadding;
  41. fontAsset.atlasRenderMode = sourceFontAsset.atlasRenderMode;
  42. // Initialize array for the font atlas textures.
  43. fontAsset.atlasTextures = new Texture2D[1];
  44. // Create and add font atlas texture
  45. Texture2D texture = new Texture2D(atlasWidth, atlasHeight, TextureFormat.Alpha8, false);
  46. Color32[] colors = new Color32[atlasWidth * atlasHeight];
  47. texture.SetPixels32(colors);
  48. texture.name = assetName + " Atlas";
  49. fontAsset.atlasTextures[0] = texture;
  50. AssetDatabase.AddObjectToAsset(texture, fontAsset);
  51. // Add free rectangle of the size of the texture.
  52. int packingModifier = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
  53. fontAsset.m_FreeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
  54. fontAsset.m_UsedGlyphRects = new List<GlyphRect>();
  55. // Create new Material and Add it as Sub-Asset
  56. Material tmp_material = new Material(sourceFontAsset.material);
  57. tmp_material.name = texture.name + " Material";
  58. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  59. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  60. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  61. tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
  62. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  63. tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  64. fontAsset.material = tmp_material;
  65. AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
  66. // Add Font Asset Creation Settings
  67. // TODO
  68. // Not sure if this is still necessary in newer versions of Unity.
  69. EditorUtility.SetDirty(fontAsset);
  70. AssetDatabase.SaveAssets();
  71. }
  72. */
  73. //[MenuItem("Assets/Create/TextMeshPro/Font Asset #%F12", true)]
  74. //public static bool CreateFontAssetMenuValidation()
  75. //{
  76. // return false;
  77. //}
  78. [MenuItem("Assets/Create/TextMeshPro/Font Asset #%F12", false, 100)]
  79. public static void CreateFontAsset()
  80. {
  81. Object target = Selection.activeObject;
  82. // Make sure the selection is a font file
  83. if (target == null || target.GetType() != typeof(Font))
  84. {
  85. Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
  86. return;
  87. }
  88. Font sourceFont = (Font)target;
  89. string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
  90. string folderPath = Path.GetDirectoryName(sourceFontFilePath);
  91. string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
  92. string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " SDF.asset");
  93. //// Create new TM Font Asset.
  94. TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
  95. AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
  96. fontAsset.version = "1.1.0";
  97. // Set face information
  98. FontEngine.InitializeFontEngine();
  99. FontEngine.LoadFontFace(sourceFont, 90);
  100. fontAsset.faceInfo = FontEngine.GetFaceInfo();
  101. // Set font reference and GUID
  102. fontAsset.m_SourceFontFileGUID = AssetDatabase.AssetPathToGUID(sourceFontFilePath);
  103. fontAsset.m_SourceFontFile_EditorRef = sourceFont;
  104. fontAsset.atlasPopulationMode = AtlasPopulationMode.Dynamic;
  105. // Default atlas resolution is 1024 x 1024.
  106. int atlasWidth = fontAsset.atlasWidth = 1024;
  107. int atlasHeight = fontAsset.atlasHeight = 1024;
  108. int atlasPadding = fontAsset.atlasPadding = 9;
  109. fontAsset.atlasRenderMode = GlyphRenderMode.SDFAA;
  110. // Initialize array for the font atlas textures.
  111. fontAsset.atlasTextures = new Texture2D[1];
  112. // Create atlas texture of size zero.
  113. Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
  114. texture.name = assetName + " Atlas";
  115. fontAsset.atlasTextures[0] = texture;
  116. AssetDatabase.AddObjectToAsset(texture, fontAsset);
  117. // Add free rectangle of the size of the texture.
  118. int packingModifier = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
  119. fontAsset.freeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
  120. fontAsset.usedGlyphRects = new List<GlyphRect>();
  121. // Create new Material and Add it as Sub-Asset
  122. Shader default_Shader = Shader.Find("TextMeshPro/Distance Field");
  123. Material tmp_material = new Material(default_Shader);
  124. tmp_material.name = texture.name + " Material";
  125. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  126. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  127. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  128. tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
  129. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  130. tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  131. fontAsset.material = tmp_material;
  132. AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
  133. // Add Font Asset Creation Settings
  134. fontAsset.creationSettings = new FontAssetCreationSettings(fontAsset.m_SourceFontFileGUID, fontAsset.faceInfo.pointSize, 0, atlasPadding, 0, 1024, 1024, 7, string.Empty, (int)GlyphRenderMode.SDFAA);
  135. // Not sure if this is still necessary in newer versions of Unity.
  136. EditorUtility.SetDirty(fontAsset);
  137. AssetDatabase.SaveAssets();
  138. }
  139. }
  140. }