Skip to content

Commit 78d032d

Browse files
committed
Add list header drag and drop
1 parent 513a250 commit 78d032d

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

Editor/Elements/TriListElement.cs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections;
3+
using System.Linq;
34
using TriInspectorUnityInternalBridge;
45
using TriInspector.Utilities;
56
using UnityEditor;
67
using UnityEditorInternal;
78
using UnityEngine;
9+
using Object = UnityEngine.Object;
810

911
namespace TriInspector.Elements
1012
{
@@ -121,10 +123,15 @@ public override void OnGUI(Rect position)
121123
}
122124

123125
private void AddElementCallback(ReorderableList reorderableList)
126+
{
127+
AddElementCallback(reorderableList, null);
128+
}
129+
130+
private void AddElementCallback(ReorderableList reorderableList, Object addedReferenceValue)
124131
{
125132
if (_property.TryGetSerializedProperty(out _))
126133
{
127-
ReorderableListProxy.defaultBehaviours.DoAddButton(reorderableList);
134+
ReorderableListProxy.DoAddButton(reorderableList, addedReferenceValue);
128135
_property.NotifyValueChanged();
129136
return;
130137
}
@@ -139,6 +146,12 @@ private void AddElementCallback(ReorderableList reorderableList)
139146
{
140147
var array = Array.CreateInstance(_property.ArrayElementType, template.Length + 1);
141148
Array.Copy(template, array, template.Length);
149+
150+
if (addedReferenceValue != null)
151+
{
152+
array.SetValue(addedReferenceValue, array.Length - 1);
153+
}
154+
142155
value = array;
143156
}
144157
else
@@ -148,7 +161,10 @@ private void AddElementCallback(ReorderableList reorderableList)
148161
value = (IList) Activator.CreateInstance(_property.FieldType);
149162
}
150163

151-
var newElement = CreateDefaultElementValue(_property);
164+
var newElement = addedReferenceValue != null
165+
? addedReferenceValue
166+
: CreateDefaultElementValue(_property);
167+
152168
value.Add(newElement);
153169
}
154170

@@ -293,6 +309,29 @@ private void DrawHeaderCallback(Rect rect)
293309

294310
var label = _reorderableListGui.count == 0 ? "Empty" : $"{_reorderableListGui.count} items";
295311
GUI.Label(arraySizeRect, label, Styles.ItemsCount);
312+
313+
if (Event.current.type == EventType.DragUpdated && rect.Contains(Event.current.mousePosition))
314+
{
315+
DragAndDrop.visualMode = DragAndDrop.objectReferences.All(obj => TryGetDragAndDropObject(obj, out _))
316+
? DragAndDropVisualMode.Copy
317+
: DragAndDropVisualMode.Rejected;
318+
319+
Event.current.Use();
320+
}
321+
else if (Event.current.type == EventType.DragPerform && rect.Contains(Event.current.mousePosition))
322+
{
323+
DragAndDrop.AcceptDrag();
324+
325+
foreach (var obj in DragAndDrop.objectReferences)
326+
{
327+
if (TryGetDragAndDropObject(obj, out var addedReferenceValue))
328+
{
329+
AddElementCallback(_reorderableListGui, addedReferenceValue);
330+
}
331+
}
332+
333+
Event.current.Use();
334+
}
296335
}
297336

298337
private void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
@@ -336,6 +375,34 @@ private static Array CloneValue(TriProperty property)
336375
return template;
337376
}
338377

378+
private bool TryGetDragAndDropObject(Object obj, out Object result)
379+
{
380+
if (obj == null)
381+
{
382+
result = null;
383+
return false;
384+
}
385+
386+
var elementType = _property.ArrayElementType;
387+
var objType = obj.GetType();
388+
389+
if (elementType == objType || elementType.IsAssignableFrom(objType))
390+
{
391+
result = obj;
392+
return true;
393+
}
394+
395+
if (obj is GameObject go && typeof(Component).IsAssignableFrom(elementType) &&
396+
go.TryGetComponent(elementType, out var component))
397+
{
398+
result = component;
399+
return true;
400+
}
401+
402+
result = null;
403+
return false;
404+
}
405+
339406
private static class Styles
340407
{
341408
public static readonly GUIStyle ItemsCount;

Unity.InternalAPIEditorBridge.012/ReorderableListProxy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33
using UnityEditorInternal;
44
using UnityEngine;
5+
using Object = UnityEngine.Object;
56

67
namespace TriInspectorUnityInternalBridge
78
{
@@ -59,5 +60,10 @@ public static void ClearCacheRecursive(ReorderableList list)
5960
ClearCacheMethod?.Invoke(list, Array.Empty<object>());
6061
#endif
6162
}
63+
64+
public static void DoAddButton(ReorderableList list, Object value)
65+
{
66+
defaultBehaviours.DoAddButton(list, value);
67+
}
6268
}
6369
}

0 commit comments

Comments
 (0)