TMP_FontAsset_CreationMenu.cs 10 KB

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