TMP_PackageResourceImporter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. var 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. }
  136. public class TMP_PackageResourceImporterWindow : EditorWindow
  137. {
  138. [SerializeField]
  139. TMP_PackageResourceImporter m_ResourceImporter;
  140. static TMP_PackageResourceImporterWindow m_ImporterWindow;
  141. public static void ShowPackageImporterWindow()
  142. {
  143. if (m_ImporterWindow == null)
  144. {
  145. m_ImporterWindow = GetWindow<TMP_PackageResourceImporterWindow>();
  146. m_ImporterWindow.titleContent = new GUIContent("TMP Importer");
  147. }
  148. m_ImporterWindow.Focus();
  149. }
  150. void OnEnable()
  151. {
  152. // Set Editor Window Size
  153. SetEditorWindowSize();
  154. if (m_ResourceImporter == null)
  155. m_ResourceImporter = new TMP_PackageResourceImporter();
  156. if (m_ResourceImporter.m_IsImportingExamples)
  157. m_ResourceImporter.RegisterResourceImportCallback();
  158. }
  159. void OnDestroy()
  160. {
  161. m_ResourceImporter.OnDestroy();
  162. }
  163. void OnGUI()
  164. {
  165. m_ResourceImporter.OnGUI();
  166. }
  167. void OnInspectorUpdate()
  168. {
  169. Repaint();
  170. }
  171. /// <summary>
  172. /// Limits the minimum size of the editor window.
  173. /// </summary>
  174. void SetEditorWindowSize()
  175. {
  176. EditorWindow editorWindow = this;
  177. Vector2 windowSize = new Vector2(640, 210);
  178. editorWindow.minSize = windowSize;
  179. editorWindow.maxSize = windowSize;
  180. }
  181. }
  182. }
  183. #endif