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.

207 lines
7.4 KiB

  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace TMPro
  6. {
  7. public class PackageResourceImporterWindow : EditorWindow
  8. {
  9. public static void ShowPackageImporterWindow()
  10. {
  11. var window = GetWindow<PackageResourceImporterWindow>();
  12. window.titleContent = new GUIContent("TMP Importer");
  13. window.Focus();
  14. }
  15. [SerializeField]
  16. bool k_EssentialResourcesImported;
  17. [SerializeField]
  18. bool k_ExamplesAndExtrasResourcesImported;
  19. [SerializeField]
  20. bool k_IsImportingExamples;
  21. void OnEnable()
  22. {
  23. // Set Editor Window Size
  24. SetEditorWindowSize();
  25. // Special handling due to scripts imported in a .unitypackage result in resulting in an assembly reload which clears the callbacks.
  26. if (k_IsImportingExamples)
  27. {
  28. AssetDatabase.importPackageCompleted += ImportCallback;
  29. k_IsImportingExamples = false;
  30. }
  31. }
  32. void OnDestroy()
  33. {
  34. k_EssentialResourcesImported = false;
  35. k_ExamplesAndExtrasResourcesImported = false;
  36. }
  37. void OnGUI()
  38. {
  39. bool importEssentialsPackage = false;
  40. bool importExamplesPackage = false;
  41. GUILayout.BeginVertical();
  42. {
  43. // Display options to import Essential resources
  44. GUILayout.BeginVertical(EditorStyles.helpBox);
  45. {
  46. GUILayout.Label("TMP Essentials", EditorStyles.boldLabel);
  47. 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 } );
  48. GUILayout.Space(5f);
  49. GUI.enabled = !k_EssentialResourcesImported;
  50. if (GUILayout.Button("Import TMP Essentials"))
  51. {
  52. importEssentialsPackage = true;
  53. }
  54. GUILayout.Space(5f);
  55. GUI.enabled = true;
  56. }
  57. GUILayout.EndVertical();
  58. // Display options to import Examples & Extras
  59. GUILayout.BeginVertical(EditorStyles.helpBox);
  60. {
  61. GUILayout.Label("TMP Examples & Extras", EditorStyles.boldLabel);
  62. 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 });
  63. GUILayout.Space(5f);
  64. GUI.enabled = k_EssentialResourcesImported && !k_ExamplesAndExtrasResourcesImported;
  65. if (GUILayout.Button("Import TMP Examples & Extras"))
  66. {
  67. importExamplesPackage = true;
  68. }
  69. GUILayout.Space(5f);
  70. GUI.enabled = true;
  71. }
  72. GUILayout.EndVertical();
  73. }
  74. GUILayout.EndVertical();
  75. GUILayout.Space(5f);
  76. // Import Essential Resources
  77. if (importEssentialsPackage)
  78. {
  79. AssetDatabase.importPackageCompleted += ImportCallback;
  80. string packageFullPath = GetPackageFullPath();
  81. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Essential Resources.unitypackage", false);
  82. }
  83. // Import Examples & Extras
  84. if (importExamplesPackage)
  85. {
  86. // 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.
  87. k_IsImportingExamples = true;
  88. string packageFullPath = GetPackageFullPath();
  89. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", false);
  90. }
  91. }
  92. void OnInspectorUpdate()
  93. {
  94. Repaint();
  95. }
  96. /// <summary>
  97. /// Limits the minimum size of the editor window.
  98. /// </summary>
  99. void SetEditorWindowSize()
  100. {
  101. EditorWindow editorWindow = this;
  102. Vector2 windowSize = new Vector2(640, 210);
  103. editorWindow.minSize = windowSize;
  104. editorWindow.maxSize = windowSize;
  105. }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. /// <param name="packageName"></param>
  110. void ImportCallback(string packageName)
  111. {
  112. if (packageName == "TMP Essential Resources")
  113. {
  114. k_EssentialResourcesImported = true;
  115. TMPro_EventManager.ON_RESOURCES_LOADED();
  116. #if UNITY_2018_3_OR_NEWER
  117. SettingsService.NotifySettingsProviderChanged();
  118. #endif
  119. }
  120. else if (packageName == "TMP Examples & Extras")
  121. {
  122. k_ExamplesAndExtrasResourcesImported = true;
  123. k_IsImportingExamples = false;
  124. }
  125. Debug.Log("[" + packageName + "] have been imported.");
  126. AssetDatabase.importPackageCompleted -= ImportCallback;
  127. }
  128. string GetPackageFullPath()
  129. {
  130. // Check for potential UPM package
  131. string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
  132. if (Directory.Exists(packagePath))
  133. {
  134. return packagePath;
  135. }
  136. packagePath = Path.GetFullPath("Assets/..");
  137. if (Directory.Exists(packagePath))
  138. {
  139. // Search default location for development package
  140. if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
  141. {
  142. return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
  143. }
  144. // Search for default location of normal TextMesh Pro AssetStore package
  145. if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
  146. {
  147. return packagePath + "/Assets/TextMesh Pro";
  148. }
  149. // Search for potential alternative locations in the user project
  150. string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
  151. string path = ValidateLocation(matchingPaths, packagePath);
  152. if (path != null) return packagePath + path;
  153. }
  154. return null;
  155. }
  156. string ValidateLocation(string[] paths, string projectPath)
  157. {
  158. for (int i = 0; i < paths.Length; i++)
  159. {
  160. // Check if the Editor Resources folder exists.
  161. if (Directory.Exists(paths[i] + "/Editor Resources"))
  162. {
  163. string folderPath = paths[i].Replace(projectPath, "");
  164. folderPath = folderPath.TrimStart('\\', '/');
  165. return folderPath;
  166. }
  167. }
  168. return null;
  169. }
  170. }
  171. }
  172. #endif