Advertisement
Bunny83

DADTestWindowA

Sep 20th, 2020
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4.  
  5.  
  6. public class DragObj
  7. {
  8.     public string title;
  9.     public DragContainer owner;
  10.     public void Draw()
  11.     {
  12.         Event e = Event.current;
  13.         GUILayout.Box(title);
  14.         var rect = GUILayoutUtility.GetLastRect();
  15.         if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
  16.         {
  17.             DragAndDrop.PrepareStartDrag();
  18.             DragAndDrop.SetGenericData("obj", this);
  19.             DragAndDrop.StartDrag("DragObj");
  20.         }
  21.     }
  22.     public void MoveTo(DragContainer aTarget)
  23.     {
  24.         if (owner != null)
  25.             owner.objs.Remove(this);
  26.         aTarget.objs.Add(this);
  27.         owner = aTarget;
  28.     }
  29. }
  30.  
  31. public class DragContainer
  32. {
  33.     public List<DragObj> objs = new List<DragObj>();
  34.     public void Draw()
  35.     {
  36.         Event e = Event.current;
  37.         GUILayout.Box("DropZone", GUILayout.Height(100));
  38.         var rect = GUILayoutUtility.GetLastRect();
  39.         if (e.type == EventType.DragUpdated && rect.Contains(e.mousePosition))
  40.         {
  41.             DragAndDrop.AcceptDrag();
  42.             DragAndDrop.visualMode = DragAndDropVisualMode.Move;
  43.         }
  44.         else if (e.type == EventType.DragPerform)
  45.         {
  46.             var obj = (DragObj)DragAndDrop.GetGenericData("obj");
  47.             obj.MoveTo(this);
  48.         }
  49.  
  50.         for (int i = 0; i < objs.Count; i++)
  51.         {
  52.             objs[i].Draw();
  53.         }
  54.     }
  55. }
  56.  
  57. public class DADTestWindowA : EditorWindow
  58. {
  59.     [MenuItem("Tools/Tests/WinA")]
  60.     static void Init()
  61.     {
  62.         CreateInstance<DADTestWindowA>().Show();
  63.     }
  64.     DragContainer container;
  65.     private void OnEnable()
  66.     {
  67.         container = new DragContainer();
  68.         new DragObj { title = "TestObj1A", owner = container }.MoveTo(container);
  69.         new DragObj { title = "TestObj2A", owner = container }.MoveTo(container);
  70.         new DragObj { title = "TestObj3A", owner = container }.MoveTo(container);
  71.     }
  72.  
  73.     void OnGUI()
  74.     {
  75.         container.Draw();
  76.     }
  77. }
  78.  
Advertisement
RAW Paste Data Copied
Advertisement