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文件夹下新建脚本:

 

 
  1. public class Car : MonoBehaviour

  2. {

  3. [SerializeField]

  4. private Transform temp = null;

  5.  
  6. }


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

 

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. public class DragAreaGetObject : Editor

  6. {

  7.  
  8. public static UnityEngine.Object GetOjbect(string meg = null)

  9. {

  10. Event aEvent;

  11. aEvent = Event.current;

  12.  
  13. GUI.contentColor = Color.white;

  14. UnityEngine.Object temp = null;

  15.  
  16. var dragArea = GUILayoutUtility.GetRect(0f, 35f, GUILayout.ExpandWidth(true));

  17.  
  18. GUIContent title = new GUIContent(meg);

  19. if (string.IsNullOrEmpty(meg))

  20. {

  21. title = new GUIContent("Drag Object here from Project view to get the object");

  22. }

  23.  
  24. GUI.Box(dragArea, title);

  25. switch (aEvent.type)

  26. {

  27. case EventType.dragUpdated:

  28. case EventType.dragPerform:

  29. if (!dragArea.Contains(aEvent.mousePosition))

  30. {

  31. break;

  32. }

  33.  
  34. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

  35. if (aEvent.type == EventType.DragPerform)

  36. {

  37. DragAndDrop.AcceptDrag();

  38.  
  39. for (int i = 0; i < DragAndDrop.objectReferences.Length; ++i)

  40. {

  41. temp = DragAndDrop.objectReferences[i];

  42.  
  43. if (temp == null)

  44. {

  45. break;

  46. }

  47. }

  48. }

  49.  
  50. Event.current.Use();

  51. break;

  52. default:

  53. break;

  54. }

  55.  
  56. return temp;

  57. }

  58. }

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

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. [CanEditMultipleObjects]

  6. [CustomEditor(typeof(Car))]

  7. public class CarEditor : Editor

  8. {

  9. private SerializedProperty tempProperty;

  10. private UnityEngine.Object tempObj = null;

  11.  
  12. void OnEnable()

  13. {

  14. tempProperty = serializedObject.FindProperty("temp");

  15. }

  16.  
  17. public override void OnInspectorGUI()

  18. {

  19. serializedObject.Update();

  20. GUILayout.Space(10);

  21.  
  22. tempObj = DragAreaGetObject.GetOjbect();

  23.  
  24. if (tempObj != null)

  25. {

  26. tempProperty.objectReferenceValue = tempObj;

  27. Debug.LogError(tempProperty.objectReferenceValue.name);

  28. }

  29.  
  30. GUILayout.Space(10);

  31. if (GUI.changed)

  32. {

  33. EditorUtility.SetDirty(target);

  34. }

  35.  
  36. serializedObject.ApplyModifiedProperties();

  37. }

  38. }

 

 

效果:


 

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

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

最后再次声明:

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

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

 

补充:

或者:

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

 
  1. Rect dragRect = EditorGUILayout.BeginHorizontal();

  2. if (GUILayout.Button("Add"))

  3. {

  4.  
  5. }

  6. EditorGUILayout.EndHorizontal();

  7.  
  8. Event e = Event.current;

  9. if (dragRect.Contains(e.mousePosition))

  10. {

  11. if (e.type == EventType.DragUpdated)

  12. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;

  13. else if (e.type == EventType.DragPerform)

  14. {

  15. Object[] objs = DragAndDrop.objectReferences;

  16. e.Use();

  17. for (int i = 0; objs != null && i < objs.Length; i++)

  18. {

  19. if (objs[i] != null)

  20. {

  21.  
  22. }

  23. }

  24. }

  25. }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack Yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值