2020-05-16 15:17:04 +02:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public static class SerializeReferenceGenericSelectionMenu
|
|
|
|
|
{
|
|
|
|
|
/// Purpose.
|
2021-04-30 16:41:33 +02:00
|
|
|
|
/// This is generic selection menu (will appear at position).
|
2020-05-16 15:17:04 +02:00
|
|
|
|
/// Filtering.
|
|
|
|
|
/// You can add substring filter here to filter by search string.
|
|
|
|
|
/// As well ass type or interface restrictions.
|
|
|
|
|
/// As well as any custom restriction that is based on input type.
|
|
|
|
|
/// And it will be performed on each Appropriate type found by TypeCache.
|
2021-04-30 16:41:33 +02:00
|
|
|
|
public static void ShowContextMenuForManagedReference(this SerializedProperty property, Rect position, IEnumerable<Func<Type,bool>> filters = null)
|
2022-12-28 14:03:46 +01:00
|
|
|
|
{
|
|
|
|
|
GenericMenu context = new GenericMenu();
|
2021-04-30 16:41:33 +02:00
|
|
|
|
FillContextMenu(filters, context, property);
|
|
|
|
|
context.DropDown(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Purpose.
|
|
|
|
|
/// This is generic selection menu (will appear at click position).
|
|
|
|
|
/// Filtering.
|
|
|
|
|
/// You can add substring filter here to filter by search string.
|
|
|
|
|
/// As well ass type or interface restrictions.
|
|
|
|
|
/// As well as any custom restriction that is based on input type.
|
|
|
|
|
/// And it will be performed on each Appropriate type found by TypeCache.
|
|
|
|
|
public static void ShowContextMenuForManagedReference(this SerializedProperty property, IEnumerable<Func<Type, bool>> filters = null)
|
|
|
|
|
{
|
2022-12-28 14:03:46 +01:00
|
|
|
|
GenericMenu context = new GenericMenu();
|
2020-05-16 15:17:04 +02:00
|
|
|
|
FillContextMenu(filters, context, property);
|
|
|
|
|
context.ShowAsContext();
|
2021-04-30 16:41:33 +02:00
|
|
|
|
}
|
2020-05-16 15:17:04 +02:00
|
|
|
|
|
|
|
|
|
private static void FillContextMenu(IEnumerable<Func<Type, bool>> enumerableFilters, GenericMenu contextMenu, SerializedProperty property)
|
|
|
|
|
{
|
2022-12-28 14:03:46 +01:00
|
|
|
|
List<Func<Type, bool>> filters = enumerableFilters.ToList();// Prevents possible multiple enumerations
|
2020-05-16 15:17:04 +02:00
|
|
|
|
|
|
|
|
|
// Adds "Make Null" menu command
|
2020-05-16 19:15:24 +02:00
|
|
|
|
contextMenu.AddItem(new GUIContent("Null"), false, property.SetManagedReferenceToNull);
|
2022-12-28 14:03:46 +01:00
|
|
|
|
|
2020-05-16 19:15:24 +02:00
|
|
|
|
// Collects appropriate types
|
2022-12-28 14:03:46 +01:00
|
|
|
|
IEnumerable<Type> appropriateTypes = property.GetAppropriateTypesForAssigningToManagedReference(filters);
|
2020-05-16 19:15:24 +02:00
|
|
|
|
|
|
|
|
|
// Adds appropriate types to menu
|
2022-12-28 14:03:46 +01:00
|
|
|
|
foreach (Type appropriateType in appropriateTypes)
|
|
|
|
|
{
|
2020-05-16 19:15:24 +02:00
|
|
|
|
AddItemToContextMenu(appropriateType, contextMenu, property);
|
2022-12-28 14:03:46 +01:00
|
|
|
|
}
|
2020-05-16 15:17:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddItemToContextMenu(Type type, GenericMenu genericMenuContext, SerializedProperty property)
|
|
|
|
|
{
|
2022-12-28 14:03:46 +01:00
|
|
|
|
string assemblyName = type.Assembly.ToString().Split('(', ',')[0];
|
|
|
|
|
string entryName = type + " ( " + assemblyName + " )";
|
2020-05-16 19:15:24 +02:00
|
|
|
|
genericMenuContext.AddItem(new GUIContent(entryName), false, AssignNewInstanceCommand, new GenericMenuParameterForAssignInstanceCommand(type, property));
|
2020-05-16 15:17:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 19:15:24 +02:00
|
|
|
|
private static void AssignNewInstanceCommand(object objectGenericMenuParameter )
|
2020-05-16 15:17:04 +02:00
|
|
|
|
{
|
2022-12-28 14:03:46 +01:00
|
|
|
|
GenericMenuParameterForAssignInstanceCommand parameter = (GenericMenuParameterForAssignInstanceCommand) objectGenericMenuParameter;
|
|
|
|
|
Type type = parameter.Type;
|
|
|
|
|
SerializedProperty property = parameter.Property;
|
2020-05-16 19:15:24 +02:00
|
|
|
|
property.AssignNewInstanceOfTypeToManagedReference(type);
|
|
|
|
|
}
|
2020-05-16 15:17:04 +02:00
|
|
|
|
|
2020-05-16 19:15:24 +02:00
|
|
|
|
private readonly struct GenericMenuParameterForAssignInstanceCommand
|
2020-05-16 15:17:04 +02:00
|
|
|
|
{
|
2020-05-16 19:15:24 +02:00
|
|
|
|
public GenericMenuParameterForAssignInstanceCommand(Type type, SerializedProperty property)
|
2020-05-16 15:17:04 +02:00
|
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
Property = property;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly SerializedProperty Property;
|
|
|
|
|
public readonly Type Type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|