码迷,mamicode.com
首页 > 其他好文 > 详细

通过拖拽prefab来存储相应的路径

时间:2014-09-04 13:08:49      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   使用   ar   for   

bubuko.com,布布扣
 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         else
29         {
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 }
ObjectToPathDrawer

 

bubuko.com,布布扣
public class ObjectToPathAttribute : PropertyAttribute
{
    public string _input;
    public ObjectToPathAttribute(string matchineName)
    {
        _input = matchineName;
    }
}
ObjectToPathAttribute
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里,省去键盘输入步骤。截图如下:

bubuko.com,布布扣

当prefab为空的时候的截图如下:

bubuko.com,布布扣

通过拖拽prefab来存储相应的路径

标签:style   blog   http   color   os   io   使用   ar   for   

原文地址:http://www.cnblogs.com/mbysky/p/3955728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!