Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TMP_FontAsset_CreationMenu.cs 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System.IO;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.TextCore;
  5. using UnityEngine.TextCore.LowLevel;
  6. using UnityEditor;
  7. using Object = UnityEngine.Object;
  8. namespace TMPro
  9. {
  10. static class TMP_FontAsset_CreationMenu
  11. {
  12. [MenuItem("Assets/Create/TextMeshPro/Font Asset/Font Asset Variant", false, 200)]
  13. static void CreateFontAssetVariant()
  14. {
  15. Object target = Selection.activeObject;
  16. // Make sure the selection is a font file
  17. if (target == null || target.GetType() != typeof(TMP_FontAsset))
  18. {
  19. Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
  20. return;
  21. }
  22. // Make sure TMP Essential Resources have been imported in the user project.
  23. if (TMP_Settings.instance == null)
  24. {
  25. Debug.Log("Unable to create font asset. Please import the TMP Essential Resources.");
  26. return;
  27. }
  28. TMP_FontAsset sourceFontAsset = (TMP_FontAsset)target;
  29. string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
  30. string folderPath = Path.GetDirectoryName(sourceFontFilePath);
  31. string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
  32. string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " - Variant.asset");
  33. // Set Texture and Material reference to the source font asset.
  34. TMP_FontAsset fontAsset = ScriptableObject.Instantiate<TMP_FontAsset>(sourceFontAsset);
  35. AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
  36. fontAsset.atlasPopulationMode = AtlasPopulationMode.Static;
  37. // Initialize array for the font atlas textures.
  38. fontAsset.atlasTextures = sourceFontAsset.atlasTextures;
  39. fontAsset.material = sourceFontAsset.material;
  40. // Not sure if this is still necessary in newer versions of Unity.
  41. EditorUtility.SetDirty(fontAsset);
  42. AssetDatabase.SaveAssets();
  43. }
  44. [MenuItem("Assets/Create/TextMeshPro/Font Asset/SDF #%F12", false, 100)]
  45. //[MenuItem("Assets/Create/TextMeshPro/Font Asset", false, 100)]
  46. static void CreateFontAssetSDF()
  47. {
  48. CreateFontAsset(GlyphRenderMode.SDFAA);
  49. }
  50. [MenuItem("Assets/Create/TextMeshPro/Font Asset/Bitmap", false, 105)]
  51. static void CreateFontAssetBitmap()
  52. {
  53. CreateFontAsset(GlyphRenderMode.SMOOTH);
  54. }
  55. #if TEXTCORE_FONT_ENGINE_1_5_OR_NEWER
  56. [MenuItem("Assets/Create/TextMeshPro/Font Asset/Color", false, 110)]
  57. static void CreateFontAssetColor()
  58. {
  59. CreateFontAsset(GlyphRenderMode.COLOR);
  60. }
  61. #endif
  62. static void CreateFontAsset(GlyphRenderMode renderMode)
  63. {
  64. Object[] targets = Selection.objects;
  65. if (targets == null)
  66. {
  67. Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
  68. return;
  69. }
  70. // Make sure TMP Essential Resources have been imported in the user project.
  71. if (TMP_Settings.instance == null)
  72. {
  73. Debug.Log("Unable to create font asset. Please import the TMP Essential Resources.");
  74. // Show Window to Import TMP Essential Resources
  75. return;
  76. }
  77. for (int i = 0; i < targets.Length; i++)
  78. {
  79. Object target = targets[i];
  80. // Make sure the selection is a font file
  81. if (target == null || target.GetType() != typeof(Font))
  82. {
  83. 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);
  84. continue;
  85. }
  86. CreateFontAssetFromSelectedObject(target, renderMode);
  87. }
  88. }
  89. static void CreateFontAssetFromSelectedObject(Object target, GlyphRenderMode renderMode)
  90. {
  91. Font font = (Font)target;
  92. string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
  93. string folderPath = Path.GetDirectoryName(sourceFontFilePath);
  94. string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
  95. string newAssetFilePathWithName;
  96. ;
  97. switch (renderMode)
  98. {
  99. case GlyphRenderMode.SMOOTH:
  100. newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " Bitmap.asset");
  101. break;
  102. #if TEXTCORE_FONT_ENGINE_1_5_OR_NEWER
  103. case GlyphRenderMode.COLOR:
  104. newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " Color.asset");
  105. break;
  106. #endif
  107. case GlyphRenderMode.SDFAA:
  108. default:
  109. newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " SDF.asset");
  110. break;
  111. }
  112. // Initialize FontEngine
  113. FontEngine.InitializeFontEngine();
  114. // Load Font Face
  115. if (FontEngine.LoadFontFace(font, 90) != FontEngineError.Success)
  116. {
  117. Debug.LogWarning("Unable to load font face for [" + font.name + "]. Make sure \"Include Font Data\" is enabled in the Font Import Settings.", font);
  118. return;
  119. }
  120. // Create new Font Asset
  121. TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
  122. AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
  123. fontAsset.version = "1.1.0";
  124. fontAsset.faceInfo = FontEngine.GetFaceInfo();
  125. // Set font reference and GUID
  126. fontAsset.sourceFontFile = font;
  127. fontAsset.m_SourceFontFileGUID = AssetDatabase.AssetPathToGUID(sourceFontFilePath);
  128. fontAsset.m_SourceFontFile_EditorRef = font;
  129. fontAsset.atlasPopulationMode = AtlasPopulationMode.Dynamic;
  130. fontAsset.clearDynamicDataOnBuild = TMP_Settings.clearDynamicDataOnBuild;
  131. // Get all font features
  132. //fontAsset.ImportFontFeatures();
  133. // Default atlas resolution is 1024 x 1024.
  134. fontAsset.atlasTextures = new Texture2D[1];
  135. int atlasWidth = fontAsset.atlasWidth = 1024;
  136. int atlasHeight = fontAsset.atlasHeight = 1024;
  137. int atlasPadding = fontAsset.atlasPadding = 9;
  138. Texture2D texture;
  139. Material mat;
  140. Shader shader;
  141. int packingModifier;
  142. switch (renderMode)
  143. {
  144. case GlyphRenderMode.SMOOTH:
  145. fontAsset.atlasRenderMode = GlyphRenderMode.SMOOTH;
  146. texture = new Texture2D(1, 1, TextureFormat.Alpha8, false);
  147. shader = Shader.Find("TextMeshPro/Bitmap");
  148. packingModifier = 0;
  149. mat = new Material(shader);
  150. break;
  151. #if TEXTCORE_FONT_ENGINE_1_5_OR_NEWER
  152. case GlyphRenderMode.COLOR:
  153. fontAsset.atlasRenderMode = GlyphRenderMode.COLOR;
  154. texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  155. shader = Shader.Find("TextMeshPro/Sprite");
  156. packingModifier = 0;
  157. mat = new Material(shader);
  158. break;
  159. #endif
  160. case GlyphRenderMode.SDFAA:
  161. default:
  162. fontAsset.atlasRenderMode = GlyphRenderMode.SDFAA;
  163. texture = new Texture2D(1, 1, TextureFormat.Alpha8, false);
  164. shader = Shader.Find("TextMeshPro/Distance Field");
  165. packingModifier = 1;
  166. mat = new Material(shader);
  167. mat.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
  168. mat.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  169. mat.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  170. break;
  171. }
  172. texture.name = assetName + " Atlas";
  173. mat.name = texture.name + " Material";
  174. fontAsset.atlasTextures[0] = texture;
  175. AssetDatabase.AddObjectToAsset(texture, fontAsset);
  176. fontAsset.freeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
  177. fontAsset.usedGlyphRects = new List<GlyphRect>();
  178. mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
  179. mat.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  180. mat.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  181. fontAsset.material = mat;
  182. AssetDatabase.AddObjectToAsset(mat, fontAsset);
  183. // Add Font Asset Creation Settings
  184. fontAsset.creationSettings = new FontAssetCreationSettings(fontAsset.m_SourceFontFileGUID, (int)fontAsset.faceInfo.pointSize, 0, atlasPadding, 0, 1024, 1024, 7, string.Empty, (int)renderMode);
  185. // Not sure if this is still necessary in newer versions of Unity.
  186. //EditorUtility.SetDirty(fontAsset);
  187. AssetDatabase.SaveAssets();
  188. }
  189. }
  190. }