Unity Editor 基础篇(十):DragAndDrop编辑器下拖拽区域

转自:http://blog.csdn.net/LIQIANGEASTSUN/article/details/59753587

请点击链接查看原文,尊重楼主版权。


在Inspector 窗口上创建区域,向区域拖拽资源对象,获取到拖拽到区域的对象。

注意点:

绘制Box区域:

GUIContent title = new GUIContent(string msg);
           var dragArea = GUILayoutUtility.GetRect(0f, 35f, GUILayout.ExpandWidth(true));
          GUI.Box(dragArea, title);

Scripts文件夹下新建脚本:

public class Car : MonoBehaviour
{
    [SerializeField]
    private Transform temp = null;
 
}


然后在Editor文件夹下创建脚本,拖拽事件相关:

using UnityEngine;
using System.Collections;
using UnityEditor;
 
public class DragAreaGetObject : Editor
{
 
    public static UnityEngine.Object GetOjbect(string meg = null)
    {
        Event aEvent;
        aEvent = Event.current;
 
        GUI.contentColor = Color.white;
        UnityEngine.Object temp = null;
 
        var dragArea = GUILayoutUtility.GetRect(0f, 35f, GUILayout.ExpandWidth(true));
 
        GUIContent title = new GUIContent(meg);
        if (string.IsNullOrEmpty(meg))
        {
            title = new GUIContent("Drag Object here from Project view to get the object");
        }
 
        GUI.Box(dragArea, title);
        switch (aEvent.type)
        {
            case EventType.dragUpdated:
            case EventType.dragPerform:
                if (!dragArea.Contains(aEvent.mousePosition))
                {
                    break;
                }
 
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                if (aEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
 
                    for (int i = 0; i < DragAndDrop.objectReferences.Length; ++i)
                    {
                        temp = DragAndDrop.objectReferences[i];
 
                        if (temp == null)
                        {
                            break;
                        }
                    }
                }
 
                Event.current.Use();
                break;
            default:
                break;
        }
 
        return temp;
    }
}

然后在Editor文件夹中创建脚本,绘制Car类:

using UnityEngine;
using System.Collections;
using UnityEditor;
 
[CanEditMultipleObjects]
[CustomEditor(typeof(Car))]
public class CarEditor : Editor
{
    private SerializedProperty tempProperty;
    private UnityEngine.Object tempObj = null;
 
    void OnEnable()
    {
        tempProperty = serializedObject.FindProperty("temp");
    }
 
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        GUILayout.Space(10);
 
        tempObj = DragAreaGetObject.GetOjbect();
 
        if (tempObj != null)
        {
            tempProperty.objectReferenceValue = tempObj;
            Debug.LogError(tempProperty.objectReferenceValue.name);
        }
 
        GUILayout.Space(10);
        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
 
        serializedObject.ApplyModifiedProperties();
    }
}

 

效果:

Unity Editor 基础篇(十):DragAndDrop编辑器下拖拽区域_第1张图片

 

拖拽Hierarchy面板一个物体到此区域,获取这个物体并打印。

你可能感兴趣的:(unity)