forked from Tunkali/com.unity.xr.management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorUtilities.cs
134 lines (118 loc) · 5.17 KB
/
EditorUtilities.cs
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
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using UnityEngine;
namespace UnityEditor.XR.Management
{
internal static class EditorUtilities
{
internal static readonly string[] s_DefaultGeneralSettingsPath = {"XR"};
internal static readonly string[] s_DefaultLoaderPath = {"XR","Loaders"};
internal static readonly string[] s_DefaultSettingsPath = {"XR","Settings"};
internal static bool AssetDatabaseHasInstanceOfType(string type)
{
var assets = AssetDatabase.FindAssets(String.Format("t:{0}", type));
return assets.Any();
}
internal static T GetInstanceOfTypeFromAssetDatabase<T>() where T : class
{
var assets = AssetDatabase.FindAssets(String.Format("t:{0}", typeof(T).Name));
if (assets.Any())
{
string assetPath = AssetDatabase.GUIDToAssetPath(assets[0]);
var asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T));
return asset as T;
}
return null;
}
internal static ScriptableObject GetInstanceOfTypeWithNameFromAssetDatabase(string typeName)
{
var assets = AssetDatabase.FindAssets(String.Format("t:{0}", typeName));
if (assets.Any())
{
string assetPath = AssetDatabase.GUIDToAssetPath(assets[0]);
var asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(ScriptableObject));
return asset as ScriptableObject;
}
return null;
}
internal static string GetAssetPathForComponents(string[] pathComponents, string root = "Assets")
{
if (pathComponents.Length <= 0)
return null;
string path = root;
foreach( var pc in pathComponents)
{
string subFolder = Path.Combine(path, pc);
bool shouldCreate = true;
foreach (var f in AssetDatabase.GetSubFolders(path))
{
if (String.Compare(Path.GetFullPath(f), Path.GetFullPath(subFolder), true) == 0)
{
shouldCreate = false;
break;
}
}
if (shouldCreate)
AssetDatabase.CreateFolder(path, pc);
path = subFolder;
}
return path;
}
internal static string TypeNameToString(Type type)
{
return type == null ? "" : TypeNameToString(type.FullName);
}
internal static string TypeNameToString(string type)
{
string[] typeParts = type.Split(new char[] { '.' });
if (!typeParts.Any())
return String.Empty;
for (int i = 0; i < typeParts.Length; ++i)
{
typeParts[i] = typeParts[i].TrimStart('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
}
string[] words = Regex.Matches(typeParts.Last(), "[\\w\\d]+")
.OfType<Match>()
.Select(m => m.Value)
.ToArray();
return string.Join("", words);
}
internal static ScriptableObject CreateScriptableObjectInstance(string typeName, string path)
{
ScriptableObject obj = ScriptableObject.CreateInstance(typeName) as ScriptableObject;
if (obj != null)
{
if (!string.IsNullOrEmpty(path))
{
string fileName = String.Format("{0}.asset", EditorUtilities.TypeNameToString(typeName));
string targetPath = Path.Combine(path, fileName);
AssetDatabase.CreateAsset(obj, targetPath);
AssetDatabase.SaveAssets();
return obj;
}
}
Debug.LogError($"We were unable to create an instance of the requested type {typeName}. Please make sure that all packages are updated to support this version of XR Plug-In Management. See the Unity documentation for XR Plug-In Management for information on resolving this issue.");
return null;
}
/// <summary>
/// Retrieves the path of the XR Management package's Android manifest XML template.
/// </summary>
/// <param name="packageName">Package name in Java convention (eg com.package.module).</param>
/// <returns><see cref="string"/> of the XML file path.</returns>
internal static string GetPackagePath(string packageName)
{
#if UNITY_2021_3_OR_NEWER
var xrManagementPackageInfo = UnityEditor.PackageManager.PackageInfo.GetAllRegisteredPackages()
.Where(package => package.name == packageName)
.First();
return xrManagementPackageInfo.resolvedPath;
#else
throw new System.NotSupportedException("XR Management package couldn't be found, please make sure you have the XR Management package added to your project through the Package Management window.");
#endif // UNITY_2021_3_OR_NEWER
}
}
}