-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssetMetadataUtility.cs
More file actions
225 lines (199 loc) · 7.82 KB
/
AssetMetadataUtility.cs
File metadata and controls
225 lines (199 loc) · 7.82 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
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.ComponentModel;
using System.Linq;
public static class AssetMetadataUtility
{
static HashSet<Type> disallowMultipleMetadataLookup;
static Dictionary<Type, Type[]> restrictedTypesLookup;
static Dictionary<Type, (string displayName, string menuPath)> metadataNames;
static List<Type> allCustomAssetMetadataTypes;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void EnsureInitialized()
{
if (allCustomAssetMetadataTypes != null)
return;
var metadataTypes = TypeCache.GetTypesDerivedFrom<CustomAssetMetadata>();
metadataNames = new Dictionary<Type, (string displayName, string menuPath)>(metadataTypes.Count);
allCustomAssetMetadataTypes = metadataTypes.ToList();
restrictedTypesLookup = new Dictionary<Type, Type[]>();
disallowMultipleMetadataLookup = new HashSet<Type>();
foreach (var metadataType in metadataTypes)
{
string displayName = null;
string menuName = null;
var displayNameAttribute = metadataType.GetCustomAttribute<DisplayNameAttribute>();
if (displayNameAttribute != null)
{
displayName = displayNameAttribute.DisplayName;
}
var addMetadataMenuAttribute = metadataType.GetCustomAttribute<AddMetadataMenuAttribute>();
if (addMetadataMenuAttribute != null)
{
menuName = addMetadataMenuAttribute.MetadataMenu.Replace('\\', '/');
}
if (displayName == null && menuName != null)
{
int index = menuName.LastIndexOf('/');
displayName = (index == -1) ? menuName : menuName.Substring(index + 1);
}
if (displayName == null)
{
displayName = ObjectNames.NicifyVariableName(metadataType.Name);
}
if (menuName == null && displayName != null)
{
menuName = displayName;
}
metadataNames[metadataType] = (displayName, menuName);
var disallowMultipleCustomAssetMetadataAttribute = metadataType.GetCustomAttribute<DisallowMultipleCustomAssetMetadataAttribute>();
if (disallowMultipleCustomAssetMetadataAttribute != null)
{
disallowMultipleMetadataLookup.Add(metadataType);
}
var restrictMetadataAssetTypesAttribute = metadataType.GetCustomAttribute<RestrictMetadataAssetTypesAttribute>();
if (restrictMetadataAssetTypesAttribute != null)
{
restrictedTypesLookup[metadataType] = restrictMetadataAssetTypesAttribute.Types;
}
}
}
public static bool HaveMetadataTypes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
EnsureInitialized();
return allCustomAssetMetadataTypes.Count > 0;
}
}
public static List<Type> AllMetadataTypes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
EnsureInitialized();
return allCustomAssetMetadataTypes;
}
}
public static string GetDisplayName(Type metadataType)
{
if (metadataType == null) throw new NullReferenceException(nameof(metadataType));
EnsureInitialized();
if (metadataNames.TryGetValue(metadataType, out var value))
return value.displayName;
return ObjectNames.NicifyVariableName(metadataType.Name);
}
public static string GetMenuName(Type metadataType)
{
if (metadataType == null) throw new NullReferenceException(nameof(metadataType));
EnsureInitialized();
if (metadataNames.TryGetValue(metadataType, out var value))
return value.menuPath;
return ObjectNames.NicifyVariableName(metadataType.Name);
}
public static void GetAll(UnityEngine.Object target, List<CustomAssetMetadata> metadata)
{
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
if (metadata == null) throw new NullReferenceException(nameof(metadata));
EnsureInitialized();
var assetPath = AssetDatabase.GetAssetPath(target);
if (assetPath == null || string.IsNullOrEmpty(assetPath))
return;
if (target is SceneAsset)
{
// calling LoadAllAssetsAtPath with a scene throws, this doesn't.
// Still, right now scenes do not support adding metadata so this cannot be validated further
foreach (var allMetadataType in AllMetadataTypes)
{
var additionalDataAsset = (CustomAssetMetadata)AssetDatabase.LoadAssetAtPath(assetPath, allMetadataType);
metadata.Add(additionalDataAsset);
}
}
else
{
var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (var asset in assets)
{
if (asset is CustomAssetMetadata additionalDataAsset)
{
metadata.Add(additionalDataAsset);
}
}
}
}
public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
{
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
if (type == null) throw new NullReferenceException(nameof(type));
EnsureInitialized();
if (!CanAddMetadataType(target, type))
return null;
var assetPath = AssetDatabase.GetAssetPath(target);
if (string.IsNullOrWhiteSpace(assetPath))
{
Debug.LogError("AssetMetadataUtility.Add: Could not find asset path for target", target);
return null;
}
// TODO: could try to make non unity assets work by putting a file next to it?
var instance = ScriptableObject.CreateInstance(type);
instance.hideFlags = HideFlags.HideInHierarchy;
if (instance is CustomAssetMetadata assetMetadata)
{
assetMetadata.name = type.Name;
assetMetadata.reference = target;
assetMetadata.OnReset();
AssetDatabase.AddObjectToAsset(assetMetadata, assetPath);
AssetDatabase.ImportAsset(assetPath);
AssetDatabase.Refresh();
return assetMetadata;
}
UnityEngine.Object.DestroyImmediate(instance);
return null;
}
public static bool CanAddMetadataType(UnityEngine.Object target, Type type)
{
if (type == null) throw new NullReferenceException(nameof(type));
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
EnsureInitialized();
if (disallowMultipleMetadataLookup.Contains(type))
{
if (MetadataLookup.HasMetadataOfType(target, type))
return false;
}
if (!restrictedTypesLookup.TryGetValue(type, out var restrictedTypes))
return true;
var targetType = target.GetType();
foreach (var restrictedType in restrictedTypes)
{
if (restrictedType == targetType)
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CustomAssetMetadata Add<Metadata>(UnityEngine.Object target)
where Metadata : CustomAssetMetadata
{
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
EnsureInitialized();
return Add(target, typeof(Metadata));
}
public static void Destroy<Metadata>(Metadata metadata)
where Metadata : CustomAssetMetadata
{
if (metadata == null || ReferenceEquals(metadata, null)) throw new NullReferenceException(nameof(metadata));
EnsureInitialized();
var assetPath = AssetDatabase.GetAssetPath(metadata);
if (assetPath == null)
return;
AssetDatabase.RemoveObjectFromAsset(metadata);
UnityEngine.Object.DestroyImmediate(metadata); // we need to destroy it otherwise it'll be saved to the scene
AssetDatabase.ImportAsset(assetPath);
AssetDatabase.Refresh();
}
}