暫無描述
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_PackageResourceImporter.cs 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace TMPro
  7. {
  8. [System.Serializable]
  9. public class TMP_PackageResourceImporter
  10. {
  11. bool m_EssentialResourcesImported;
  12. bool m_ExamplesAndExtrasResourcesImported;
  13. internal bool m_IsImportingExamples;
  14. public TMP_PackageResourceImporter() { }
  15. public void OnDestroy()
  16. {
  17. }
  18. public void OnGUI()
  19. {
  20. // Check if the resources state has changed.
  21. m_EssentialResourcesImported = File.Exists("Assets/TextMesh Pro/Resources/TMP Settings.asset");
  22. m_ExamplesAndExtrasResourcesImported = Directory.Exists("Assets/TextMesh Pro/Examples & Extras");
  23. GUILayout.BeginVertical();
  24. {
  25. // Display options to import Essential resources
  26. GUILayout.BeginVertical(EditorStyles.helpBox);
  27. {
  28. GUILayout.Label("TMP Essentials", EditorStyles.boldLabel);
  29. GUILayout.Label("This appears to be the first time you access TextMesh Pro, as such we need to add resources to your project that are essential for using TextMesh Pro. These new resources will be placed at the root of your project in the \"TextMesh Pro\" folder.", new GUIStyle(EditorStyles.label) { wordWrap = true } );
  30. GUILayout.Space(5f);
  31. GUI.enabled = !m_EssentialResourcesImported;
  32. if (GUILayout.Button("Import TMP Essentials"))
  33. {
  34. AssetDatabase.importPackageCompleted += ImportCallback;
  35. string packageFullPath = GetPackageFullPath();
  36. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Essential Resources.unitypackage", false);
  37. }
  38. GUILayout.Space(5f);
  39. GUI.enabled = true;
  40. }
  41. GUILayout.EndVertical();
  42. // Display options to import Examples & Extras
  43. GUILayout.BeginVertical(EditorStyles.helpBox);
  44. {
  45. GUILayout.Label("TMP Examples & Extras", EditorStyles.boldLabel);
  46. GUILayout.Label("The Examples & Extras package contains addition resources and examples that will make discovering and learning about TextMesh Pro's powerful features easier. These additional resources will be placed in the same folder as the TMP essential resources.", new GUIStyle(EditorStyles.label) { wordWrap = true });
  47. GUILayout.Space(5f);
  48. GUI.enabled = m_EssentialResourcesImported && !m_ExamplesAndExtrasResourcesImported;
  49. if (GUILayout.Button("Import TMP Examples & Extras"))
  50. {
  51. // Set flag to get around importing scripts as per of this package which results in an assembly reload which in turn prevents / clears any callbacks.
  52. m_IsImportingExamples = true;
  53. // Disable AssetDatabase refresh until examples have been imported.
  54. //AssetDatabase.DisallowAutoRefresh();
  55. string packageFullPath = GetPackageFullPath();
  56. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", false);
  57. }
  58. GUILayout.Space(5f);
  59. GUI.enabled = true;
  60. }
  61. GUILayout.EndVertical();
  62. }
  63. GUILayout.EndVertical();
  64. GUILayout.Space(5f);
  65. }
  66. internal void RegisterResourceImportCallback()
  67. {
  68. AssetDatabase.importPackageCompleted += ImportCallback;
  69. }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="packageName"></param>
  74. void ImportCallback(string packageName)
  75. {
  76. if (packageName == "TMP Essential Resources")
  77. {
  78. m_EssentialResourcesImported = true;
  79. TMPro_EventManager.ON_RESOURCES_LOADED();
  80. #if UNITY_2018_3_OR_NEWER
  81. SettingsService.NotifySettingsProviderChanged();
  82. #endif
  83. }
  84. else if (packageName == "TMP Examples & Extras")
  85. {
  86. m_ExamplesAndExtrasResourcesImported = true;
  87. m_IsImportingExamples = false;
  88. //AssetDatabase.AllowAutoRefresh();
  89. }
  90. Debug.Log("[" + packageName + "] have been imported.");
  91. AssetDatabase.importPackageCompleted -= ImportCallback;
  92. }
  93. static string GetPackageFullPath()
  94. {
  95. // Check for potential UPM package
  96. string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
  97. if (Directory.Exists(packagePath))
  98. {
  99. return packagePath;
  100. }
  101. packagePath = Path.GetFullPath("Assets/..");
  102. if (Directory.Exists(packagePath))
  103. {
  104. // Search default location for development package
  105. if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
  106. {
  107. return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
  108. }
  109. // Search for default location of normal TextMesh Pro AssetStore package
  110. if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
  111. {
  112. return packagePath + "/Assets/TextMesh Pro";
  113. }
  114. // Search for potential alternative locations in the user project
  115. string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
  116. string path = ValidateLocation(matchingPaths, packagePath);
  117. if (path != null) return packagePath + path;
  118. }
  119. return null;
  120. }
  121. static string ValidateLocation(string[] paths, string projectPath)
  122. {
  123. for (int i = 0; i < paths.Length; i++)
  124. {
  125. // Check if the Editor Resources folder exists.
  126. if (Directory.Exists(paths[i] + "/Editor Resources"))
  127. {
  128. string folderPath = paths[i].Replace(projectPath, "");
  129. folderPath = folderPath.TrimStart('\\', '/');
  130. return folderPath;
  131. }
  132. }
  133. return null;
  134. }
  135. /// <summary>
  136. /// Imports the specified TMP resources.
  137. /// </summary>
  138. /// <param name="importEssentials">Should import the TMP Essential Resources.</param>
  139. /// <param name="importExamples">Should import the TMP Examples & Extras.</param>
  140. /// <param name="interactive">If interactive is true, an import package dialog will be opened, else all assets in the package will be imported into the current project silently.</param>
  141. public static void ImportResources(bool importEssentials, bool importExamples, bool interactive)
  142. {
  143. string packageFullPath = GetPackageFullPath();
  144. if (importEssentials)
  145. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Essential Resources.unitypackage", interactive);
  146. if (importExamples)
  147. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", interactive);
  148. }
  149. }
  150. public class TMP_PackageResourceImporterWindow : EditorWindow
  151. {
  152. [SerializeField]
  153. TMP_PackageResourceImporter m_ResourceImporter;
  154. static TMP_PackageResourceImporterWindow m_ImporterWindow;
  155. public static void ShowPackageImporterWindow()
  156. {
  157. if (m_ImporterWindow == null)
  158. {
  159. m_ImporterWindow = GetWindow<TMP_PackageResourceImporterWindow>();
  160. m_ImporterWindow.titleContent = new GUIContent("TMP Importer");
  161. m_ImporterWindow.Focus();
  162. }
  163. }
  164. void OnEnable()
  165. {
  166. // Set Editor Window Size
  167. SetEditorWindowSize();
  168. if (m_ResourceImporter == null)
  169. m_ResourceImporter = new TMP_PackageResourceImporter();
  170. if (m_ResourceImporter.m_IsImportingExamples)
  171. m_ResourceImporter.RegisterResourceImportCallback();
  172. }
  173. void OnDestroy()
  174. {
  175. m_ResourceImporter.OnDestroy();
  176. }
  177. void OnGUI()
  178. {
  179. m_ResourceImporter.OnGUI();
  180. }
  181. void OnInspectorUpdate()
  182. {
  183. Repaint();
  184. }
  185. /// <summary>
  186. /// Limits the minimum size of the editor window.
  187. /// </summary>
  188. void SetEditorWindowSize()
  189. {
  190. EditorWindow editorWindow = this;
  191. Vector2 windowSize = new Vector2(640, 210);
  192. editorWindow.minSize = windowSize;
  193. editorWindow.maxSize = windowSize;
  194. }
  195. }
  196. }
  197. #endif