浏览代码

Handle drop in Callable Reorderable Lists

/feature-handle-drop-rlist
Thomas ICHÉ 4 年前
当前提交
6da1266b
共有 2 个文件被更改,包括 54 次插入2 次删除
  1. 4
      CHANGELOG.md
  2. 52
      Editor/PropertyDrawers/CallableReorderableList.cs

4
CHANGELOG.md


## 2020.2.6
#### Added
* Handle Drop in Callable Reorderable Lists
## 2020.2.5
#### Added

52
Editor/PropertyDrawers/CallableReorderableList.cs


using UnityEditor;
using UnityEditorInternal;
using System.Linq;
using System;
namespace GameplayIngredients.Editor
{

void DrawHeader(Rect r)
{
GUI.Label(r, new GUIContent($" {serializedProperty.displayName}", Styles.callableIcon), EditorStyles.boldLabel);
HandleDrop(r, this);
}
void HandleDrop(Rect rect, CallableReorderableList list)
{
var currentEvent = Event.current;
var usedEvent = false;
switch (currentEvent.type)
{
case EventType.DragExited:
if (GUI.enabled)
HandleUtility.Repaint();
break;
case EventType.DragUpdated:
case EventType.DragPerform:
if (rect.Contains(currentEvent.mousePosition) && GUI.enabled)
{
bool acceptAtLeastOne = false;
Object[] droppedObjects = DragAndDrop.objectReferences;
foreach (Object obj in droppedObjects)
{
if (obj != null && obj is Callable)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (currentEvent.type == EventType.DragPerform)
{
list.serializedProperty.arraySize++;
int arrayEnd = list.serializedProperty.arraySize - 1;
list.serializedProperty.GetArrayElementAtIndex(arrayEnd).objectReferenceValue = obj as Callable;
acceptAtLeastOne = true;
}
}
}
if (acceptAtLeastOne)
{
GUI.changed = true;
DragAndDrop.AcceptDrag();
usedEvent = true;
}
}
break;
}
if (usedEvent)
currentEvent.Use();
}
void DrawElement(Rect r, int index, bool isActive, bool isFocused)

public static class CallableExtensions
{
public static void AddCallable(this GameObject gameObject, Component component, string propertyName, Type t)
public static void AddCallable(this GameObject gameObject, Component component, string propertyName, System.Type t)
{
var field = component.GetType().GetFields().Where(f => f.Name == propertyName).FirstOrDefault();
var val = field.GetValue(component) as Callable[];

正在加载...
取消
保存