summaryrefslogtreecommitdiff
path: root/Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-10-15 07:24:10 +0800
committerchai <chaifix@163.com>2020-10-15 07:24:10 +0800
commite846c64d6f927879cb8a095e62d773a8d7b3c9f4 (patch)
tree7882744bbf2b6c7096ec15fb300f088c5a0807c5 /Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs
parentcd12e74241678ee3c0752484d310b202187ba24c (diff)
*ability system
Diffstat (limited to 'Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs')
-rw-r--r--Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs b/Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs
new file mode 100644
index 00000000..16a56fc4
--- /dev/null
+++ b/Assets/Plugins/AdvancedInspector/Attributes/RuntimeResolve.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Reflection;
+
+using UnityEngine;
+
+namespace AdvancedInspector
+{
+ /// <summary>
+ /// Forces a field to display a FieldEditor related to its current runtime type instead of the field type.
+ /// The Runtime version supply the type itself. Useful when the field value is null or for an unknown object picker.
+ /// </summary>
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
+ public class RuntimeResolveAttribute : Attribute, IListAttribute, IRuntimeAttribute<Type>
+ {
+ public delegate Type RuntimeResolveDelegate();
+ public delegate Type RuntimeResolveStaticDelegate(RuntimeResolveAttribute runtimeResolve, object instance, object value);
+
+ #region IRuntime Implementation
+ private string methodName = "";
+
+ public string MethodName
+ {
+ get { return methodName; }
+ }
+
+ public Type Template
+ {
+ get { return typeof(RuntimeResolveDelegate); }
+ }
+
+ public Type TemplateStatic
+ {
+ get { return typeof(RuntimeResolveStaticDelegate); }
+ }
+
+ private List<Delegate> delegates = new List<Delegate>();
+
+ public List<Delegate> Delegates
+ {
+ get { return delegates; }
+ set { delegates = value; }
+ }
+
+ public Type Invoke(int index, object instance, object value)
+ {
+ if (delegates.Count == 0 || index >= delegates.Count)
+ return null;
+
+ try
+ {
+ if (delegates[index].Target == null)
+ {
+ return delegates[index].DynamicInvoke(this, instance, value) as Type;
+ }
+ else
+ {
+ return delegates[index].DynamicInvoke() as Type;
+ }
+ }
+ catch (Exception e)
+ {
+ if (e is TargetInvocationException)
+ e = ((TargetInvocationException)e).InnerException;
+
+ Debug.LogError(string.Format("Invoking a method from a RuntimeResolve attribute failed. The exception was \"{0}\".", e.Message));
+ return null;
+ }
+ }
+ #endregion
+
+ public RuntimeResolveAttribute() { }
+
+ public RuntimeResolveAttribute(string methodName)
+ {
+ this.methodName = methodName;
+ }
+
+ public RuntimeResolveAttribute(Delegate method)
+ {
+ this.delegates.Add(method);
+ }
+ }
+} \ No newline at end of file