1.8.0: Added AssetsOnly, SceneObjectsOnly, AssignAssetIfNull, AssignComponentIfNull

- Attributes: Added AssetsOnly, SceneObjectsOnly, AssignAssetIfNull, AssignComponentIfNull
- Attributes: Replaced AssignIfNull with AssignComponentIfNull
This commit is contained in:
Anders Ejlersen 2022-08-27 14:04:17 +02:00
parent 070b82767f
commit eb19150d98
23 changed files with 302 additions and 73 deletions

View file

@ -0,0 +1,25 @@
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(AssetsOnlyAttribute))]
internal sealed class AssetsOnlyAttributeDrawer : ValueModifierPropertyDrawer
{
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
return;
Object obj = property.objectReferenceValue;
if (obj != null && !IsValid(obj))
property.objectReferenceValue = null;
}
private static bool IsValid(Object obj)
{
return AssetDatabase.Contains(obj);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 84b797e92aa740e4866d71c9afaa6ff2
timeCreated: 1660250173

View file

@ -0,0 +1,43 @@
using System;
using UnityEditor;
using Object = UnityEngine.Object;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(AssignAssetIfNullAttribute))]
internal sealed class AssignAssetIfNullAttributeDrawer : ValueModifierPropertyDrawer
{
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
return;
if (property.objectReferenceValue != null)
return;
var att = (AssignAssetIfNullAttribute)attribute;
Type type = att.useType ? att.type : property.GetValueType();
property.objectReferenceValue = AssignAsset(type, att.filter, att.searchFolders);
}
private static Object AssignAsset(Type type, string filter, string[] folders)
{
string[] guids;
if (folders != null && folders.Length != 0)
guids = AssetDatabase.FindAssets($"{type.Name} {filter}", folders);
else
guids = AssetDatabase.FindAssets($"{type.Name} {filter}");
for (var i = 0; i < guids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
Object obj = AssetDatabase.LoadAssetAtPath(path, type);
if (obj != null)
return obj;
}
return null;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 68325a7d22ed40e2aae8e00fad5f05dc
timeCreated: 1661599398

View file

@ -5,8 +5,8 @@ using Object = UnityEngine.Object;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(AssignIfNullAttribute))]
internal sealed class AssignIfNullAttributeDrawer : ValueModifierPropertyDrawer
[CustomPropertyDrawer(typeof(AssignComponentIfNullAttribute))]
internal sealed class AssignComponentIfNullAttributeDrawer : ValueModifierPropertyDrawer
{
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
{
@ -15,7 +15,7 @@ namespace Module.Inspector.Editor
if (property.objectReferenceValue != null)
return;
var att = (AssignIfNullAttribute)attribute;
var att = (AssignComponentIfNullAttribute)attribute;
Type type = att.useType ? att.type : property.GetValueType();
if (!typeof(Component).IsAssignableFrom(type))

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 38c05c2014ac40c0915f6e36df16c7e4
timeCreated: 1661600081

View file

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: e029b0d7c1384ab2b961e74a2bf23fe0
timeCreated: 1638695373

View file

@ -0,0 +1,25 @@
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(SceneObjectsOnlyAttribute))]
internal sealed class SceneObjectsOnlyAttributeDrawer : ValueModifierPropertyDrawer
{
public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
return;
Object obj = property.objectReferenceValue;
if (obj != null && !IsValid(obj))
property.objectReferenceValue = null;
}
private static bool IsValid(Object obj)
{
return !AssetDatabase.Contains(obj);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 48dbd634fecb45699637d9dfff17f8e9
timeCreated: 1660250619