暫無描述
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.

TMPro_ContextMenus.cs 15KB

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