暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_FontAsset_CreationMenu.cs 11KB

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