TMPro_ContextMenus.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections;
  5. namespace TMPro.EditorUtilities
  6. {
  7. public class TMP_ContextMenus : Editor
  8. {
  9. private static Texture m_copiedTexture;
  10. private static Material m_copiedProperties;
  11. private static Material m_copiedAtlasProperties;
  12. // Add a Context Menu to the Texture Editor Panel to allow Copy / Paste of Texture.
  13. [MenuItem("CONTEXT/Texture/Copy", false, 2000)]
  14. static void CopyTexture(MenuCommand command)
  15. {
  16. m_copiedTexture = command.context as Texture;
  17. }
  18. // Select the currently assigned material or material preset.
  19. [MenuItem("CONTEXT/Material/Select Material", false, 500)]
  20. static void SelectMaterial(MenuCommand command)
  21. {
  22. Material mat = command.context as Material;
  23. // Select current material
  24. EditorUtility.FocusProjectWindow();
  25. EditorGUIUtility.PingObject(mat);
  26. }
  27. // Add a Context Menu to allow easy duplication of the Material.
  28. [MenuItem("CONTEXT/Material/Create Material Preset", false)]
  29. static void DuplicateMaterial(MenuCommand command)
  30. {
  31. // Get the type of text object
  32. // If material is not a base material, we get material leaks...
  33. Material source_Mat = (Material)command.context;
  34. if (!EditorUtility.IsPersistent(source_Mat))
  35. {
  36. Debug.LogWarning("Material is an instance and cannot be converted into a permanent asset.");
  37. return;
  38. }
  39. string assetPath = AssetDatabase.GetAssetPath(source_Mat).Split('.')[0];
  40. Material duplicate = new Material(source_Mat);
  41. // Need to manually copy the shader keywords
  42. duplicate.shaderKeywords = source_Mat.shaderKeywords;
  43. AssetDatabase.CreateAsset(duplicate, AssetDatabase.GenerateUniqueAssetPath(assetPath + ".mat"));
  44. // Assign duplicate Material to selected object (if one is)
  45. if (Selection.activeGameObject != null)
  46. {
  47. TMP_Text textObject = Selection.activeGameObject.GetComponent<TMP_Text>();
  48. if (textObject != null)
  49. {
  50. textObject.fontSharedMaterial = duplicate;
  51. }
  52. else
  53. {
  54. TMP_SubMesh subMeshObject = Selection.activeGameObject.GetComponent<TMP_SubMesh>();
  55. if (subMeshObject != null)
  56. subMeshObject.sharedMaterial = duplicate;
  57. else
  58. {
  59. TMP_SubMeshUI subMeshUIObject = Selection.activeGameObject.GetComponent<TMP_SubMeshUI>();
  60. if (subMeshUIObject != null)
  61. subMeshUIObject.sharedMaterial = duplicate;
  62. }
  63. }
  64. }
  65. // Ping newly created Material Preset.
  66. EditorUtility.FocusProjectWindow();
  67. EditorGUIUtility.PingObject(duplicate);
  68. }
  69. //[MenuItem("CONTEXT/MaterialComponent/Copy Material Properties", false)]
  70. [MenuItem("CONTEXT/Material/Copy Material Properties", false)]
  71. static void CopyMaterialProperties(MenuCommand command)
  72. {
  73. Material mat = null;
  74. if (command.context.GetType() == typeof(Material))
  75. mat = (Material)command.context;
  76. else
  77. {
  78. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  79. }
  80. m_copiedProperties = new Material(mat);
  81. m_copiedProperties.shaderKeywords = mat.shaderKeywords;
  82. m_copiedProperties.hideFlags = HideFlags.DontSave;
  83. }
  84. // PASTE MATERIAL
  85. //[MenuItem("CONTEXT/MaterialComponent/Paste Material Properties", false)]
  86. [MenuItem("CONTEXT/Material/Paste Material Properties", false)]
  87. static void PasteMaterialProperties(MenuCommand command)
  88. {
  89. if (m_copiedProperties == null)
  90. {
  91. Debug.LogWarning("No Material Properties to Paste. Use Copy Material Properties first.");
  92. return;
  93. }
  94. Material mat = null;
  95. if (command.context.GetType() == typeof(Material))
  96. mat = (Material)command.context;
  97. else
  98. {
  99. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  100. }
  101. Undo.RecordObject(mat, "Paste Material");
  102. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  103. if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
  104. {
  105. // Preserve unique SDF properties from destination material.
  106. m_copiedProperties.SetTexture(ShaderUtilities.ID_MainTex, mat.GetTexture(ShaderUtilities.ID_MainTex));
  107. m_copiedProperties.SetFloat(ShaderUtilities.ID_GradientScale, mat.GetFloat(ShaderUtilities.ID_GradientScale));
  108. m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureWidth, mat.GetFloat(ShaderUtilities.ID_TextureWidth));
  109. m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureHeight, mat.GetFloat(ShaderUtilities.ID_TextureHeight));
  110. }
  111. EditorShaderUtilities.CopyMaterialProperties(m_copiedProperties, mat);
  112. // Copy ShaderKeywords from one material to the other.
  113. mat.shaderKeywords = m_copiedProperties.shaderKeywords;
  114. // Let TextMeshPro Objects that this mat has changed.
  115. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  116. }
  117. // Enable Resetting of Material properties without losing unique properties of the font atlas.
  118. [MenuItem("CONTEXT/Material/Reset", false, 2100)]
  119. static void ResetSettings(MenuCommand command)
  120. {
  121. Material mat = null;
  122. if (command.context.GetType() == typeof(Material))
  123. mat = (Material)command.context;
  124. else
  125. {
  126. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  127. }
  128. Undo.RecordObject(mat, "Reset Material");
  129. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  130. if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
  131. {
  132. // Copy unique properties of the SDF Material
  133. var texture = mat.GetTexture(ShaderUtilities.ID_MainTex);
  134. var gradientScale = mat.GetFloat(ShaderUtilities.ID_GradientScale);
  135. var texWidth = mat.GetFloat(ShaderUtilities.ID_TextureWidth);
  136. var texHeight = mat.GetFloat(ShaderUtilities.ID_TextureHeight);
  137. var stencilId = 0.0f;
  138. var stencilComp = 0.0f;
  139. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  140. {
  141. stencilId = mat.GetFloat(ShaderUtilities.ID_StencilID);
  142. stencilComp = mat.GetFloat(ShaderUtilities.ID_StencilComp);
  143. }
  144. var normalWeight = mat.GetFloat(ShaderUtilities.ID_WeightNormal);
  145. var boldWeight = mat.GetFloat(ShaderUtilities.ID_WeightBold);
  146. // Reset the material
  147. Unsupported.SmartReset(mat);
  148. // Reset ShaderKeywords
  149. mat.shaderKeywords = new string[0]; // { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
  150. // Copy unique material properties back to the material.
  151. mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
  152. mat.SetFloat(ShaderUtilities.ID_GradientScale, gradientScale);
  153. mat.SetFloat(ShaderUtilities.ID_TextureWidth, texWidth);
  154. mat.SetFloat(ShaderUtilities.ID_TextureHeight, texHeight);
  155. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  156. {
  157. mat.SetFloat(ShaderUtilities.ID_StencilID, stencilId);
  158. mat.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  159. }
  160. mat.SetFloat(ShaderUtilities.ID_WeightNormal, normalWeight);
  161. mat.SetFloat(ShaderUtilities.ID_WeightBold, boldWeight);
  162. }
  163. else
  164. {
  165. Unsupported.SmartReset(mat);
  166. }
  167. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  168. }
  169. //This function is used for debugging and fixing potentially broken font atlas links.
  170. [MenuItem("CONTEXT/Material/Copy Atlas", false, 2000)]
  171. static void CopyAtlas(MenuCommand command)
  172. {
  173. Material mat = command.context as Material;
  174. m_copiedAtlasProperties = new Material(mat);
  175. m_copiedAtlasProperties.hideFlags = HideFlags.DontSave;
  176. }
  177. // This function is used for debugging and fixing potentially broken font atlas links
  178. [MenuItem("CONTEXT/Material/Paste Atlas", false, 2001)]
  179. static void PasteAtlas(MenuCommand command)
  180. {
  181. Material mat = command.context as Material;
  182. if (m_copiedAtlasProperties != null)
  183. {
  184. Undo.RecordObject(mat, "Paste Texture");
  185. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  186. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedAtlasProperties.GetTexture(ShaderUtilities.ID_MainTex));
  187. mat.SetFloat(ShaderUtilities.ID_GradientScale, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_GradientScale));
  188. mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureWidth));
  189. mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureHeight));
  190. }
  191. else if (m_copiedTexture != null)
  192. {
  193. Undo.RecordObject(mat, "Paste Texture");
  194. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedTexture);
  195. }
  196. //DestroyImmediate(m_copiedAtlasProperties);
  197. }
  198. // Context Menus for TMPro Font Assets
  199. //This function is used for debugging and fixing potentially broken font atlas links.
  200. [MenuItem("CONTEXT/TMP_FontAsset/Extract Atlas", false, 2100)]
  201. static void ExtractAtlas(MenuCommand command)
  202. {
  203. TMP_FontAsset font = command.context as TMP_FontAsset;
  204. string fontPath = AssetDatabase.GetAssetPath(font);
  205. string texPath = Path.GetDirectoryName(fontPath) + "/" + Path.GetFileNameWithoutExtension(fontPath) + " Atlas.png";
  206. // Create a Serialized Object of the texture to allow us to make it readable.
  207. SerializedObject texprop = new SerializedObject(font.material.GetTexture(ShaderUtilities.ID_MainTex));
  208. texprop.FindProperty("m_IsReadable").boolValue = true;
  209. texprop.ApplyModifiedProperties();
  210. // Create a copy of the texture.
  211. Texture2D tex = Instantiate(font.material.GetTexture(ShaderUtilities.ID_MainTex)) as Texture2D;
  212. // Set the texture to not readable again.
  213. texprop.FindProperty("m_IsReadable").boolValue = false;
  214. texprop.ApplyModifiedProperties();
  215. Debug.Log(texPath);
  216. // Saving File for Debug
  217. var pngData = tex.EncodeToPNG();
  218. File.WriteAllBytes(texPath, pngData);
  219. AssetDatabase.Refresh();
  220. DestroyImmediate(tex);
  221. }
  222. /// <summary>
  223. ///
  224. /// </summary>
  225. /// <param name="command"></param>
  226. [MenuItem("CONTEXT/TMP_FontAsset/Update Atlas Texture...", false, 2000)]
  227. static void RegenerateFontAsset(MenuCommand command)
  228. {
  229. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  230. if (fontAsset != null)
  231. {
  232. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(fontAsset);
  233. }
  234. }
  235. /// <summary>
  236. /// Clear Font Asset Data
  237. /// </summary>
  238. /// <param name="command"></param>
  239. [MenuItem("CONTEXT/TMP_FontAsset/Reset", false, 100)]
  240. static void ClearFontAssetData(MenuCommand command)
  241. {
  242. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  243. if (fontAsset != null && Selection.activeObject != fontAsset)
  244. {
  245. Selection.activeObject = fontAsset;
  246. }
  247. fontAsset.ClearFontAssetData(true);
  248. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  249. }
  250. [MenuItem("CONTEXT/TrueTypeFontImporter/Create TMP Font Asset...", false, 200)]
  251. static void CreateFontAsset(MenuCommand command)
  252. {
  253. TrueTypeFontImporter importer = command.context as TrueTypeFontImporter;
  254. if (importer != null)
  255. {
  256. Font sourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(importer.assetPath);
  257. if (sourceFontFile)
  258. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(sourceFontFile);
  259. }
  260. }
  261. }
  262. }