Skip to content

Commit 0b23ce0

Browse files
authored
Merge pull request #9 from Thundernerd/feature/drag_and_drop
Feature/drag and drop
2 parents f029648 + 0b1185c commit 0b23ce0

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public void OnGUI(Rect position)
6969
RawReferenceProperty,
7070
new GUIContent(rawReferenceValue.GetType().Name),
7171
true);
72+
73+
HandleDragAndDrop(objectFieldRect);
7274
}
7375

7476
/// <inheritdoc />

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
using System;
2+
using System.Linq;
23
using TNRD.Utilities;
34
using UnityEditor;
45
using UnityEditor.IMGUI.Controls;
56
using UnityEngine;
7+
using UnityEngine.Assertions;
68
using UnityEngine.SceneManagement;
79
using Object = UnityEngine.Object;
810

911
namespace TNRD.Drawers
1012
{
1113
internal abstract class ReferenceDrawer
1214
{
15+
private enum DragAndDropMode
16+
{
17+
None,
18+
Raw,
19+
Unity
20+
}
21+
22+
private DragAndDropMode dragAndDropMode;
23+
1324
protected readonly SerializedProperty Property;
1425
protected readonly Type GenericType;
1526
protected readonly CustomObjectDrawer CustomObjectDrawer;
@@ -95,5 +106,94 @@ private void OnItemSelected(ReferenceMode mode, object reference)
95106
}
96107

97108
protected abstract void OnPropertiesClicked();
109+
110+
protected void HandleDragAndDrop(Rect position)
111+
{
112+
if (Event.current.type == EventType.DragPerform)
113+
{
114+
HandleDragPerform();
115+
}
116+
else if (Event.current.type == EventType.DragUpdated)
117+
{
118+
HandleDragUpdated(position);
119+
}
120+
}
121+
122+
private void SetDragAndDropMode(bool success, DragAndDropMode? successMode = null)
123+
{
124+
if (success)
125+
{
126+
Assert.IsTrue(successMode.HasValue);
127+
dragAndDropMode = successMode.Value;
128+
DragAndDrop.visualMode = DragAndDropVisualMode.Link;
129+
}
130+
else
131+
{
132+
dragAndDropMode = DragAndDropMode.None;
133+
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
134+
}
135+
}
136+
137+
private void HandleDragUpdated(Rect position)
138+
{
139+
if (!position.Contains(Event.current.mousePosition))
140+
return;
141+
142+
if (DragAndDrop.objectReferences.Length > 1)
143+
{
144+
SetDragAndDropMode(false);
145+
return;
146+
}
147+
148+
Object objectReference = DragAndDrop.objectReferences[0];
149+
150+
if (objectReference is GameObject gameObject)
151+
{
152+
Component component = gameObject.GetComponent(GenericType);
153+
SetDragAndDropMode(component != null, DragAndDropMode.Unity);
154+
}
155+
else if (objectReference is MonoScript monoScript)
156+
{
157+
Type scriptType = monoScript.GetClass();
158+
159+
if (scriptType.IsSubclassOf(typeof(UnityEngine.Object)))
160+
{
161+
SetDragAndDropMode(false);
162+
return;
163+
}
164+
165+
if (!GenericType.IsAssignableFrom(scriptType))
166+
{
167+
SetDragAndDropMode(false);
168+
return;
169+
}
170+
171+
bool isValidDrop = scriptType.GetConstructors().Any(x => x.GetParameters().Length == 0);
172+
SetDragAndDropMode(isValidDrop, DragAndDropMode.Raw);
173+
}
174+
else
175+
{
176+
SetDragAndDropMode(GenericType.IsInstanceOfType(objectReference), DragAndDropMode.Unity);
177+
}
178+
}
179+
180+
private void HandleDragPerform()
181+
{
182+
switch (dragAndDropMode)
183+
{
184+
case DragAndDropMode.Raw:
185+
RawReferenceProperty.managedReferenceValue =
186+
Activator.CreateInstance(((MonoScript)DragAndDrop.objectReferences[0]).GetClass());
187+
ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Raw;
188+
break;
189+
case DragAndDropMode.Unity:
190+
UnityReferenceProperty.objectReferenceValue = DragAndDrop.objectReferences[0];
191+
ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Unity;
192+
break;
193+
case DragAndDropMode.None:
194+
default:
195+
throw new ArgumentOutOfRangeException();
196+
}
197+
}
98198
}
99199
}

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void OnGUI(Rect position)
2727
Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType();
2828
GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType);
2929
CustomObjectDrawer.OnGUI(position, label, objectContent);
30+
HandleDragAndDrop(position);
3031
}
3132

3233
protected override void OnPropertiesClicked()

0 commit comments

Comments
 (0)