No Description
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.

TMP_EditorUtility.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Text;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. namespace TMPro.EditorUtilities
  8. {
  9. public static class TMP_EditorUtility
  10. {
  11. /// <summary>
  12. /// Returns the relative path of the package.
  13. /// </summary>
  14. public static string packageRelativePath
  15. {
  16. get
  17. {
  18. if (string.IsNullOrEmpty(m_PackagePath))
  19. m_PackagePath = GetPackageRelativePath();
  20. return m_PackagePath;
  21. }
  22. }
  23. [SerializeField]
  24. private static string m_PackagePath;
  25. /// <summary>
  26. /// Returns the fully qualified path of the package.
  27. /// </summary>
  28. public static string packageFullPath
  29. {
  30. get
  31. {
  32. if (string.IsNullOrEmpty(m_PackageFullPath))
  33. m_PackageFullPath = GetPackageFullPath();
  34. return m_PackageFullPath;
  35. }
  36. }
  37. [SerializeField]
  38. private static string m_PackageFullPath;
  39. // Static Fields Related to locating the TextMesh Pro Asset
  40. private static string folderPath = "Not Found";
  41. private static EditorWindow Gameview;
  42. private static bool isInitialized = false;
  43. private static void GetGameview()
  44. {
  45. System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
  46. System.Type type = assembly.GetType("UnityEditor.GameView");
  47. Gameview = EditorWindow.GetWindow(type);
  48. }
  49. internal static void RepaintAll()
  50. {
  51. if (isInitialized == false)
  52. {
  53. GetGameview();
  54. isInitialized = true;
  55. }
  56. SceneView.RepaintAll();
  57. Gameview.Repaint();
  58. }
  59. /// <summary>
  60. /// Create and return a new asset in a smart location based on the current selection and then select it.
  61. /// </summary>
  62. /// <param name="name">
  63. /// Name of the new asset. Do not include the .asset extension.
  64. /// </param>
  65. /// <returns>
  66. /// The new asset.
  67. /// </returns>
  68. internal static T CreateAsset<T>(string name) where T : ScriptableObject
  69. {
  70. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  71. if (path.Length == 0)
  72. {
  73. // no asset selected, place in asset root
  74. path = "Assets/" + name + ".asset";
  75. }
  76. else if (Directory.Exists(path))
  77. {
  78. // place in currently selected directory
  79. path += "/" + name + ".asset";
  80. }
  81. else {
  82. // place in current selection's containing directory
  83. path = Path.GetDirectoryName(path) + "/" + name + ".asset";
  84. }
  85. T asset = ScriptableObject.CreateInstance<T>();
  86. AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath(path));
  87. EditorUtility.FocusProjectWindow();
  88. Selection.activeObject = asset;
  89. return asset;
  90. }
  91. // Function used to find all materials which reference a font atlas so we can update all their references.
  92. internal static Material[] FindMaterialReferences(TMP_FontAsset fontAsset)
  93. {
  94. List<Material> refs = new List<Material>();
  95. Material mat = fontAsset.material;
  96. refs.Add(mat);
  97. // Get materials matching the search pattern.
  98. string searchPattern = "t:Material" + " " + fontAsset.name.Split(new char[] { ' ' })[0];
  99. string[] materialAssetGUIDs = AssetDatabase.FindAssets(searchPattern);
  100. for (int i = 0; i < materialAssetGUIDs.Length; i++)
  101. {
  102. string materialPath = AssetDatabase.GUIDToAssetPath(materialAssetGUIDs[i]);
  103. Material targetMaterial = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
  104. if (targetMaterial.HasProperty(ShaderUtilities.ID_MainTex) && targetMaterial.GetTexture(ShaderUtilities.ID_MainTex) != null && mat.GetTexture(ShaderUtilities.ID_MainTex) != null && targetMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID() == mat.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  105. {
  106. if (!refs.Contains(targetMaterial))
  107. refs.Add(targetMaterial);
  108. }
  109. else
  110. {
  111. // TODO: Find a more efficient method to unload resources.
  112. //Resources.UnloadAsset(targetMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  113. }
  114. }
  115. return refs.ToArray();
  116. }
  117. // Function used to find the Font Asset which matches the given Material Preset and Font Atlas Texture.
  118. internal static TMP_FontAsset FindMatchingFontAsset(Material mat)
  119. {
  120. if (mat.GetTexture(ShaderUtilities.ID_MainTex) == null) return null;
  121. // Find the dependent assets of this material.
  122. string[] dependentAssets = AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(mat), false);
  123. for (int i = 0; i < dependentAssets.Length; i++)
  124. {
  125. TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(dependentAssets[i]);
  126. if (fontAsset != null)
  127. return fontAsset;
  128. }
  129. return null;
  130. }
  131. private static string GetPackageRelativePath()
  132. {
  133. // Check for potential UPM package
  134. string packagePath = Path.GetFullPath("Packages/com.unity.ugui");
  135. if (Directory.Exists(packagePath))
  136. {
  137. return "Packages/com.unity.ugui";
  138. }
  139. packagePath = Path.GetFullPath("Assets/..");
  140. if (Directory.Exists(packagePath))
  141. {
  142. // Search default location for development package
  143. if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.ugui/Editor Resources"))
  144. {
  145. return "Assets/Packages/com.unity.ugui";
  146. }
  147. // Search for potential alternative locations in the user project
  148. string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
  149. packagePath = ValidateLocation(matchingPaths, packagePath);
  150. if (packagePath != null) return packagePath;
  151. }
  152. return null;
  153. }
  154. private static string GetPackageFullPath()
  155. {
  156. // Check for potential UPM package
  157. string packagePath = Path.GetFullPath("Packages/com.unity.ugui");
  158. if (Directory.Exists(packagePath))
  159. {
  160. return packagePath;
  161. }
  162. packagePath = Path.GetFullPath("Assets/..");
  163. if (Directory.Exists(packagePath))
  164. {
  165. // Search default location for development package
  166. if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.ugui/Editor Resources"))
  167. {
  168. return packagePath + "/Assets/Packages/com.unity.ugui";
  169. }
  170. // Search for potential alternative locations in the user project
  171. string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
  172. string path = ValidateLocation(matchingPaths, packagePath);
  173. if (path != null) return packagePath + path;
  174. }
  175. return null;
  176. }
  177. /// <summary>
  178. /// Method to validate the location of the asset folder by making sure the GUISkins folder exists.
  179. /// </summary>
  180. /// <param name="paths"></param>
  181. /// <returns></returns>
  182. private static string ValidateLocation(string[] paths, string projectPath)
  183. {
  184. for (int i = 0; i < paths.Length; i++)
  185. {
  186. // Check if any of the matching directories contain a GUISkins directory.
  187. if (Directory.Exists(paths[i] + "/Editor Resources"))
  188. {
  189. folderPath = paths[i].Replace(projectPath, "");
  190. folderPath = folderPath.TrimStart('\\', '/');
  191. return folderPath;
  192. }
  193. }
  194. return null;
  195. }
  196. /// <summary>
  197. /// Function which returns a string containing a sequence of Decimal character ranges.
  198. /// </summary>
  199. /// <param name="characterSet"></param>
  200. /// <returns></returns>
  201. internal static string GetDecimalCharacterSequence(int[] characterSet)
  202. {
  203. if (characterSet == null || characterSet.Length == 0)
  204. return string.Empty;
  205. string characterSequence = string.Empty;
  206. int count = characterSet.Length;
  207. int first = characterSet[0];
  208. int last = first;
  209. for (int i = 1; i < count; i++)
  210. {
  211. if (characterSet[i - 1] + 1 == characterSet[i])
  212. {
  213. last = characterSet[i];
  214. }
  215. else
  216. {
  217. if (first == last)
  218. characterSequence += first + ",";
  219. else
  220. characterSequence += first + "-" + last + ",";
  221. first = last = characterSet[i];
  222. }
  223. }
  224. // handle the final group
  225. if (first == last)
  226. characterSequence += first;
  227. else
  228. characterSequence += first + "-" + last;
  229. return characterSequence;
  230. }
  231. /// <summary>
  232. /// Function which returns a string containing a sequence of Unicode (Hex) character ranges.
  233. /// </summary>
  234. /// <param name="characterSet"></param>
  235. /// <returns></returns>
  236. internal static string GetUnicodeCharacterSequence(int[] characterSet)
  237. {
  238. if (characterSet == null || characterSet.Length == 0)
  239. return string.Empty;
  240. string characterSequence = string.Empty;
  241. int count = characterSet.Length;
  242. int first = characterSet[0];
  243. int last = first;
  244. for (int i = 1; i < count; i++)
  245. {
  246. if (characterSet[i - 1] + 1 == characterSet[i])
  247. {
  248. last = characterSet[i];
  249. }
  250. else
  251. {
  252. if (first == last)
  253. characterSequence += first.ToString("X2") + ",";
  254. else
  255. characterSequence += first.ToString("X2") + "-" + last.ToString("X2") + ",";
  256. first = last = characterSet[i];
  257. }
  258. }
  259. // handle the final group
  260. if (first == last)
  261. characterSequence += first.ToString("X2");
  262. else
  263. characterSequence += first.ToString("X2") + "-" + last.ToString("X2");
  264. return characterSequence;
  265. }
  266. /// <summary>
  267. ///
  268. /// </summary>
  269. /// <param name="rect"></param>
  270. /// <param name="thickness"></param>
  271. /// <param name="color"></param>
  272. internal static void DrawBox(Rect rect, float thickness, Color color)
  273. {
  274. EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + thickness, rect.width + thickness * 2, thickness), color);
  275. EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + thickness, thickness, rect.height - thickness * 2), color);
  276. EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + rect.height - thickness * 2, rect.width + thickness * 2, thickness), color);
  277. EditorGUI.DrawRect(new Rect(rect.x + rect.width, rect.y + thickness, thickness, rect.height - thickness * 2), color);
  278. }
  279. /// <summary>
  280. /// Function to return the horizontal alignment grid value.
  281. /// </summary>
  282. /// <param name="value"></param>
  283. /// <returns></returns>
  284. internal static int GetHorizontalAlignmentGridValue(int value)
  285. {
  286. if ((value & 0x1) == 0x1)
  287. return 0;
  288. else if ((value & 0x2) == 0x2)
  289. return 1;
  290. else if ((value & 0x4) == 0x4)
  291. return 2;
  292. else if ((value & 0x8) == 0x8)
  293. return 3;
  294. else if ((value & 0x10) == 0x10)
  295. return 4;
  296. else if ((value & 0x20) == 0x20)
  297. return 5;
  298. return 0;
  299. }
  300. /// <summary>
  301. /// Function to return the vertical alignment grid value.
  302. /// </summary>
  303. /// <param name="value"></param>
  304. /// <returns></returns>
  305. internal static int GetVerticalAlignmentGridValue(int value)
  306. {
  307. if ((value & 0x100) == 0x100)
  308. return 0;
  309. if ((value & 0x200) == 0x200)
  310. return 1;
  311. if ((value & 0x400) == 0x400)
  312. return 2;
  313. if ((value & 0x800) == 0x800)
  314. return 3;
  315. if ((value & 0x1000) == 0x1000)
  316. return 4;
  317. if ((value & 0x2000) == 0x2000)
  318. return 5;
  319. return 0;
  320. }
  321. internal static void DrawColorProperty(Rect rect, SerializedProperty property)
  322. {
  323. int oldIndent = EditorGUI.indentLevel;
  324. EditorGUI.indentLevel = 0;
  325. if (EditorGUIUtility.wideMode)
  326. {
  327. EditorGUI.PropertyField(new Rect(rect.x, rect.y, 50f, rect.height), property, GUIContent.none);
  328. rect.x += 50f;
  329. rect.width = Mathf.Min(100f, rect.width - 55f);
  330. }
  331. else
  332. {
  333. rect.height /= 2f;
  334. rect.width = Mathf.Min(100f, rect.width - 5f);
  335. EditorGUI.PropertyField(rect, property, GUIContent.none);
  336. rect.y += rect.height;
  337. }
  338. EditorGUI.BeginChangeCheck();
  339. string colorString = EditorGUI.TextField(rect, string.Format("#{0}", ColorUtility.ToHtmlStringRGBA(property.colorValue)));
  340. if (EditorGUI.EndChangeCheck())
  341. {
  342. Color color;
  343. if (ColorUtility.TryParseHtmlString(colorString, out color))
  344. {
  345. property.colorValue = color;
  346. }
  347. }
  348. EditorGUI.indentLevel = oldIndent;
  349. }
  350. internal static bool EditorToggle(Rect position, bool value, GUIContent content, GUIStyle style)
  351. {
  352. var id = GUIUtility.GetControlID(content, FocusType.Keyboard, position);
  353. var evt = Event.current;
  354. // Toggle selected toggle on space or return key
  355. if (GUIUtility.keyboardControl == id && evt.type == EventType.KeyDown && (evt.keyCode == KeyCode.Space || evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter))
  356. {
  357. value = !value;
  358. evt.Use();
  359. GUI.changed = true;
  360. }
  361. if (evt.type == EventType.MouseDown && position.Contains(Event.current.mousePosition))
  362. {
  363. GUIUtility.keyboardControl = id;
  364. EditorGUIUtility.editingTextField = false;
  365. HandleUtility.Repaint();
  366. }
  367. return GUI.Toggle(position, id, value, content, style);
  368. }
  369. }
  370. }