更新
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GraphicsCat
|
||||
{
|
||||
// Allow this attribute to be used on fields only
|
||||
[System.AttributeUsage(System.AttributeTargets.Field)]
|
||||
public class AutoLoadAttribute : PropertyAttribute
|
||||
{
|
||||
// Store the asset GUID for reliable loading
|
||||
public string assetGUID;
|
||||
|
||||
// Add a constructor that allows a path to be passed in for initial setup.
|
||||
// The drawer will convert this path to a GUID.
|
||||
public AutoLoadAttribute(string path)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
this.assetGUID = AssetDatabase.AssetPathToGUID(path);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91ff3b2732c536048991e97899a4613a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 328765
|
||||
packageName: LitPlus - URP Enhanced Lit Shader
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/GraphicsCat/Modules/Common/AutoLoad/AutoLoadAttribute.cs
|
||||
uploadId: 820558
|
||||
@@ -0,0 +1,49 @@
|
||||
#if UNITY_EDITOR
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GraphicsCat
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(AutoLoadAttribute))]
|
||||
public class AutoLoadDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
AutoLoadAttribute autoLoad = attribute as AutoLoadAttribute;
|
||||
|
||||
EditorGUI.PropertyField(position, property, label);
|
||||
|
||||
if (property.objectReferenceValue == null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(autoLoad.assetGUID))
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(autoLoad.assetGUID);
|
||||
if (!string.IsNullOrEmpty(assetPath))
|
||||
{
|
||||
System.Type fieldType = fieldInfo.FieldType;
|
||||
Object loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, fieldType);
|
||||
if (loadedAsset != null)
|
||||
{
|
||||
property.objectReferenceValue = loadedAsset;
|
||||
Debug.Log($"Auto-loaded asset '{loadedAsset.name}' for field '{property.displayName}' using GUID.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"AutoLoadDrawer: Failed to load asset with GUID '{autoLoad.assetGUID}'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (property.objectReferenceValue != null)
|
||||
{
|
||||
string assetPath = AssetDatabase.GetAssetPath(property.objectReferenceValue);
|
||||
string guid = AssetDatabase.AssetPathToGUID(assetPath);
|
||||
autoLoad.assetGUID = guid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a73153d0825f8f04b991a1086b09ce43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 328765
|
||||
packageName: LitPlus - URP Enhanced Lit Shader
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/GraphicsCat/Modules/Common/AutoLoad/AutoLoadDrawer.cs
|
||||
uploadId: 820558
|
||||
Reference in New Issue
Block a user