Created
April 5, 2013 15:15
-
-
Save mattbenic/5320101 to your computer and use it in GitHub Desktop.
Accepting drag and drop to multiple layout controlled areas in Unity Editor scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Text; | |
public class MultiDragAndDropExample : EditorWindow | |
{ | |
[MenuItem("Examples/Multi Drag and Drop")] | |
public static void ShowWindow() | |
{ | |
MultiDragAndDropExample example = ScriptableObject.CreateInstance<MultiDragAndDropExample>(); | |
if (null != example) example.Show(); | |
} | |
/// <summary> | |
/// Handle Unity's OnGUI call | |
/// </summary> | |
void OnGUI() | |
{ | |
// Draw first area | |
BoxWithEventsOnGUI("First"); | |
// Draw second area | |
BoxWithEventsOnGUI("Second"); | |
} | |
/// <summary> | |
/// Draws a vertical layout box and handles it's events | |
/// </summary> | |
/// <param name="label">Label for the box</param> | |
void BoxWithEventsOnGUI(string label) | |
{ | |
// Draw the controls | |
EditorGUILayout.BeginHorizontal("Box"); | |
{ | |
GUILayout.Label(label); | |
} | |
EditorGUILayout.EndHorizontal(); | |
Rect lastRect = GUILayoutUtility.GetLastRect(); | |
// Handle events | |
Event evt = Event.current; | |
switch (evt.type) | |
{ | |
case EventType.DragUpdated: | |
// Test against rect from last repaint | |
if (lastRect.Contains(evt.mousePosition)) | |
{ | |
// Change cursor and consume the event | |
DragAndDrop.visualMode = DragAndDropVisualMode.Copy; | |
evt.Use(); | |
} | |
break; | |
case EventType.DragPerform: | |
// Test against rect from last repaint | |
if (lastRect.Contains(evt.mousePosition)) | |
{ | |
// Handle the drop however you want to | |
StringBuilder builder = new StringBuilder(); | |
builder.Append(label).Append(" accepted items: \n"); | |
foreach (string path in DragAndDrop.paths) builder.Append(path).Append("\n"); | |
EditorUtility.DisplayDialog("Drag and Drop", builder.ToString(), "Ok"); | |
// Change cursor and consume the event and drag | |
DragAndDrop.visualMode = DragAndDropVisualMode.Copy; | |
DragAndDrop.AcceptDrag(); | |
evt.Use(); | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment