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