Skip to content

Commit 5540896

Browse files
committed
Added AddMetadataOfType extension (editor only)
1 parent 6015dbd commit 5540896

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

Editor/AssetExtensions.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
public static class AddMetadataOfTypeExtensions
5+
{
6+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7+
public static Metadata AddMetadataOfType<Metadata>(this Material asset)
8+
where Metadata : CustomAssetMetadata
9+
{
10+
return AssetMetadataUtility.Add<Metadata>(asset) as Metadata;
11+
}
12+
}

Editor/AssetExtensions/AddMetadataOfTypeExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Common/AssetMetadataUtility.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public static List<Type> AllMetadataTypes
9494

9595
public static string GetDisplayName(Type metadataType)
9696
{
97-
if (metadataType == null)
98-
return null;
97+
if (metadataType == null) throw new NullReferenceException(nameof(metadataType));
9998
EnsureInitialized();
10099
if (metadataNames.TryGetValue(metadataType, out var value))
101100
return value.displayName;
@@ -104,17 +103,19 @@ public static string GetDisplayName(Type metadataType)
104103

105104
public static string GetMenuName(Type metadataType)
106105
{
107-
if (metadataType == null)
108-
return null;
106+
if (metadataType == null) throw new NullReferenceException(nameof(metadataType));
109107
EnsureInitialized();
110108
if (metadataNames.TryGetValue(metadataType, out var value))
111109
return value.menuPath;
112110
return ObjectNames.NicifyVariableName(metadataType.Name);
113111
}
114112

115113
public static void GetAll(UnityEngine.Object target, List<CustomAssetMetadata> metadata)
116-
{
117-
var assetPath = AssetDatabase.GetAssetPath(target);
114+
{
115+
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
116+
if (metadata == null) throw new NullReferenceException(nameof(metadata));
117+
EnsureInitialized();
118+
var assetPath = AssetDatabase.GetAssetPath(target);
118119
if (assetPath == null ||
119120
string.IsNullOrEmpty(assetPath))
120121
return;
@@ -130,8 +131,11 @@ public static void GetAll(UnityEngine.Object target, List<CustomAssetMetadata> m
130131
}
131132

132133
public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
133-
{
134-
if (!CanAddMetadataType(target, type))
134+
{
135+
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
136+
if (type == null) throw new NullReferenceException(nameof(type));
137+
EnsureInitialized();
138+
if (!CanAddMetadataType(target, type))
135139
return null;
136140

137141
var assetPath = AssetDatabase.GetAssetPath(target);
@@ -161,9 +165,10 @@ public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
161165
}
162166

163167
public static bool CanAddMetadataType(UnityEngine.Object target, Type type)
164-
{
165-
if (ReferenceEquals(target, null) || target == null)
166-
return false;
168+
{
169+
if (type == null) throw new NullReferenceException(nameof(type));
170+
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
171+
EnsureInitialized();
167172
if (disallowMultipleMetadataLookup.Contains(type))
168173
{
169174
if (MetadataLookup.HasMetadataOfType(target, type))
@@ -184,15 +189,17 @@ public static bool CanAddMetadataType(UnityEngine.Object target, Type type)
184189
[MethodImpl(MethodImplOptions.AggressiveInlining)]
185190
public static CustomAssetMetadata Add<Metadata>(UnityEngine.Object target)
186191
where Metadata : CustomAssetMetadata
187-
{
188-
return Add(target, typeof(Metadata));
192+
{
193+
if (target == null || ReferenceEquals(target, null)) throw new NullReferenceException(nameof(target));
194+
EnsureInitialized();
195+
return Add(target, typeof(Metadata));
189196
}
190197

191198
public static void Destroy<Metadata>(Metadata metadata)
192199
where Metadata : CustomAssetMetadata
193-
{
194-
if (metadata == null)
195-
return;
200+
{
201+
if (metadata == null || ReferenceEquals(metadata, null)) throw new NullReferenceException(nameof(metadata));
202+
EnsureInitialized();
196203

197204
var assetPath = AssetDatabase.GetAssetPath(metadata);
198205
if (assetPath == null)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.custom_asset_metadata",
33
"displayName": "Custom Asset Metadata",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"unity": "2020.3",
66
"description": "Adds the ability to store meta data on Assets",
77
"license": "MIT",

0 commit comments

Comments
 (0)