mirror of
https://github.com/maxartz15/UnitySerializedReferenceUI.git
synced 2025-06-19 19:16:45 +02:00
organize into upm directory structure
This commit is contained in:
101
Core/ManagedReferenceUtility.cs
Normal file
101
Core/ManagedReferenceUtility.cs
Normal file
@ -0,0 +1,101 @@
|
||||
#if UNITY_EDITOR
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
public static class ManagedReferenceUtility
|
||||
{
|
||||
/// Creates instance of passed type and assigns it to managed reference
|
||||
public static object AssignNewInstanceOfTypeToManagedReference(this SerializedProperty serializedProperty, Type type)
|
||||
{
|
||||
var instance = Activator.CreateInstance(type);
|
||||
|
||||
serializedProperty.serializedObject.Update();
|
||||
serializedProperty.managedReferenceValue = instance;
|
||||
serializedProperty.serializedObject.ApplyModifiedProperties();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Sets managed reference to null
|
||||
public static void SetManagedReferenceToNull(this SerializedProperty serializedProperty)
|
||||
{
|
||||
serializedProperty.serializedObject.Update();
|
||||
serializedProperty.managedReferenceValue = null;
|
||||
serializedProperty.serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
/// Collects appropriate types based on managed reference field type and filters. Filters all derive
|
||||
public static IEnumerable<Type> GetAppropriateTypesForAssigningToManagedReference(this SerializedProperty property, List<Func<Type, bool>> filters = null)
|
||||
{
|
||||
var fieldType = property.GetManagedReferenceFieldType();
|
||||
return GetAppropriateTypesForAssigningToManagedReference(fieldType, filters);
|
||||
}
|
||||
|
||||
/// Filters derived types of field typ parameter and finds ones whose are compatible with managed reference and filters.
|
||||
public static IEnumerable<Type> GetAppropriateTypesForAssigningToManagedReference(Type fieldType, List<Func<Type, bool>> filters = null)
|
||||
{
|
||||
var appropriateTypes = new List<Type>();
|
||||
|
||||
// Get and filter all appropriate types
|
||||
var derivedTypes = TypeCache.GetTypesDerivedFrom(fieldType);
|
||||
foreach (var type in derivedTypes)
|
||||
{
|
||||
// Skips unity engine Objects (because they are not serialized by SerializeReference)
|
||||
if (type.IsSubclassOf(typeof(Object)))
|
||||
continue;
|
||||
// Skip abstract classes because they should not be instantiated
|
||||
if (type.IsAbstract)
|
||||
continue;
|
||||
// Skip generic classes because they can not be instantiated
|
||||
if (type.ContainsGenericParameters)
|
||||
continue;
|
||||
// Skip types that has no public empty constructors (activator can not create them)
|
||||
if (type.IsClass && type.GetConstructor(Type.EmptyTypes) == null) // Structs still can be created (strangely)
|
||||
continue;
|
||||
// Filter types by provided filters if there is ones
|
||||
if (filters != null && filters.All(f => f == null || f.Invoke(type)) == false)
|
||||
continue;
|
||||
|
||||
appropriateTypes.Add(type);
|
||||
}
|
||||
|
||||
return appropriateTypes;
|
||||
}
|
||||
|
||||
/// Gets real type of managed reference
|
||||
public static Type GetManagedReferenceFieldType(this SerializedProperty property)
|
||||
{
|
||||
var realPropertyType = GetRealTypeFromTypename(property.managedReferenceFieldTypename);
|
||||
if (realPropertyType != null)
|
||||
return realPropertyType;
|
||||
|
||||
Debug.LogError($"Can not get field type of managed reference : {property.managedReferenceFieldTypename}");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Gets real type of managed reference's field typeName
|
||||
public static Type GetRealTypeFromTypename(string stringType)
|
||||
{
|
||||
var names = GetSplitNamesFromTypename(stringType);
|
||||
var realType = Type.GetType($"{names.ClassName}, {names.AssemblyName}");
|
||||
return realType;
|
||||
}
|
||||
|
||||
/// Get assembly and class names from typeName
|
||||
public static (string AssemblyName, string ClassName) GetSplitNamesFromTypename(string typename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(typename))
|
||||
return ("","");
|
||||
|
||||
var typeSplitString = typename.Split(char.Parse(" "));
|
||||
var typeClassName = typeSplitString[1];
|
||||
var typeAssemblyName = typeSplitString[0];
|
||||
return (typeAssemblyName, typeClassName);
|
||||
}
|
||||
}
|
||||
#endif
|
11
Core/ManagedReferenceUtility.cs.meta
Normal file
11
Core/ManagedReferenceUtility.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 07d7d3b6c96d5a04093953c060518522
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
82
Core/SerializeReferenceGenericSelectionMenu.cs
Normal file
82
Core/SerializeReferenceGenericSelectionMenu.cs
Normal file
@ -0,0 +1,82 @@
|
||||
#if UNITY_EDITOR
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public static class SerializeReferenceGenericSelectionMenu
|
||||
{
|
||||
/// Purpose.
|
||||
/// This is generic selection menu (will appear at 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, Rect position, IEnumerable<Func<Type,bool>> filters = null)
|
||||
{
|
||||
var context = new GenericMenu();
|
||||
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)
|
||||
{
|
||||
var context = new GenericMenu();
|
||||
FillContextMenu(filters, context, property);
|
||||
context.ShowAsContext();
|
||||
}
|
||||
|
||||
private static void FillContextMenu(IEnumerable<Func<Type, bool>> enumerableFilters, GenericMenu contextMenu, SerializedProperty property)
|
||||
{
|
||||
var filters = enumerableFilters.ToList();// Prevents possible multiple enumerations
|
||||
|
||||
// Adds "Make Null" menu command
|
||||
contextMenu.AddItem(new GUIContent("Null"), false, property.SetManagedReferenceToNull);
|
||||
|
||||
// Collects appropriate types
|
||||
var appropriateTypes = property.GetAppropriateTypesForAssigningToManagedReference(filters);
|
||||
|
||||
// Adds appropriate types to menu
|
||||
foreach (var appropriateType in appropriateTypes)
|
||||
AddItemToContextMenu(appropriateType, contextMenu, property);
|
||||
}
|
||||
|
||||
private static void AddItemToContextMenu(Type type, GenericMenu genericMenuContext, SerializedProperty property)
|
||||
{
|
||||
var assemblyName = type.Assembly.ToString().Split('(', ',')[0];
|
||||
var entryName = type + " ( " + assemblyName + " )";
|
||||
genericMenuContext.AddItem(new GUIContent(entryName), false, AssignNewInstanceCommand, new GenericMenuParameterForAssignInstanceCommand(type, property));
|
||||
}
|
||||
|
||||
private static void AssignNewInstanceCommand(object objectGenericMenuParameter )
|
||||
{
|
||||
var parameter = (GenericMenuParameterForAssignInstanceCommand) objectGenericMenuParameter;
|
||||
var type = parameter.Type;
|
||||
var property = parameter.Property;
|
||||
property.AssignNewInstanceOfTypeToManagedReference(type);
|
||||
}
|
||||
|
||||
private readonly struct GenericMenuParameterForAssignInstanceCommand
|
||||
{
|
||||
public GenericMenuParameterForAssignInstanceCommand(Type type, SerializedProperty property)
|
||||
{
|
||||
Type = type;
|
||||
Property = property;
|
||||
}
|
||||
|
||||
public readonly SerializedProperty Property;
|
||||
public readonly Type Type;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
11
Core/SerializeReferenceGenericSelectionMenu.cs.meta
Normal file
11
Core/SerializeReferenceGenericSelectionMenu.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7796948b498be241ba10c88bcf3d5fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
14
Core/SerializeReferenceTypeRestrictionFilters.cs
Normal file
14
Core/SerializeReferenceTypeRestrictionFilters.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public static class SerializeReferenceTypeRestrictionFilters
|
||||
{
|
||||
public static Func<Type, bool> TypeIsNotSubclassOrEqualOrHasInterface(Type[] types) => type =>
|
||||
TypeIsSubclassOrEqualOrHasInterface(types).Invoke(type) == false;
|
||||
|
||||
public static Func<Type, bool> TypeIsSubclassOrEqualOrHasInterface(Type[] types) => type =>
|
||||
types.Any(e => e.IsInterface ? TypeHasInterface(type, e) : TypeIsSubclassOrEqual(type, e));
|
||||
|
||||
public static bool TypeIsSubclassOrEqual(Type type, Type comparator) => type.IsSubclassOf(comparator) || type == comparator;
|
||||
public static bool TypeHasInterface(this Type type, Type comparator) => type.GetInterface(comparator.ToString()) != null;
|
||||
}
|
11
Core/SerializeReferenceTypeRestrictionFilters.cs.meta
Normal file
11
Core/SerializeReferenceTypeRestrictionFilters.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b71802ffed0a18d43901672fcbf668c9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user