TMPro_ContextMenus.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 persistent asset.");
  37. return;
  38. }
  39. string assetPath = AssetDatabase.GetAssetPath(source_Mat).Split('.')[0];
  40. if (assetPath.IndexOf("Assets/", System.StringComparison.InvariantCultureIgnoreCase) == -1)
  41. {
  42. Debug.LogWarning("Material Preset cannot be created from a material that is located outside the project.");
  43. return;
  44. }
  45. Material duplicate = new Material(source_Mat);
  46. // Need to manually copy the shader keywords
  47. duplicate.shaderKeywords = source_Mat.shaderKeywords;
  48. AssetDatabase.CreateAsset(duplicate, AssetDatabase.GenerateUniqueAssetPath(assetPath + ".mat"));
  49. GameObject[] selectedObjects = Selection.gameObjects;
  50. // Assign new Material Preset to selected text objects.
  51. for (int i = 0; i < selectedObjects.Length; i++)
  52. {
  53. TMP_Text textObject = selectedObjects[i].GetComponent<TMP_Text>();
  54. if (textObject != null)
  55. {
  56. textObject.fontSharedMaterial = duplicate;
  57. }
  58. else
  59. {
  60. TMP_SubMesh subMeshObject = selectedObjects[i].GetComponent<TMP_SubMesh>();
  61. if (subMeshObject != null)
  62. subMeshObject.sharedMaterial = duplicate;
  63. else
  64. {
  65. TMP_SubMeshUI subMeshUIObject = selectedObjects[i].GetComponent<TMP_SubMeshUI>();
  66. if (subMeshUIObject != null)
  67. subMeshUIObject.sharedMaterial = duplicate;
  68. }
  69. }
  70. }
  71. // Ping newly created Material Preset.
  72. EditorUtility.FocusProjectWindow();
  73. EditorGUIUtility.PingObject(duplicate);
  74. }
  75. // COPY MATERIAL PROPERTIES
  76. [MenuItem("CONTEXT/Material/Copy Material Properties", false)]
  77. static void CopyMaterialProperties(MenuCommand command)
  78. {
  79. Material mat = null;
  80. if (command.context.GetType() == typeof(Material))
  81. mat = (Material)command.context;
  82. else
  83. {
  84. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  85. }
  86. m_copiedProperties = new Material(mat);
  87. m_copiedProperties.shaderKeywords = mat.shaderKeywords;
  88. m_copiedProperties.hideFlags = HideFlags.DontSave;
  89. }
  90. // PASTE MATERIAL
  91. //[MenuItem("CONTEXT/MaterialComponent/Paste Material Properties", false)]
  92. [MenuItem("CONTEXT/Material/Paste Material Properties", false)]
  93. static void PasteMaterialProperties(MenuCommand command)
  94. {
  95. if (m_copiedProperties == null)
  96. {
  97. Debug.LogWarning("No Material Properties to Paste. Use Copy Material Properties first.");
  98. return;
  99. }
  100. Material mat = null;
  101. if (command.context.GetType() == typeof(Material))
  102. mat = (Material)command.context;
  103. else
  104. {
  105. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  106. }
  107. Undo.RecordObject(mat, "Paste Material");
  108. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  109. if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
  110. {
  111. // Preserve unique SDF properties from destination material.
  112. m_copiedProperties.SetTexture(ShaderUtilities.ID_MainTex, mat.GetTexture(ShaderUtilities.ID_MainTex));
  113. m_copiedProperties.SetFloat(ShaderUtilities.ID_GradientScale, mat.GetFloat(ShaderUtilities.ID_GradientScale));
  114. m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureWidth, mat.GetFloat(ShaderUtilities.ID_TextureWidth));
  115. m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureHeight, mat.GetFloat(ShaderUtilities.ID_TextureHeight));
  116. }
  117. EditorShaderUtilities.CopyMaterialProperties(m_copiedProperties, mat);
  118. // Copy ShaderKeywords from one material to the other.
  119. mat.shaderKeywords = m_copiedProperties.shaderKeywords;
  120. // Let TextMeshPro Objects that this mat has changed.
  121. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  122. }
  123. // Enable Resetting of Material properties without losing unique properties of the font atlas.
  124. [MenuItem("CONTEXT/Material/Reset", false, 2100)]
  125. static void ResetSettings(MenuCommand command)
  126. {
  127. Material mat = null;
  128. if (command.context.GetType() == typeof(Material))
  129. mat = (Material)command.context;
  130. else
  131. {
  132. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  133. }
  134. Undo.RecordObject(mat, "Reset Material");
  135. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  136. if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
  137. {
  138. // Copy unique properties of the SDF Material
  139. var texture = mat.GetTexture(ShaderUtilities.ID_MainTex);
  140. var gradientScale = mat.GetFloat(ShaderUtilities.ID_GradientScale);
  141. var texWidth = mat.GetFloat(ShaderUtilities.ID_TextureWidth);
  142. var texHeight = mat.GetFloat(ShaderUtilities.ID_TextureHeight);
  143. var stencilId = 0.0f;
  144. var stencilComp = 0.0f;
  145. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  146. {
  147. stencilId = mat.GetFloat(ShaderUtilities.ID_StencilID);
  148. stencilComp = mat.GetFloat(ShaderUtilities.ID_StencilComp);
  149. }
  150. var normalWeight = mat.GetFloat(ShaderUtilities.ID_WeightNormal);
  151. var boldWeight = mat.GetFloat(ShaderUtilities.ID_WeightBold);
  152. // Reset the material
  153. Unsupported.SmartReset(mat);
  154. // Reset ShaderKeywords
  155. mat.shaderKeywords = new string[0]; // { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
  156. // Copy unique material properties back to the material.
  157. mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
  158. mat.SetFloat(ShaderUtilities.ID_GradientScale, gradientScale);
  159. mat.SetFloat(ShaderUtilities.ID_TextureWidth, texWidth);
  160. mat.SetFloat(ShaderUtilities.ID_TextureHeight, texHeight);
  161. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  162. {
  163. mat.SetFloat(ShaderUtilities.ID_StencilID, stencilId);
  164. mat.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  165. }
  166. mat.SetFloat(ShaderUtilities.ID_WeightNormal, normalWeight);
  167. mat.SetFloat(ShaderUtilities.ID_WeightBold, boldWeight);
  168. }
  169. else
  170. {
  171. Unsupported.SmartReset(mat);
  172. }
  173. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  174. }
  175. //This function is used for debugging and fixing potentially broken font atlas links.
  176. [MenuItem("CONTEXT/Material/Copy Atlas", false, 2000)]
  177. static void CopyAtlas(MenuCommand command)
  178. {
  179. Material mat = command.context as Material;
  180. m_copiedAtlasProperties = new Material(mat);
  181. m_copiedAtlasProperties.hideFlags = HideFlags.DontSave;
  182. }
  183. // This function is used for debugging and fixing potentially broken font atlas links
  184. [MenuItem("CONTEXT/Material/Paste Atlas", false, 2001)]
  185. static void PasteAtlas(MenuCommand command)
  186. {
  187. Material mat = command.context as Material;
  188. if (mat == null)
  189. return;
  190. if (m_copiedAtlasProperties != null)
  191. {
  192. Undo.RecordObject(mat, "Paste Texture");
  193. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  194. if (m_copiedAtlasProperties.HasProperty(ShaderUtilities.ID_MainTex))
  195. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedAtlasProperties.GetTexture(ShaderUtilities.ID_MainTex));
  196. if (m_copiedAtlasProperties.HasProperty(ShaderUtilities.ID_GradientScale))
  197. {
  198. mat.SetFloat(ShaderUtilities.ID_GradientScale, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_GradientScale));
  199. mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureWidth));
  200. mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureHeight));
  201. }
  202. }
  203. else if (m_copiedTexture != null)
  204. {
  205. Undo.RecordObject(mat, "Paste Texture");
  206. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedTexture);
  207. }
  208. //DestroyImmediate(m_copiedAtlasProperties);
  209. }
  210. // Context Menus for TMPro Font Assets
  211. //This function is used for debugging and fixing potentially broken font atlas links.
  212. [MenuItem("CONTEXT/TMP_FontAsset/Extract Atlas", false, 2100)]
  213. static void ExtractAtlas(MenuCommand command)
  214. {
  215. TMP_FontAsset font = command.context as TMP_FontAsset;
  216. string fontPath = AssetDatabase.GetAssetPath(font);
  217. string texPath = Path.GetDirectoryName(fontPath) + "/" + Path.GetFileNameWithoutExtension(fontPath) + " Atlas.png";
  218. // Create a Serialized Object of the texture to allow us to make it readable.
  219. SerializedObject texprop = new SerializedObject(font.material.GetTexture(ShaderUtilities.ID_MainTex));
  220. texprop.FindProperty("m_IsReadable").boolValue = true;
  221. texprop.ApplyModifiedProperties();
  222. // Create a copy of the texture.
  223. Texture2D tex = Instantiate(font.material.GetTexture(ShaderUtilities.ID_MainTex)) as Texture2D;
  224. // Set the texture to not readable again.
  225. texprop.FindProperty("m_IsReadable").boolValue = false;
  226. texprop.ApplyModifiedProperties();
  227. Debug.Log(texPath);
  228. // Saving File for Debug
  229. var pngData = tex.EncodeToPNG();
  230. File.WriteAllBytes(texPath, pngData);
  231. AssetDatabase.Refresh();
  232. DestroyImmediate(tex);
  233. }
  234. /// <summary>
  235. ///
  236. /// </summary>
  237. /// <param name="command"></param>
  238. [MenuItem("CONTEXT/TMP_FontAsset/Update Atlas Texture...", false, 2000)]
  239. static void RegenerateFontAsset(MenuCommand command)
  240. {
  241. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  242. if (fontAsset != null)
  243. {
  244. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(fontAsset);
  245. }
  246. }
  247. [MenuItem("CONTEXT/TMP_FontAsset/Force Upgrade To Version 1.1.0...", false, 2010)]
  248. static void ForceFontAssetUpgrade(MenuCommand command)
  249. {
  250. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  251. if (fontAsset != null)
  252. {
  253. fontAsset.UpgradeFontAsset();
  254. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  255. }
  256. }
  257. /// <summary>
  258. /// Clear Dynamic Font Asset data such as glyph, character and font features.
  259. /// </summary>
  260. /// <param name="command"></param>
  261. [MenuItem("CONTEXT/TMP_FontAsset/Reset", false, 100)]
  262. static void ClearFontAssetData(MenuCommand command)
  263. {
  264. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  265. if (fontAsset != null && Selection.activeObject != fontAsset)
  266. {
  267. Selection.activeObject = fontAsset;
  268. }
  269. fontAsset.ClearFontAssetData(true);
  270. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  271. }
  272. [MenuItem("CONTEXT/TrueTypeFontImporter/Create TMP Font Asset...", false, 200)]
  273. static void CreateFontAsset(MenuCommand command)
  274. {
  275. TrueTypeFontImporter importer = command.context as TrueTypeFontImporter;
  276. if (importer != null)
  277. {
  278. Font sourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(importer.assetPath);
  279. if (sourceFontFile)
  280. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(sourceFontFile);
  281. }
  282. }
  283. }
  284. }