Skip to content

Commit

Permalink
Added AddMetadataOfType extension (editor only)
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicalError committed Aug 30, 2024
1 parent 6015dbd commit 5540896
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
8 changes: 8 additions & 0 deletions Editor/AssetExtensions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Editor/AssetExtensions/AddMetadataOfTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.CompilerServices;
using UnityEngine;

public static class AddMetadataOfTypeExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Metadata AddMetadataOfType<Metadata>(this Material asset)
where Metadata : CustomAssetMetadata
{
return AssetMetadataUtility.Add<Metadata>(asset) as Metadata;
}
}
11 changes: 11 additions & 0 deletions Editor/AssetExtensions/AddMetadataOfTypeExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 23 additions & 16 deletions Editor/Common/AssetMetadataUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public static List<Type> AllMetadataTypes

public static string GetDisplayName(Type metadataType)
{
if (metadataType == null)
return null;
if (metadataType == null) throw new NullReferenceException(nameof(metadataType));
EnsureInitialized();
if (metadataNames.TryGetValue(metadataType, out var value))
return value.displayName;
Expand All @@ -104,17 +103,19 @@ public static string GetDisplayName(Type metadataType)

public static string GetMenuName(Type metadataType)
{
if (metadataType == null)
return null;
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)
{
var assetPath = AssetDatabase.GetAssetPath(target);
{
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;
Expand All @@ -130,8 +131,11 @@ public static void GetAll(UnityEngine.Object target, List<CustomAssetMetadata> m
}

public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
{
if (!CanAddMetadataType(target, 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);
Expand Down Expand Up @@ -161,9 +165,10 @@ public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
}

public static bool CanAddMetadataType(UnityEngine.Object target, Type type)
{
if (ReferenceEquals(target, null) || target == null)
return false;
{
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))
Expand All @@ -184,15 +189,17 @@ public static bool CanAddMetadataType(UnityEngine.Object target, Type type)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CustomAssetMetadata Add<Metadata>(UnityEngine.Object target)
where Metadata : CustomAssetMetadata
{
return Add(target, typeof(Metadata));
{
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)
return;
{
if (metadata == null || ReferenceEquals(metadata, null)) throw new NullReferenceException(nameof(metadata));
EnsureInitialized();

var assetPath = AssetDatabase.GetAssetPath(metadata);
if (assetPath == null)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.custom_asset_metadata",
"displayName": "Custom Asset Metadata",
"version": "1.1.0",
"version": "1.2.0",
"unity": "2020.3",
"description": "Adds the ability to store meta data on Assets",
"license": "MIT",
Expand Down

0 comments on commit 5540896

Please sign in to comment.