Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions Editor/Elements/TriListElement.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections;
using System.Linq;
using TriInspectorUnityInternalBridge;
using TriInspector.Utilities;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;

namespace TriInspector.Elements
{
Expand Down Expand Up @@ -121,10 +123,15 @@ public override void OnGUI(Rect position)
}

private void AddElementCallback(ReorderableList reorderableList)
{
AddElementCallback(reorderableList, null);
}

private void AddElementCallback(ReorderableList reorderableList, Object addedReferenceValue)
{
if (_property.TryGetSerializedProperty(out _))
{
ReorderableListProxy.defaultBehaviours.DoAddButton(reorderableList);
ReorderableListProxy.DoAddButton(reorderableList, addedReferenceValue);
_property.NotifyValueChanged();
return;
}
Expand All @@ -139,6 +146,12 @@ private void AddElementCallback(ReorderableList reorderableList)
{
var array = Array.CreateInstance(_property.ArrayElementType, template.Length + 1);
Array.Copy(template, array, template.Length);

if (addedReferenceValue != null)
{
array.SetValue(addedReferenceValue, array.Length - 1);
}

value = array;
}
else
Expand All @@ -148,7 +161,10 @@ private void AddElementCallback(ReorderableList reorderableList)
value = (IList) Activator.CreateInstance(_property.FieldType);
}

var newElement = CreateDefaultElementValue(_property);
var newElement = addedReferenceValue != null
? addedReferenceValue
: CreateDefaultElementValue(_property);

value.Add(newElement);
}

Expand Down Expand Up @@ -293,6 +309,29 @@ private void DrawHeaderCallback(Rect rect)

var label = _reorderableListGui.count == 0 ? "Empty" : $"{_reorderableListGui.count} items";
GUI.Label(arraySizeRect, label, Styles.ItemsCount);

if (Event.current.type == EventType.DragUpdated && rect.Contains(Event.current.mousePosition))
{
DragAndDrop.visualMode = DragAndDrop.objectReferences.All(obj => TryGetDragAndDropObject(obj, out _))
? DragAndDropVisualMode.Copy
: DragAndDropVisualMode.Rejected;

Event.current.Use();
}
else if (Event.current.type == EventType.DragPerform && rect.Contains(Event.current.mousePosition))
{
DragAndDrop.AcceptDrag();

foreach (var obj in DragAndDrop.objectReferences)
{
if (TryGetDragAndDropObject(obj, out var addedReferenceValue))
{
AddElementCallback(_reorderableListGui, addedReferenceValue);
}
}

Event.current.Use();
}
}

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

private bool TryGetDragAndDropObject(Object obj, out Object result)
{
if (obj == null)
{
result = null;
return false;
}

var elementType = _property.ArrayElementType;
var objType = obj.GetType();

if (elementType == objType || elementType.IsAssignableFrom(objType))
{
result = obj;
return true;
}

if (obj is GameObject go && typeof(Component).IsAssignableFrom(elementType) &&
go.TryGetComponent(elementType, out var component))
{
result = component;
return true;
}

result = null;
return false;
}

private static class Styles
{
public static readonly GUIStyle ItemsCount;
Expand Down
6 changes: 6 additions & 0 deletions Unity.InternalAPIEditorBridge.012/ReorderableListProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;

namespace TriInspectorUnityInternalBridge
{
Expand Down Expand Up @@ -59,5 +60,10 @@ public static void ClearCacheRecursive(ReorderableList list)
ClearCacheMethod?.Invoke(list, Array.Empty<object>());
#endif
}

public static void DoAddButton(ReorderableList list, Object value)
{
defaultBehaviours.DoAddButton(list, value);
}
}
}