您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

70 行
2.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(WeakAssetReference))]
public class WeakAssetReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
{
// Figure out what asset types we allow. Default to all.
AssetTypeAttribute assetTypeProperty = System.Attribute.GetCustomAttribute(fieldInfo, typeof(AssetTypeAttribute)) as AssetTypeAttribute;
var assetType = assetTypeProperty != null ? assetTypeProperty.assetType : typeof(GameObject);
SerializedProperty guid = prop.FindPropertyRelative("guid");
string path = AssetDatabase.GUIDToAssetPath(guid.stringValue);
Object obj = AssetDatabase.LoadAssetAtPath(path,assetType);
pos = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(label.text+"("+guid.stringValue+")"));
Object newObj = EditorGUI.ObjectField(pos, obj, assetType, false);
if(newObj != obj)
{
if (newObj != null)
{
path = AssetDatabase.GetAssetPath(newObj);
guid.stringValue = AssetDatabase.AssetPathToGUID(path);
}
else
guid.stringValue = "";
}
}
}
[CustomPropertyDrawer(typeof(WeakBase), true)]
public class WeakBaseDrawer : PropertyDrawer
{
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
{
// Figure out what asset types we allow. Default to all.
var assetType = typeof(GameObject);
var baseType = fieldInfo.FieldType.BaseType;
if (baseType != null && baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(Weak<>))
assetType = baseType.GetGenericArguments()[0];
SerializedProperty guid = prop.FindPropertyRelative("guid");
string path = AssetDatabase.GUIDToAssetPath(guid.stringValue);
Object obj = AssetDatabase.LoadAssetAtPath(path,assetType);
pos = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), label);
Object newObj = EditorGUI.ObjectField(pos, obj, assetType, false);
if(newObj != obj)
{
if (newObj != null)
{
path = AssetDatabase.GetAssetPath(newObj);
guid.stringValue = AssetDatabase.AssetPathToGUID(path);
}
else
{
guid.stringValue = "";
}
guid.serializedObject.ApplyModifiedProperties();
}
}
}