using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(Reference))]
public class ResReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var reference = (Reference)attribute;
switch (reference.RefType)
{
case ReferenceType.Prefab:
HandlePrefabReference(position, property, label);
break;
}
}
private void HandlePrefabReference (Rect position, SerializedProperty property, GUIContent label)
{
if (SerializedPropertyType.String != property.propertyType)
{
EditorGUI.PropertyField(position, property);
return;
}
//根据路径得到一个类型为GameObject的对象
var prefab = (GameObject)AssetDatabase.LoadAssetAtPath(property.stringValue, typeof(GameObject));
//ObjectField会在Inspector面板中显示一个框,后面带一个小按钮,点击后弹出面板选择prefab
var obj = (GameObject
Unity--PropertyAttribute和PropertyDrawer结合使用将string转为资源引用(优化)
最新推荐文章于 2025-01-01 23:49:20 发布