Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Last active September 23, 2022 07:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rutcreate/0af3c34abd497a2bceed506f953308d7 to your computer and use it in GitHub Desktop.
Save rutcreate/0af3c34abd497a2bceed506f953308d7 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.
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