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();
    }
}

 

 

效果:


 

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

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

最后再次声明:

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

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

 

补充:

或者:

使用EditorGUILayout.BeginHorizontal();这种绘制一个区域,接受拖拽放下鼠标

            Rect dragRect = EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Add"))
            {
                
            }
            EditorGUILayout.EndHorizontal();

            Event e = Event.current;
            if (dragRect.Contains(e.mousePosition))
            {
                if (e.type == EventType.DragUpdated)
                    DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                else if (e.type == EventType.DragPerform)
                {
                    Object[] objs = DragAndDrop.objectReferences;
                    e.Use();
                    for (int i = 0; objs != null && i < objs.Length; i++)
                    {
                        if (objs[i] != null)
                        {
                            
                        }
                    }
                }
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值