首页 > 代码库 > 通过拖拽prefab来存储相应的路径
通过拖拽prefab来存储相应的路径
1 using UnityEngine; 2 using System.Collections; 3 using UnityEditor; 4 using System.Reflection; 5 6 [CustomPropertyDrawer(typeof(ObjectToPathAttribute))] 7 public class ObjectToPathDrawer : PropertyDrawer 8 { 9 Object _obj = null;10 11 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)12 {13 Object target = property.serializedObject.targetObject;14 FieldInfo fi = target.GetType().GetField(property.name);15 16 string path = string.Empty;17 EditorGUI.LabelField(position, label);18 position.x += EditorGUIUtility.labelWidth * 2.0f / 3.0f;19 if (_obj == null)20 {21 path = fi.GetValue(target).ToString();22 if (!string.IsNullOrEmpty(path))23 {24 _obj = AssetDatabase.LoadMainAssetAtPath(path);25 }26 _obj = EditorGUI.ObjectField(position, _obj, typeof(GameObject), false);27 }28 else29 {30 _obj = EditorGUI.ObjectField(position, _obj, typeof(GameObject), false);31 path = AssetDatabase.GetAssetPath(_obj);32 fi.SetValue(target, path);33 }34 35 position.x += EditorGUIUtility.labelWidth * 2.0f / 3.0f;36 EditorGUI.TextField(position, path);37 }38 }
public class ObjectToPathAttribute : PropertyAttribute{ public string _input; public ObjectToPathAttribute(string matchineName) { _input = matchineName; }}
using UnityEngine;using System.Collections;public class UITest: MonoBehaviour{ [ObjectToPath("UITest)] public string _itemPerfabPath; // Use this for initialization void Start () { } // Update is called once per frame void Update () { }}
使用上面的代码可以通过拖拽一个prefab的方式把相应的路径直接存储到public string _itemPerfabPath里,省去键盘输入步骤。截图如下:
当prefab为空的时候的截图如下:
通过拖拽prefab来存储相应的路径
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。