Version 1.1. Generic parameters support
This commit is contained in:
3
CHANGELOG.md
Normal file
3
CHANGELOG.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### 1.1.0
|
||||
|
||||
Added support for generic arguments. Works like a generic Func, where the last generic argument is the return type of the function. Having only the return value will look for getter properties aswell. Having > 1 generic argument will only look for suitable public methods
|
||||
7
CHANGELOG.md.meta
Normal file
7
CHANGELOG.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4313795d52b054b409f55b7abf4be995
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a98267ecc93a89f4c9b31bad1ccb0bb0
|
||||
guid: 0600072b8385f4646a65c03fada4ed7d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
@@ -17,8 +17,8 @@ using Object = UnityEngine.Object;
|
||||
|
||||
namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(SerializableFunc<>))]
|
||||
public class SerializableFuncPropertyDrawer : PropertyDrawer
|
||||
[CustomPropertyDrawer(typeof(SerializableFuncBase<>), true)]
|
||||
public class SerializableFuncBasePropertyDrawer : PropertyDrawer
|
||||
{
|
||||
private const string Target_Function_Label = "Target Function";
|
||||
private const string Target_Object_Label = "Target Object";
|
||||
@@ -29,6 +29,8 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
private const string MixedValueContent_Property_Name = "mixedValueContent";
|
||||
|
||||
private static BindingFlags SuitableMethodsFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance;
|
||||
|
||||
#region GUI Drawing
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
@@ -98,7 +100,7 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
EditorGUI.indentLevel = indentLevel;
|
||||
}
|
||||
|
||||
private void DrawMethodNameProperty(ref Rect previousRect,
|
||||
private Rect DrawMethodNameProperty(ref Rect previousRect,
|
||||
SerializedProperty funcProperty,
|
||||
SerializedProperty targetObjectSerializedProperty,
|
||||
SerializedProperty targetMethodSerializedProperty)
|
||||
@@ -120,6 +122,7 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
else
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
if (targetObjectSerializedProperty.objectReferenceValue == null
|
||||
|| string.IsNullOrEmpty(targetMethodSerializedProperty.stringValue))
|
||||
{
|
||||
@@ -128,6 +131,7 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
else if (!IsPersistantListenerValid(funcProperty, targetObjectSerializedProperty, targetMethodSerializedProperty))
|
||||
{
|
||||
string missingComponentString = GetMissingComponentMethodString(targetObjectSerializedProperty, targetMethodSerializedProperty);
|
||||
|
||||
stringBuilder.Append(missingComponentString);
|
||||
}
|
||||
else
|
||||
@@ -153,6 +157,8 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
return targetMethodRect;
|
||||
}
|
||||
|
||||
private bool IsPersistantListenerValid(SerializedProperty funcProperty,
|
||||
@@ -162,19 +168,21 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
if (targetObjectProperty.objectReferenceValue == null
|
||||
|| string.IsNullOrWhiteSpace(targetMethodProperty.stringValue)) return false;
|
||||
|
||||
Type returnType = GetFuncReturnTypeFromProperty(funcProperty);
|
||||
Type[] funcArguments = GetFuncTypeArguments(funcProperty);
|
||||
Type returnType = funcArguments.Last();
|
||||
|
||||
MethodInfo targetMethod = targetObjectProperty.objectReferenceValue
|
||||
.GetType()
|
||||
.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(x => IsSuitableMethodInfo(x, returnType))
|
||||
.GetMethods(SuitableMethodsFlags)
|
||||
.Where(x => IsSuitableMethodInfo(x, funcArguments))
|
||||
.FirstOrDefault(x => string.Equals(x.Name, targetMethodProperty.stringValue));
|
||||
|
||||
if (targetMethod != null) return true;
|
||||
if ((funcArguments.Length - 1) != 0) return false;
|
||||
|
||||
MethodInfo targetProperty = targetObjectProperty.objectReferenceValue
|
||||
.GetType()
|
||||
.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance)
|
||||
.GetProperties(SuitableMethodsFlags)
|
||||
.Where(x => IsSuitableProperty(x, returnType))
|
||||
.Select(x => x.GetGetMethod())
|
||||
.FirstOrDefault(x => string.Equals(x.Name, targetMethodProperty.stringValue));
|
||||
@@ -234,15 +242,6 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property Height Override
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
return 65f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region UI Toolkit Drawing
|
||||
@@ -387,18 +386,23 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
private string GetHeaderText(SerializedProperty property, string labelText)
|
||||
{
|
||||
return $"{(string.IsNullOrEmpty(labelText) ? "Func" : labelText)} {GetEventParams(property)}";
|
||||
return $"{(string.IsNullOrEmpty(labelText) ? "Func" : labelText)} {GetEventParamsString(property)}";
|
||||
}
|
||||
|
||||
private string GetEventParams(SerializedProperty property)
|
||||
private string GetEventParamsString(SerializedProperty property)
|
||||
{
|
||||
string funcReturnValue = GetFuncReturnTypeFromProperty(property).NicifyTypeName();
|
||||
IEnumerable<string> typeNames = GetFuncTypeArguments(property)
|
||||
.Select(x => x.NicifyTypeName());
|
||||
|
||||
string funcReturnValue = string.Join(", ", typeNames);
|
||||
return $"({funcReturnValue})";
|
||||
}
|
||||
|
||||
private Type GetFuncReturnTypeFromProperty(SerializedProperty funcProperty)
|
||||
private Type[] GetFuncTypeArguments(SerializedProperty funcProperty)
|
||||
{
|
||||
return funcProperty.GetBoxedValue().GetType().GetGenericArguments().Last();
|
||||
return funcProperty.GetBoxedValue().
|
||||
GetType().
|
||||
GetGenericArguments();
|
||||
}
|
||||
|
||||
private string GetMissingComponentMethodString(SerializedProperty objectProperty,
|
||||
@@ -437,15 +441,14 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
genericMenu.AddSeparator("");
|
||||
|
||||
Type returnType = GetFuncReturnTypeFromProperty(data.FuncProperty);
|
||||
|
||||
Type[] funcParameters = GetFuncTypeArguments(data.FuncProperty);
|
||||
Component[] components = ((GameObject)target).GetComponents<Component>();
|
||||
|
||||
DrawMenuForComponent(genericMenu, data, (GameObject)target, returnType);
|
||||
DrawMenuForComponent(genericMenu, data, (GameObject)target, funcParameters);
|
||||
|
||||
foreach (Component component in components)
|
||||
{
|
||||
DrawMenuForComponent(genericMenu, data, component, returnType);
|
||||
DrawMenuForComponent(genericMenu, data, component, funcParameters);
|
||||
}
|
||||
|
||||
return genericMenu;
|
||||
@@ -454,14 +457,14 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
private void DrawMenuForComponent(GenericMenu genericMenu,
|
||||
CachedPropertiesAndObjectsData data,
|
||||
Object component,
|
||||
Type returnType)
|
||||
Type[] funcParameters)
|
||||
{
|
||||
Type componentType = component.GetType();
|
||||
string componentName = componentType.Name;
|
||||
|
||||
MethodInfo[] suitableProperties = GetSuitablePropertiesFromType(componentType, returnType);
|
||||
MethodInfo[] suitableProperties = GetSuitablePropertiesFromType(componentType, funcParameters);
|
||||
|
||||
if (suitableProperties.Length > 0)
|
||||
if (suitableProperties != null && suitableProperties.Any())
|
||||
{
|
||||
GUIContent propertiesContent = new GUIContent($"{componentName}/Property Getters");
|
||||
genericMenu.AddDisabledItem(propertiesContent);
|
||||
@@ -482,9 +485,9 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
genericMenu.AddSeparator($"{componentName}/");
|
||||
}
|
||||
|
||||
MethodInfo[] suitableMethods = GetSuitableMethodsFromType(componentType, returnType);
|
||||
MethodInfo[] suitableMethods = GetSuitableMethodsFromType(componentType, funcParameters);
|
||||
|
||||
if (suitableMethods.Length > 0)
|
||||
if (suitableMethods.Any())
|
||||
{
|
||||
GUIContent methodsContent = new GUIContent($"{componentName}/Invokable Methods");
|
||||
genericMenu.AddDisabledItem(methodsContent);
|
||||
@@ -521,18 +524,23 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
#region Method List Getters
|
||||
|
||||
private MethodInfo[] GetSuitableMethodsFromType(Type componentType, Type returnType)
|
||||
private MethodInfo[] GetSuitableMethodsFromType(Type componentType, Type[] funcParameters)
|
||||
{
|
||||
IEnumerable<MethodInfo> suitableMethods = componentType.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(x => IsSuitableMethodInfo(x, returnType))
|
||||
IEnumerable<MethodInfo> suitableMethods = componentType
|
||||
.GetMethods(SuitableMethodsFlags)
|
||||
.Where(x => IsSuitableMethodInfo(x, funcParameters))
|
||||
.OrderBy(x => x.Name);
|
||||
|
||||
return suitableMethods.ToArray();
|
||||
}
|
||||
|
||||
private MethodInfo[] GetSuitablePropertiesFromType(Type componentType, Type returnType)
|
||||
private MethodInfo[] GetSuitablePropertiesFromType(Type componentType, Type[] funcParameters)
|
||||
{
|
||||
IEnumerable<PropertyInfo> suitableProperties = componentType.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance)
|
||||
if (funcParameters.Length != 1) return null;
|
||||
Type returnType = funcParameters.Last();
|
||||
|
||||
IEnumerable<PropertyInfo> suitableProperties = componentType
|
||||
.GetProperties(SuitableMethodsFlags)
|
||||
.Where(x => IsSuitableProperty(x, returnType));
|
||||
|
||||
IEnumerable<MethodInfo> propertyGetters = suitableProperties
|
||||
@@ -542,11 +550,25 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
return propertyGetters.ToArray();
|
||||
}
|
||||
|
||||
private bool IsSuitableMethodInfo(MethodInfo info, Type returnType)
|
||||
private bool IsSuitableMethodInfo(MethodInfo info, Type[] funcParameters)
|
||||
{
|
||||
return !info.IsSpecialName
|
||||
&& info.ReturnType == returnType
|
||||
&& info.GetParameters().Length == 0;
|
||||
if (info.IsSpecialName) return false;
|
||||
|
||||
Type returnType = funcParameters.Last();
|
||||
if (info.ReturnType != returnType) return false;
|
||||
|
||||
ParameterInfo[] parameters = info.GetParameters();
|
||||
if (parameters.Length != (funcParameters.Length - 1)) return false;
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
ParameterInfo parameter = parameters[i];
|
||||
Type funcParameterType = funcParameters[i];
|
||||
|
||||
if (parameter.ParameterType != funcParameterType) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool IsSuitableProperty(PropertyInfo propertyInfo, Type returnType)
|
||||
@@ -583,18 +605,14 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
targetObjectField?.SetValueWithoutNotify(info.TargetObject);
|
||||
|
||||
data.TargetObjectProperty.objectReferenceValue = info.TargetObject;
|
||||
#if UNITY_2023_1_OR_NEWER
|
||||
|
||||
#else
|
||||
#if !UNITY_2023_1_OR_NEWER
|
||||
// Has to happen before we reassign the value, so an additional annoying block of ifs here
|
||||
bool isSameString = string.Equals(data.TargetMethodProperty.stringValue, info.TargetMethod.Name);
|
||||
#endif
|
||||
|
||||
data.TargetMethodProperty.stringValue = info.TargetMethod.Name;
|
||||
|
||||
#if UNITY_2023_1_OR_NEWER
|
||||
|
||||
#else
|
||||
#if !UNITY_2023_1_OR_NEWER
|
||||
// Older Unity version doesn't automatically use the formatting callback if the old value is the same as the new value
|
||||
// This is a problem because properties or methods can have the same name on different objects (gameobject.name and transform.name, for example)
|
||||
// The easiest way to call for formatting to happen is to just reassign the callback
|
||||
@@ -608,7 +626,7 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
data.TargetMethodProperty.serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Utility
|
||||
|
||||
@@ -628,7 +646,7 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Serialized Property Getters
|
||||
|
||||
@@ -646,6 +664,15 @@ namespace Utilities.SerializableData.SerializableFunc.UnityEditorDrawers
|
||||
|
||||
#endregion
|
||||
|
||||
#region GUI Overrides
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
return 65f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Classes
|
||||
|
||||
private struct SelectedMethodInfo
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33baceefadfdeb945aa7f92e190518f0
|
||||
guid: a27853cfcf7edf94fa6821cd44d52513
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -4,6 +4,8 @@ Allows you to assign Func\<T\> via the Inspector.
|
||||
Looks and acts like a UnityEvent.
|
||||
Supports both GUI and UI Toolkit. Tested with Unity 2020.3, 2021.3, 2022.2, 2023.1.
|
||||
Tested in standalone Windows and Android builds, both with Mono and IL2CPP.
|
||||
Version 1.1 supports generic parameters.
|
||||
If there are no generic parameters for the func, you'll also be able to assign getter properties.
|
||||
|
||||
GUI Representation
|
||||

|
||||
@@ -17,11 +19,15 @@ public class ExampleClass : MonoBehaviour
|
||||
{
|
||||
[Header("My Bool Func")]
|
||||
[SerializeField] private SerializableFunc<bool> boolFunc;
|
||||
[SerializeField] private SerializableFunc<int, string> stringFunc;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
bool result = boolFunc.Invoke();
|
||||
Debug.Log(result);
|
||||
|
||||
string stringResult = stringFunc.Invoke(69);
|
||||
Debug.Log(stringResult);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
8
Runtime/SerializableFunc/Base.meta
Normal file
8
Runtime/SerializableFunc/Base.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bd647a4a04c7f947afa36dc0ab3e8bf
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
83
Runtime/SerializableFunc/Base/SerializableFuncBase.cs
Normal file
83
Runtime/SerializableFunc/Base/SerializableFuncBase.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
[System.Serializable]
|
||||
public abstract class SerializableFuncBase<TFuncType>
|
||||
where TFuncType : Delegate
|
||||
{
|
||||
[SerializeField] protected Object targetObject;
|
||||
[SerializeField] protected string methodName;
|
||||
|
||||
private TFuncType func;
|
||||
|
||||
private static BindingFlags SuitableMethodsFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance;
|
||||
|
||||
protected TFuncType GetReturnedFunc()
|
||||
{
|
||||
if (func == null)
|
||||
{
|
||||
if (targetObject == null)
|
||||
{
|
||||
throw new ArgumentNullException("Target Object is null!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(methodName))
|
||||
{
|
||||
throw new ArgumentNullException("Target Method is null!");
|
||||
}
|
||||
|
||||
Type funcType = typeof(TFuncType);
|
||||
|
||||
MethodInfo info = targetObject
|
||||
.GetType()
|
||||
.GetMethods(SuitableMethodsFlags)
|
||||
.FirstOrDefault(x => IsTargetMethodInfo(x, funcType));
|
||||
|
||||
if (info == null)
|
||||
{
|
||||
throw new MissingMethodException($"Object \"{targetObject.name}\" is missing target method: {methodName}");
|
||||
}
|
||||
|
||||
func = (TFuncType)Delegate.CreateDelegate(funcType, targetObject, methodName);
|
||||
}
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
#region Utility Functions
|
||||
|
||||
private bool IsTargetMethodInfo(MethodInfo methodInfo, Type funcType)
|
||||
{
|
||||
if (string.Equals(methodInfo.Name, methodName, StringComparison.InvariantCulture)) return false;
|
||||
|
||||
Type[] typeArguments = funcType.GetGenericArguments();
|
||||
|
||||
if (methodInfo.ReturnType != typeArguments.Last()) return false;
|
||||
|
||||
ParameterInfo[] parameters = methodInfo.GetParameters();
|
||||
|
||||
if (parameters.Length != (typeArguments.Length - 1)) return false;
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
Type argType = typeArguments[i];
|
||||
ParameterInfo parameterInfo = parameters[i];
|
||||
if (argType != parameterInfo.ParameterType) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public static implicit operator TFuncType(SerializableFuncBase<TFuncType> func)
|
||||
{
|
||||
if (func == null) return null;
|
||||
|
||||
TFuncType result = func.GetReturnedFunc();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
11
Runtime/SerializableFunc/Base/SerializableFuncBase.cs.meta
Normal file
11
Runtime/SerializableFunc/Base/SerializableFuncBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa96f9624fc8bad459ec0bc927427de8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Runtime/SerializableFunc/Implementations.meta
Normal file
8
Runtime/SerializableFunc/Implementations.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a05eff3308be0b2489425421e455c65f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Runtime/SerializableFunc/Implementations/SerializableFunc.cs
Normal file
175
Runtime/SerializableFunc/Implementations/SerializableFunc.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TReturn> : SerializableFuncBase<Func<TReturn>>
|
||||
{
|
||||
public TReturn Invoke()
|
||||
{
|
||||
Func<TReturn> func = GetReturnedFunc();
|
||||
return func();
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TReturn> : SerializableFuncBase<Func<TArg0, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0)
|
||||
{
|
||||
Func<TArg0, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1)
|
||||
{
|
||||
Func<TArg0, TArg1, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TReturn> : SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TReturn> :
|
||||
SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TReturn> :
|
||||
SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TReturn> :
|
||||
SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13, TArg14 arg14)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TReturn> :
|
||||
SerializableFuncBase<Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TReturn>>
|
||||
{
|
||||
public TReturn Invoke(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, TArg9 arg9, TArg10 arg10, TArg11 arg11, TArg12 arg12, TArg13 arg13, TArg14 arg14, TArg15 arg15)
|
||||
{
|
||||
Func<TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TReturn> func = GetReturnedFunc();
|
||||
return func(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 638c2cd796656454f8e97e9ef2cc6ae9
|
||||
guid: 37cbc0382ca8c094b869e417b3e986fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,73 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Utilities.SerializableData.SerializableFunc
|
||||
{
|
||||
[System.Serializable]
|
||||
public class SerializableFunc<TReturn>
|
||||
{
|
||||
[SerializeField] protected Object targetObject;
|
||||
[SerializeField] protected string methodName;
|
||||
|
||||
private Func<TReturn> func;
|
||||
|
||||
public Func<TReturn> Func { get { return GetReturnedFunc(); } }
|
||||
|
||||
public TReturn Invoke()
|
||||
{
|
||||
Func<TReturn> func = GetReturnedFunc();
|
||||
|
||||
if (func != null) return func();
|
||||
return default;
|
||||
}
|
||||
|
||||
#region Protected Interface
|
||||
|
||||
protected Func<TReturn> GetReturnedFunc()
|
||||
{
|
||||
if (func == null)
|
||||
{
|
||||
if (targetObject == null) return null;
|
||||
if (string.IsNullOrWhiteSpace(methodName)) return null;
|
||||
|
||||
MethodInfo info = targetObject
|
||||
.GetType()
|
||||
.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
|
||||
.FirstOrDefault(x => IsTargetMethodInfo(x));
|
||||
|
||||
if (info == null) return null;
|
||||
|
||||
func = (Func<TReturn>)Delegate.CreateDelegate(typeof(Func<TReturn>), targetObject, methodName);
|
||||
}
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Functions
|
||||
|
||||
private bool IsTargetMethodInfo(MethodInfo x)
|
||||
{
|
||||
return string.Equals(x.Name, methodName, StringComparison.InvariantCultureIgnoreCase)
|
||||
&& x.ReturnType == typeof(TReturn)
|
||||
&& x.GetParameters().Length == 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operators
|
||||
|
||||
public static implicit operator Func<TReturn>(SerializableFunc<TReturn> serializableFunc)
|
||||
{
|
||||
if (serializableFunc == null) return null;
|
||||
|
||||
return serializableFunc.GetReturnedFunc();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user