TMP_PackageResourceImporter.cs
7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#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()
{
// Check if the resources state has changed.
m_EssentialResourcesImported = Directory.Exists("Assets/TextMesh Pro");
m_ExamplesAndExtrasResourcesImported = Directory.Exists("Assets/TextMesh Pro/Examples & Extras");
GUILayout.BeginVertical();
{
// Display options to import Essential resources
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();
// Display options to import Examples & Extras
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"))
{
// 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.
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;
}
/// <summary>
///
/// </summary>
/// <param name="packageName"></param>
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()
{
// Check for potential UPM package
string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
if (Directory.Exists(packagePath))
{
return packagePath;
}
packagePath = Path.GetFullPath("Assets/..");
if (Directory.Exists(packagePath))
{
// Search default location for development package
if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
{
return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
}
// Search for default location of normal TextMesh Pro AssetStore package
if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
{
return packagePath + "/Assets/TextMesh Pro";
}
// Search for potential alternative locations in the user project
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++)
{
// Check if the Editor Resources folder exists.
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;
public static void ShowPackageImporterWindow()
{
var window = GetWindow<TMP_PackageResourceImporterWindow>();
window.titleContent = new GUIContent("TMP Importer");
window.Focus();
}
void OnEnable()
{
// Set Editor Window Size
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();
}
/// <summary>
/// Limits the minimum size of the editor window.
/// </summary>
void SetEditorWindowSize()
{
EditorWindow editorWindow = this;
Vector2 windowSize = new Vector2(640, 210);
editorWindow.minSize = windowSize;
editorWindow.maxSize = windowSize;
}
}
}
#endif