From e846c64d6f927879cb8a095e62d773a8d7b3c9f4 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 15 Oct 2020 07:24:10 +0800 Subject: *ability system --- .../AdvancedInspector/Attributes/ReadOnly.cs | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Assets/Plugins/AdvancedInspector/Attributes/ReadOnly.cs (limited to 'Assets/Plugins/AdvancedInspector/Attributes/ReadOnly.cs') diff --git a/Assets/Plugins/AdvancedInspector/Attributes/ReadOnly.cs b/Assets/Plugins/AdvancedInspector/Attributes/ReadOnly.cs new file mode 100644 index 00000000..145f1167 --- /dev/null +++ b/Assets/Plugins/AdvancedInspector/Attributes/ReadOnly.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +using UnityEngine; + +namespace AdvancedInspector +{ + /// + /// Makes a Property read only (cannot be modified) + /// It's grayed out in the inspector, even if there's a setter. + /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] + public class ReadOnlyAttribute : Attribute, IListAttribute, IRuntimeAttribute + { + public delegate bool ReadOnlyDelegate(); + public delegate bool ReadOnlyStaticDelegate(ReadOnlyAttribute readOnly, object instance, object value); + + #region IRuntime Implementation + private string methodName = ""; + + public string MethodName + { + get { return methodName; } + } + + public Type Template + { + get { return typeof(ReadOnlyDelegate); } + } + + public Type TemplateStatic + { + get { return typeof(ReadOnlyStaticDelegate); } + } + + private List delegates = new List(); + + public List Delegates + { + get { return delegates; } + set { delegates = value; } + } + + public bool Invoke(int index, object instance, object value) + { + if (delegates.Count == 0 || index >= delegates.Count) + return true; + + try + { + if (delegates[index].Target == null) + { + return (bool)delegates[index].DynamicInvoke(this, instance, value); + } + else + { + return (bool)delegates[index].DynamicInvoke(); + } + } + catch (Exception e) + { + if (e is TargetInvocationException) + e = ((TargetInvocationException)e).InnerException; + + Debug.LogError(string.Format("Invoking a method to retrieve a ReadOnly attribute failed. The exception was \"{0}\".", e.Message)); + return false; + } + } + #endregion + + public ReadOnlyAttribute() { } + + public ReadOnlyAttribute(Delegate method) + { + this.delegates.Add(method); + } + + public ReadOnlyAttribute(string methodName) + { + this.methodName = methodName; + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0