123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #if UNITY_EDITOR
- using System;
- using System.IO;
- using UnityEngine;
- using UnityEditor;
- namespace TMPro
- {
- [System.Serializable]
- public class TMP_PackageResourceImporter
- {
- bool m_EssentialResourcesImported;
- bool m_ExamplesAndExtrasResourcesImported;
- internal bool m_IsImportingExamples;
- public TMP_PackageResourceImporter() { }
- public void OnDestroy()
- {
- }
- public void OnGUI()
- {
-
- m_EssentialResourcesImported = File.Exists("Assets/TextMesh Pro/Resources/TMP Settings.asset");
- m_ExamplesAndExtrasResourcesImported = Directory.Exists("Assets/TextMesh Pro/Examples & Extras");
- GUILayout.BeginVertical();
- {
-
- GUILayout.BeginVertical(EditorStyles.helpBox);
- {
- GUILayout.Label("TMP Essentials", EditorStyles.boldLabel);
- 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 } );
- GUILayout.Space(5f);
- GUI.enabled = !m_EssentialResourcesImported;
- if (GUILayout.Button("Import TMP Essentials"))
- {
- AssetDatabase.importPackageCompleted += ImportCallback;
- string packageFullPath = GetPackageFullPath();
- AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Essential Resources.unitypackage", false);
- }
- GUILayout.Space(5f);
- GUI.enabled = true;
- }
- GUILayout.EndVertical();
-
- GUILayout.BeginVertical(EditorStyles.helpBox);
- {
- GUILayout.Label("TMP Examples & Extras", EditorStyles.boldLabel);
- 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 });
- GUILayout.Space(5f);
- GUI.enabled = m_EssentialResourcesImported && !m_ExamplesAndExtrasResourcesImported;
- if (GUILayout.Button("Import TMP Examples & Extras"))
- {
-
- m_IsImportingExamples = true;
-
-
- var packageFullPath = GetPackageFullPath();
- AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", false);
- }
- GUILayout.Space(5f);
- GUI.enabled = true;
- }
- GUILayout.EndVertical();
- }
- GUILayout.EndVertical();
- GUILayout.Space(5f);
- }
- internal void RegisterResourceImportCallback()
- {
- AssetDatabase.importPackageCompleted += ImportCallback;
- }
-
-
-
-
- void ImportCallback(string packageName)
- {
- if (packageName == "TMP Essential Resources")
- {
- m_EssentialResourcesImported = true;
- TMPro_EventManager.ON_RESOURCES_LOADED();
- #if UNITY_2018_3_OR_NEWER
- SettingsService.NotifySettingsProviderChanged();
- #endif
- }
- else if (packageName == "TMP Examples & Extras")
- {
- m_ExamplesAndExtrasResourcesImported = true;
- m_IsImportingExamples = false;
-
- }
- Debug.Log("[" + packageName + "] have been imported.");
- AssetDatabase.importPackageCompleted -= ImportCallback;
- }
- static string GetPackageFullPath()
- {
-
- string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
- if (Directory.Exists(packagePath))
- {
- return packagePath;
- }
- packagePath = Path.GetFullPath("Assets/..");
- if (Directory.Exists(packagePath))
- {
-
- if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
- {
- return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
- }
-
- if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
- {
- return packagePath + "/Assets/TextMesh Pro";
- }
-
- string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
- string path = ValidateLocation(matchingPaths, packagePath);
- if (path != null) return packagePath + path;
- }
- return null;
- }
- static string ValidateLocation(string[] paths, string projectPath)
- {
- for (int i = 0; i < paths.Length; i++)
- {
-
- if (Directory.Exists(paths[i] + "/Editor Resources"))
- {
- string folderPath = paths[i].Replace(projectPath, "");
- folderPath = folderPath.TrimStart('\\', '/');
- return folderPath;
- }
- }
- return null;
- }
- }
- public class TMP_PackageResourceImporterWindow : EditorWindow
- {
- [SerializeField]
- TMP_PackageResourceImporter m_ResourceImporter;
- static TMP_PackageResourceImporterWindow m_ImporterWindow;
- public static void ShowPackageImporterWindow()
- {
- if (m_ImporterWindow == null)
- {
- m_ImporterWindow = GetWindow<TMP_PackageResourceImporterWindow>();
- m_ImporterWindow.titleContent = new GUIContent("TMP Importer");
- }
- m_ImporterWindow.Focus();
- }
- void OnEnable()
- {
-
- SetEditorWindowSize();
- if (m_ResourceImporter == null)
- m_ResourceImporter = new TMP_PackageResourceImporter();
- if (m_ResourceImporter.m_IsImportingExamples)
- m_ResourceImporter.RegisterResourceImportCallback();
- }
- void OnDestroy()
- {
- m_ResourceImporter.OnDestroy();
- }
- void OnGUI()
- {
- m_ResourceImporter.OnGUI();
- }
- void OnInspectorUpdate()
- {
- Repaint();
- }
-
-
-
- void SetEditorWindowSize()
- {
- EditorWindow editorWindow = this;
- Vector2 windowSize = new Vector2(640, 210);
- editorWindow.minSize = windowSize;
- editorWindow.maxSize = windowSize;
- }
- }
- }
- #endif
|