暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TMPro_ContextMenus.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. bool isSRPShader = mat.HasProperty(ShaderUtilities.ID_IsoPerimeter);
  153. // Copy unique properties of the SDF Material
  154. var texture = mat.GetTexture(ShaderUtilities.ID_MainTex);
  155. var gradientScale = mat.GetFloat(ShaderUtilities.ID_GradientScale);
  156. float texWidth = 0, texHeight = 0;
  157. float normalWeight = 0, boldWeight = 0;
  158. if (!isSRPShader)
  159. {
  160. texWidth = mat.GetFloat(ShaderUtilities.ID_TextureWidth);
  161. texHeight = mat.GetFloat(ShaderUtilities.ID_TextureHeight);
  162. normalWeight = mat.GetFloat(ShaderUtilities.ID_WeightNormal);
  163. boldWeight = mat.GetFloat(ShaderUtilities.ID_WeightBold);
  164. }
  165. var stencilId = 0.0f;
  166. var stencilComp = 0.0f;
  167. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  168. {
  169. stencilId = mat.GetFloat(ShaderUtilities.ID_StencilID);
  170. stencilComp = mat.GetFloat(ShaderUtilities.ID_StencilComp);
  171. }
  172. // Reset the material
  173. Unsupported.SmartReset(mat);
  174. // Reset ShaderKeywords
  175. mat.shaderKeywords = new string[0]; // { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
  176. // Copy unique material properties back to the material.
  177. mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
  178. mat.SetFloat(ShaderUtilities.ID_GradientScale, gradientScale);
  179. if (!isSRPShader)
  180. {
  181. mat.SetFloat(ShaderUtilities.ID_TextureWidth, texWidth);
  182. mat.SetFloat(ShaderUtilities.ID_TextureHeight, texHeight);
  183. mat.SetFloat(ShaderUtilities.ID_WeightNormal, normalWeight);
  184. mat.SetFloat(ShaderUtilities.ID_WeightBold, boldWeight);
  185. }
  186. if (mat.HasProperty(ShaderUtilities.ID_StencilID))
  187. {
  188. mat.SetFloat(ShaderUtilities.ID_StencilID, stencilId);
  189. mat.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  190. }
  191. }
  192. else
  193. {
  194. Unsupported.SmartReset(mat);
  195. }
  196. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
  197. }
  198. //This function is used for debugging and fixing potentially broken font atlas links.
  199. [MenuItem("CONTEXT/Material/Copy Atlas", false, 2000)]
  200. static void CopyAtlas(MenuCommand command)
  201. {
  202. Material mat = command.context as Material;
  203. m_copiedAtlasProperties = new Material(mat);
  204. m_copiedAtlasProperties.hideFlags = HideFlags.DontSave;
  205. }
  206. // This function is used for debugging and fixing potentially broken font atlas links
  207. [MenuItem("CONTEXT/Material/Paste Atlas", true, 2001)]
  208. static bool PasteAtlasValidate(MenuCommand command)
  209. {
  210. if (m_copiedAtlasProperties == null && m_copiedTexture == null)
  211. return false;
  212. return AssetDatabase.IsOpenForEdit(command.context);
  213. }
  214. [MenuItem("CONTEXT/Material/Paste Atlas", false, 2001)]
  215. static void PasteAtlas(MenuCommand command)
  216. {
  217. Material mat = command.context as Material;
  218. if (mat == null)
  219. return;
  220. if (m_copiedAtlasProperties != null)
  221. {
  222. Undo.RecordObject(mat, "Paste Texture");
  223. ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
  224. if (m_copiedAtlasProperties.HasProperty(ShaderUtilities.ID_MainTex))
  225. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedAtlasProperties.GetTexture(ShaderUtilities.ID_MainTex));
  226. if (m_copiedAtlasProperties.HasProperty(ShaderUtilities.ID_GradientScale))
  227. {
  228. mat.SetFloat(ShaderUtilities.ID_GradientScale, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_GradientScale));
  229. mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureWidth));
  230. mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureHeight));
  231. }
  232. }
  233. else if (m_copiedTexture != null)
  234. {
  235. Undo.RecordObject(mat, "Paste Texture");
  236. mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedTexture);
  237. }
  238. }
  239. #endif
  240. // Context Menus for TMPro Font Assets
  241. //This function is used for debugging and fixing potentially broken font atlas links.
  242. [MenuItem("CONTEXT/TMP_FontAsset/Extract Atlas", false, 2200)]
  243. static void ExtractAtlas(MenuCommand command)
  244. {
  245. TMP_FontAsset font = command.context as TMP_FontAsset;
  246. string fontPath = AssetDatabase.GetAssetPath(font);
  247. string texPath = Path.GetDirectoryName(fontPath) + "/" + Path.GetFileNameWithoutExtension(fontPath) + " Atlas.png";
  248. // Create a Serialized Object of the texture to allow us to make it readable.
  249. SerializedObject texprop = new SerializedObject(font.material.GetTexture(ShaderUtilities.ID_MainTex));
  250. texprop.FindProperty("m_IsReadable").boolValue = true;
  251. texprop.ApplyModifiedProperties();
  252. // Create a copy of the texture.
  253. Texture2D tex = Instantiate(font.material.GetTexture(ShaderUtilities.ID_MainTex)) as Texture2D;
  254. // Set the texture to not readable again.
  255. texprop.FindProperty("m_IsReadable").boolValue = false;
  256. texprop.ApplyModifiedProperties();
  257. Debug.Log(texPath);
  258. // Saving File for Debug
  259. var pngData = tex.EncodeToPNG();
  260. File.WriteAllBytes(texPath, pngData);
  261. AssetDatabase.Refresh();
  262. DestroyImmediate(tex);
  263. }
  264. /// <summary>
  265. ///
  266. /// </summary>
  267. /// <param name="command"></param>
  268. [MenuItem("CONTEXT/TMP_FontAsset/Update Atlas Texture...", false, 2000)]
  269. static void RegenerateFontAsset(MenuCommand command)
  270. {
  271. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  272. if (fontAsset != null)
  273. {
  274. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(fontAsset);
  275. }
  276. }
  277. /*[MenuItem("CONTEXT/TMP_FontAsset/Force Upgrade To Version 1.1.0...", false, 2020)]
  278. static void ForceFontAssetUpgrade(MenuCommand command)
  279. {
  280. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  281. if (fontAsset != null)
  282. {
  283. fontAsset.UpgradeFontAsset();
  284. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  285. }
  286. }*/
  287. /// <summary>
  288. /// Clear Dynamic Font Asset data such as glyph, character and font features.
  289. /// </summary>
  290. /// <param name="command"></param>
  291. [MenuItem("CONTEXT/TMP_FontAsset/Reset", true, 100)]
  292. static bool ClearFontAssetDataValidate(MenuCommand command)
  293. {
  294. return AssetDatabase.IsOpenForEdit(command.context);
  295. }
  296. [MenuItem("CONTEXT/TMP_FontAsset/Reset", false, 100)]
  297. static void ClearFontAssetData(MenuCommand command)
  298. {
  299. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  300. if (fontAsset == null)
  301. return;
  302. if (Selection.activeObject != fontAsset)
  303. Selection.activeObject = fontAsset;
  304. fontAsset.ClearFontAssetData(true);
  305. TMP_ResourceManager.RebuildFontAssetCache();
  306. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  307. }
  308. /// <summary>
  309. /// Clear Character and Glyph data (only).
  310. /// </summary>
  311. /// <param name="command"></param>
  312. [MenuItem("CONTEXT/TMP_FontAsset/Clear Dynamic Data", true, 2100)]
  313. static bool ClearFontCharacterDataValidate(MenuCommand command)
  314. {
  315. return AssetDatabase.IsOpenForEdit(command.context);
  316. }
  317. [MenuItem("CONTEXT/TMP_FontAsset/Clear Dynamic Data", false, 2100)]
  318. static void ClearFontCharacterData(MenuCommand command)
  319. {
  320. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  321. if (fontAsset == null)
  322. return;
  323. if (Selection.activeObject != fontAsset)
  324. Selection.activeObject = fontAsset;
  325. fontAsset.ClearCharacterAndGlyphTablesInternal();
  326. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  327. }
  328. /// <summary>
  329. /// Import all font features
  330. /// </summary>
  331. /// <param name="command"></param>
  332. #if TEXTCORE_FONT_ENGINE_1_5_OR_NEWER
  333. [MenuItem("CONTEXT/TMP_FontAsset/Import Font Features", true, 2110)]
  334. static bool ReimportFontFeaturesValidate(MenuCommand command)
  335. {
  336. return AssetDatabase.IsOpenForEdit(command.context);
  337. }
  338. [MenuItem("CONTEXT/TMP_FontAsset/Import Font Features", false, 2110)]
  339. static void ReimportFontFeatures(MenuCommand command)
  340. {
  341. TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
  342. if (fontAsset == null)
  343. return;
  344. if (Selection.activeObject != fontAsset)
  345. Selection.activeObject = fontAsset;
  346. fontAsset.ImportFontFeatures();
  347. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  348. }
  349. #endif
  350. /// <summary>
  351. ///
  352. /// </summary>
  353. /// <param name="command"></param>
  354. [MenuItem("CONTEXT/TrueTypeFontImporter/Create TMP Font Asset...", false, 200)]
  355. static void CreateFontAsset(MenuCommand command)
  356. {
  357. TrueTypeFontImporter importer = command.context as TrueTypeFontImporter;
  358. if (importer != null)
  359. {
  360. Font sourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(importer.assetPath);
  361. if (sourceFontFile)
  362. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(sourceFontFile);
  363. }
  364. }
  365. }
  366. }