Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- public class DragObj
- {
- public string title;
- public DragContainer owner;
- public void Draw()
- {
- Event e = Event.current;
- GUILayout.Box(title);
- var rect = GUILayoutUtility.GetLastRect();
- if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
- {
- DragAndDrop.PrepareStartDrag();
- DragAndDrop.SetGenericData("obj", this);
- DragAndDrop.StartDrag("DragObj");
- }
- }
- public void MoveTo(DragContainer aTarget)
- {
- if (owner != null)
- owner.objs.Remove(this);
- aTarget.objs.Add(this);
- owner = aTarget;
- }
- }
- public class DragContainer
- {
- public List<DragObj> objs = new List<DragObj>();
- public void Draw()
- {
- Event e = Event.current;
- GUILayout.Box("DropZone", GUILayout.Height(100));
- var rect = GUILayoutUtility.GetLastRect();
- if (e.type == EventType.DragUpdated && rect.Contains(e.mousePosition))
- {
- DragAndDrop.AcceptDrag();
- DragAndDrop.visualMode = DragAndDropVisualMode.Move;
- }
- else if (e.type == EventType.DragPerform)
- {
- var obj = (DragObj)DragAndDrop.GetGenericData("obj");
- obj.MoveTo(this);
- }
- for (int i = 0; i < objs.Count; i++)
- {
- objs[i].Draw();
- }
- }
- }
- public class DADTestWindowA : EditorWindow
- {
- [MenuItem("Tools/Tests/WinA")]
- static void Init()
- {
- CreateInstance<DADTestWindowA>().Show();
- }
- DragContainer container;
- private void OnEnable()
- {
- container = new DragContainer();
- new DragObj { title = "TestObj1A", owner = container }.MoveTo(container);
- new DragObj { title = "TestObj2A", owner = container }.MoveTo(container);
- new DragObj { title = "TestObj3A", owner = container }.MoveTo(container);
- }
- void OnGUI()
- {
- container.Draw();
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement