TMP_PackageResourceImporter.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 = Directory.Exists("Assets/TextMesh Pro");
  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. var packageFullPath = GetPackageFullPath();
  54. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", false);
  55. }
  56. GUILayout.Space(5f);
  57. GUI.enabled = true;
  58. }
  59. GUILayout.EndVertical();
  60. }
  61. GUILayout.EndVertical();
  62. GUILayout.Space(5f);
  63. }
  64. internal void RegisterResourceImportCallback()
  65. {
  66. AssetDatabase.importPackageCompleted += ImportCallback;
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="packageName"></param>
  72. void ImportCallback(string packageName)
  73. {
  74. if (packageName == "TMP Essential Resources")
  75. {
  76. m_EssentialResourcesImported = true;
  77. TMPro_EventManager.ON_RESOURCES_LOADED();
  78. #if UNITY_2018_3_OR_NEWER
  79. SettingsService.NotifySettingsProviderChanged();
  80. #endif
  81. }
  82. else if (packageName == "TMP Examples & Extras")
  83. {
  84. m_ExamplesAndExtrasResourcesImported = true;
  85. m_IsImportingExamples = false;
  86. }
  87. Debug.Log("[" + packageName + "] have been imported.");
  88. AssetDatabase.importPackageCompleted -= ImportCallback;
  89. }
  90. static string GetPackageFullPath()
  91. {
  92. // Check for potential UPM package
  93. string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
  94. if (Directory.Exists(packagePath))
  95. {
  96. return packagePath;
  97. }
  98. packagePath = Path.GetFullPath("Assets/..");
  99. if (Directory.Exists(packagePath))
  100. {
  101. // Search default location for development package
  102. if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
  103. {
  104. return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
  105. }
  106. // Search for default location of normal TextMesh Pro AssetStore package
  107. if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
  108. {
  109. return packagePath + "/Assets/TextMesh Pro";
  110. }
  111. // Search for potential alternative locations in the user project
  112. string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
  113. string path = ValidateLocation(matchingPaths, packagePath);
  114. if (path != null) return packagePath + path;
  115. }
  116. return null;
  117. }
  118. static string ValidateLocation(string[] paths, string projectPath)
  119. {
  120. for (int i = 0; i < paths.Length; i++)
  121. {
  122. // Check if the Editor Resources folder exists.
  123. if (Directory.Exists(paths[i] + "/Editor Resources"))
  124. {
  125. string folderPath = paths[i].Replace(projectPath, "");
  126. folderPath = folderPath.TrimStart('\\', '/');
  127. return folderPath;
  128. }
  129. }
  130. return null;
  131. }
  132. }
  133. public class TMP_PackageResourceImporterWindow : EditorWindow
  134. {
  135. [SerializeField]
  136. TMP_PackageResourceImporter m_ResourceImporter;
  137. public static void ShowPackageImporterWindow()
  138. {
  139. var window = GetWindow<TMP_PackageResourceImporterWindow>();
  140. window.titleContent = new GUIContent("TMP Importer");
  141. window.Focus();
  142. }
  143. void OnEnable()
  144. {
  145. // Set Editor Window Size
  146. SetEditorWindowSize();
  147. if (m_ResourceImporter == null)
  148. m_ResourceImporter = new TMP_PackageResourceImporter();
  149. if (m_ResourceImporter.m_IsImportingExamples)
  150. m_ResourceImporter.RegisterResourceImportCallback();
  151. }
  152. void OnDestroy()
  153. {
  154. m_ResourceImporter.OnDestroy();
  155. }
  156. void OnGUI()
  157. {
  158. m_ResourceImporter.OnGUI();
  159. }
  160. void OnInspectorUpdate()
  161. {
  162. Repaint();
  163. }
  164. /// <summary>
  165. /// Limits the minimum size of the editor window.
  166. /// </summary>
  167. void SetEditorWindowSize()
  168. {
  169. EditorWindow editorWindow = this;
  170. Vector2 windowSize = new Vector2(640, 210);
  171. editorWindow.minSize = windowSize;
  172. editorWindow.maxSize = windowSize;
  173. }
  174. }
  175. }
  176. #endif