浏览代码

Handle drop in Callable Reorderable Lists (#47)

* Handle drop in Callable Reorderable Lists

* Handle drop as Game Objects

* Updated Changelog
/main
GitHub 3 年前
当前提交
68045e21
共有 3 个文件被更改,包括 81 次插入3 次删除
  1. 8
      CHANGELOG.md
  2. 1
      Editor/BrowsePopup/CallableProvider.cs
  3. 75
      Editor/PropertyDrawers/CallableReorderableList.cs

8
CHANGELOG.md


## 2020.2.6
#### Added
* Handle Drop in Callable Reorderable Lists : Dropped Callables components are added to the list. Dropped Game Objects will prompt a menu to select which callable to add.
#### Fixed
* Fixed Callable Reorderable List Undo.
## 2020.2.5
#### Added

1
Editor/BrowsePopup/CallableProvider.cs


static void AddCallable()
{
Undo.RecordObject(addNextComponentInfo.gameObject, "Add Callable");
addNextComponentInfo.gameObject.AddCallable(
addNextComponentInfo.component,
addNextComponentInfo.propertyName,

75
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 GameObject)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (currentEvent.type == EventType.DragPerform)
{
GenericMenu m = new GenericMenu();
var callables = (obj as GameObject).GetComponents<Callable>();
foreach(var c in callables)
{
m.AddItem(new GUIContent($"{c.Name} ({c.GetType().Name})"), false, () => {
list.serializedProperty.serializedObject.Update();
list.serializedProperty.arraySize++;
int arrayEnd = list.serializedProperty.arraySize - 1;
list.serializedProperty.GetArrayElementAtIndex(arrayEnd).objectReferenceValue = c;
list.serializedProperty.serializedObject.ApplyModifiedProperties();
});
}
m.ShowAsContext();
acceptAtLeastOne = true;
}
}
else 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[];

var newCmp = gameObject.AddComponent(t);
var newCmp = Undo.AddComponent(gameObject, t);
field.SetValue(component, val.Append(newCmp as Callable));
}
else

正在加载...
取消
保存