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.

307 lines
12 KiB

  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/MaterialComponent/Reset", false, 2100)]
  119. [MenuItem("CONTEXT/Material/Reset", false, 2100)]
  120. static void ResetSettings(MenuCommand command)
  121. {
  122. Material mat = null;
  123. if (command.context.GetType() == typeof(Material))
  124. mat = (Material)command.context;
  125. else
  126. {
  127. mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
  128. }
  129. Undo.RecordObject(mat, "Reset Material");
  130. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  131. if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
  132. {
  133. // Copy unique properties of the SDF Material
  134. var texture = mat.GetTexture(ShaderUtilities.ID_MainTex);
  135. var gradientScale = mat.GetFloat(ShaderUtilities.ID_GradientScale);
  136. var texWidth = mat.GetFloat(ShaderUtilities.ID_TextureWidth);
  137. var texHeight = mat.GetFloat(ShaderUtilities.ID_TextureHeight);
  138. var stencilId = mat.GetFloat(ShaderUtilities.ID_StencilID);
  139. var stencilComp = mat.GetFloat(ShaderUtilities.ID_StencilComp);
  140. var normalWeight = mat.GetFloat(ShaderUtilities.ID_WeightNormal);
  141. var boldWeight = mat.GetFloat(ShaderUtilities.ID_WeightBold);
  142. // Reset the material
  143. Unsupported.SmartReset(mat);
  144. // Reset ShaderKeywords
  145. mat.shaderKeywords = new string[0]; // { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
  146. // Copy unique material properties back to the material.
  147. mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
  148. mat.SetFloat(ShaderUtilities.ID_GradientScale, gradientScale);
  149. mat.SetFloat(ShaderUtilities.ID_TextureWidth, texWidth);
  150. mat.SetFloat(ShaderUtilities.ID_TextureHeight, texHeight);
  151. mat.SetFloat(ShaderUtilities.ID_StencilID, stencilId);
  152. mat.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  153. mat.SetFloat(ShaderUtilities.ID_WeightNormal, normalWeight);
  154. mat.SetFloat(ShaderUtilities.ID_WeightBold, boldWeight);
  155. }
  156. else
  157. {
  158. Unsupported.SmartReset(mat);
  159. }
  160. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  161. }
  162. //This function is used for debugging and fixing potentially broken font atlas links.
  163. [MenuItem("CONTEXT/Material/Copy Atlas", false, 2000)]
  164. static void CopyAtlas(MenuCommand command)
  165. {
  166. Material mat = command.context as Material;
  167. m_copiedAtlasProperties = new Material(mat);
  168. m_copiedAtlasProperties.hideFlags = HideFlags.DontSave;
  169. }
  170. // This function is used for debugging and fixing potentially broken font atlas links
  171. [MenuItem("CONTEXT/Material/Paste Atlas", false, 2001)]
  172. static void PasteAtlas(MenuCommand command)
  173. {
  174. Material mat = command.context as Material;
  175. if (m_copiedAtlasProperties != null)
  176. {
  177. Undo.RecordObject(mat, "Paste Texture");
  178. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  179. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedAtlasProperties.GetTexture(ShaderUtilities.ID_MainTex));
  180. mat.SetFloat(ShaderUtilities.ID_GradientScale, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_GradientScale));
  181. mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureWidth));
  182. mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureHeight));
  183. }
  184. else if (m_copiedTexture != null)
  185. {
  186. Undo.RecordObject(mat, "Paste Texture");
  187. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedTexture);
  188. }
  189. //DestroyImmediate(m_copiedAtlasProperties);
  190. }
  191. // Context Menus for TMPro Font Assets
  192. //This function is used for debugging and fixing potentially broken font atlas links.
  193. [MenuItem("CONTEXT/TMP_FontAsset/Extract Atlas", false, 2100)]
  194. static void ExtractAtlas(MenuCommand command)
  195. {
  196. TMP_FontAsset font = command.context as TMP_FontAsset;
  197. string fontPath = AssetDatabase.GetAssetPath(font);
  198. string texPath = Path.GetDirectoryName(fontPath) + "/" + Path.GetFileNameWithoutExtension(fontPath) + " Atlas.png";
  199. // Create a Serialized Object of the texture to allow us to make it readable.
  200. SerializedObject texprop = new SerializedObject(font.material.GetTexture(ShaderUtilities.ID_MainTex));
  201. texprop.FindProperty("m_IsReadable").boolValue = true;
  202. texprop.ApplyModifiedProperties();
  203. // Create a copy of the texture.
  204. Texture2D tex = Instantiate(font.material.GetTexture(ShaderUtilities.ID_MainTex)) as Texture2D;
  205. // Set the texture to not readable again.
  206. texprop.FindProperty("m_IsReadable").boolValue = false;
  207. texprop.ApplyModifiedProperties();
  208. Debug.Log(texPath);
  209. // Saving File for Debug
  210. var pngData = tex.EncodeToPNG();
  211. File.WriteAllBytes(texPath, pngData);
  212. AssetDatabase.Refresh();
  213. DestroyImmediate(tex);
  214. }
  215. /// <summary>
  216. ///
  217. /// </summary>
  218. /// <param name="command"></param>
  219. [MenuItem("CONTEXT/TMP_FontAsset/Update Atlas Texture...", false, 2000)]
  220. static void RegenerateFontAsset(MenuCommand command)
  221. {
  222. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  223. if (fontAsset != null)
  224. {
  225. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(fontAsset);
  226. }
  227. }
  228. [MenuItem("CONTEXT/TrueTypeFontImporter/Create TMP Font Asset...", false, 200)]
  229. static void CreateFontAsset(MenuCommand command)
  230. {
  231. TrueTypeFontImporter importer = command.context as TrueTypeFontImporter;
  232. if (importer != null)
  233. {
  234. Font sourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(importer.assetPath);
  235. if (sourceFontFile)
  236. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(sourceFontFile);
  237. }
  238. }
  239. }
  240. }