11using System ;
22using System . Collections ;
3+ using System . Linq ;
34using TriInspectorUnityInternalBridge ;
45using TriInspector . Utilities ;
56using UnityEditor ;
67using UnityEditorInternal ;
78using UnityEngine ;
9+ using Object = UnityEngine . Object ;
810
911namespace 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 ;
0 commit comments