-
-
Save hybridherbst/350565664bb9cd6ea1ad0e3e73cf7f4c to your computer and use it in GitHub Desktop.
How to use DragAndDrop function in Unity3D custom editor. This is just handle when user drag anything outside to THIS EditorWindow only.
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.Collections.Generic; | |
public class DragAndDropEditorWindow : EditorWindow | |
{ | |
[MenuItem("Window/Drag And Drop")] | |
public static void Open() | |
{ | |
GetWindow<DragAndDropEditorWindow>(false, "DragDrop", true); | |
} | |
private void OnGUI() | |
{ | |
if (Event.current.type == EventType.DragUpdated) | |
{ | |
DragAndDrop.visualMode = DragAndDropVisualMode.Copy; | |
Event.current.Use(); | |
} | |
else if (Event.current.type == EventType.DragPerform) | |
{ | |
// To consume drag data. | |
DragAndDrop.AcceptDrag(); | |
// GameObjects from hierarchy. | |
if (DragAndDrop.paths.Length == 0 && DragAndDrop.objectReferences.Length > 0) | |
{ | |
Debug.Log("GameObject"); | |
foreach (Object obj in DragAndDrop.objectReferences) | |
{ | |
Debug.Log("- " + obj); | |
} | |
} | |
// Object outside project. It mays from File Explorer (Finder in OSX). | |
else if (DragAndDrop.paths.Length > 0 && DragAndDrop.objectReferences.Length == 0) | |
{ | |
Debug.Log("File"); | |
foreach (string path in DragAndDrop.paths) | |
{ | |
Debug.Log("- " + path); | |
} | |
} | |
// Unity Assets including folder. | |
else if (DragAndDrop.paths.Length == DragAndDrop.objectReferences.Length) | |
{ | |
Debug.Log("UnityAsset"); | |
for (int i = 0; i < DragAndDrop.objectReferences.Length; i++) | |
{ | |
Object obj = DragAndDrop.objectReferences[i]; | |
string path = DragAndDrop.paths[i]; | |
Debug.Log(obj.GetType().Name); | |
// Folder. | |
if (obj is DefaultAsset) | |
{ | |
Debug.Log(path); | |
} | |
// C# or JavaScript. | |
else if (obj is MonoScript) | |
{ | |
Debug.Log(path + "\n" + obj); | |
} | |
else if (obj is Texture2D) | |
{ | |
} | |
} | |
} | |
// Log to make sure we cover all cases. | |
else | |
{ | |
Debug.Log("Out of reach"); | |
Debug.Log("Paths:"); | |
foreach (string path in DragAndDrop.paths) | |
{ | |
Debug.Log("- " + path); | |
} | |
Debug.Log("ObjectReferences:"); | |
foreach (Object obj in DragAndDrop.objectReferences) | |
{ | |
Debug.Log("- " + obj); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment