123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- using System;
- using System.Diagnostics;
- using System.Reflection;
- using UnityEngine;
- [Serializable]
- public class PropertyReference
- {
- public PropertyReference()
- {
- }
- public PropertyReference(Component target, string fieldName)
- {
- this.mTarget = target;
- this.mName = fieldName;
- }
- public Component target
- {
- get
- {
- return this.mTarget;
- }
- set
- {
- this.mTarget = value;
- this.mProperty = null;
- this.mField = null;
- }
- }
- public string name
- {
- get
- {
- return this.mName;
- }
- set
- {
- this.mName = value;
- this.mProperty = null;
- this.mField = null;
- }
- }
- public bool isValid
- {
- get
- {
- return this.mTarget != null && !string.IsNullOrEmpty(this.mName);
- }
- }
- public bool isEnabled
- {
- get
- {
- if (this.mTarget == null)
- {
- return false;
- }
- MonoBehaviour monoBehaviour = this.mTarget as MonoBehaviour;
- return monoBehaviour == null || monoBehaviour.enabled;
- }
- }
- public Type GetPropertyType()
- {
- if (this.mProperty == null && this.mField == null && this.isValid)
- {
- this.Cache();
- }
- if (this.mProperty != null)
- {
- return this.mProperty.PropertyType;
- }
- if (this.mField != null)
- {
- return this.mField.FieldType;
- }
- return typeof(void);
- }
- public override bool Equals(object obj)
- {
- if (obj == null)
- {
- return !this.isValid;
- }
- if (obj is PropertyReference)
- {
- PropertyReference propertyReference = obj as PropertyReference;
- return this.mTarget == propertyReference.mTarget && string.Equals(this.mName, propertyReference.mName);
- }
- return false;
- }
- public override int GetHashCode()
- {
- return PropertyReference.s_Hash;
- }
- public void Set(Component target, string methodName)
- {
- this.mTarget = target;
- this.mName = methodName;
- }
- public void Clear()
- {
- this.mTarget = null;
- this.mName = null;
- }
- public void Reset()
- {
- this.mField = null;
- this.mProperty = null;
- }
- public override string ToString()
- {
- return PropertyReference.ToString(this.mTarget, this.name);
- }
- public static string ToString(Component comp, string property)
- {
- if (!(comp != null))
- {
- return null;
- }
- string text = comp.GetType().ToString();
- int num = text.LastIndexOf('.');
- if (num > 0)
- {
- text = text.Substring(num + 1);
- }
- if (!string.IsNullOrEmpty(property))
- {
- return text + "." + property;
- }
- return text + ".[property]";
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- public object Get()
- {
- if (this.mProperty == null && this.mField == null && this.isValid)
- {
- this.Cache();
- }
- if (this.mProperty != null)
- {
- if (this.mProperty.CanRead)
- {
- return this.mProperty.GetValue(this.mTarget, null);
- }
- }
- else if (this.mField != null)
- {
- return this.mField.GetValue(this.mTarget);
- }
- return null;
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- public bool Set(object value)
- {
- if (this.mProperty == null && this.mField == null && this.isValid)
- {
- this.Cache();
- }
- if (this.mProperty == null && this.mField == null)
- {
- return false;
- }
- if (value == null)
- {
- try
- {
- if (this.mProperty == null)
- {
- this.mField.SetValue(this.mTarget, null);
- return true;
- }
- if (this.mProperty.CanWrite)
- {
- this.mProperty.SetValue(this.mTarget, null, null);
- return true;
- }
- }
- catch (Exception)
- {
- return false;
- }
- }
- if (!this.Convert(ref value))
- {
- if (Application.isPlaying)
- {
- UnityEngine.Debug.LogError(string.Concat(new object[]
- {
- "Unable to convert ",
- value.GetType(),
- " to ",
- this.GetPropertyType()
- }));
- }
- }
- else
- {
- if (this.mField != null)
- {
- this.mField.SetValue(this.mTarget, value);
- return true;
- }
- if (this.mProperty.CanWrite)
- {
- this.mProperty.SetValue(this.mTarget, value, null);
- return true;
- }
- }
- return false;
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- private bool Cache()
- {
- if (this.mTarget != null && !string.IsNullOrEmpty(this.mName))
- {
- Type type = this.mTarget.GetType();
- this.mField = type.GetField(this.mName);
- this.mProperty = type.GetProperty(this.mName);
- }
- else
- {
- this.mField = null;
- this.mProperty = null;
- }
- return this.mField != null || this.mProperty != null;
- }
- private bool Convert(ref object value)
- {
- if (this.mTarget == null)
- {
- return false;
- }
- Type propertyType = this.GetPropertyType();
- Type from;
- if (value == null)
- {
- if (!propertyType.IsClass)
- {
- return false;
- }
- from = propertyType;
- }
- else
- {
- from = value.GetType();
- }
- return PropertyReference.Convert(ref value, from, propertyType);
- }
- public static bool Convert(Type from, Type to)
- {
- object obj = null;
- return PropertyReference.Convert(ref obj, from, to);
- }
- public static bool Convert(object value, Type to)
- {
- if (value == null)
- {
- value = null;
- return PropertyReference.Convert(ref value, to, to);
- }
- return PropertyReference.Convert(ref value, value.GetType(), to);
- }
- public static bool Convert(ref object value, Type from, Type to)
- {
- if (to.IsAssignableFrom(from))
- {
- return true;
- }
- if (to == typeof(string))
- {
- value = ((value == null) ? "null" : value.ToString());
- return true;
- }
- if (value == null)
- {
- return false;
- }
- float num2;
- if (to == typeof(int))
- {
- if (from == typeof(string))
- {
- int num;
- if (int.TryParse((string)value, out num))
- {
- value = num;
- return true;
- }
- }
- else if (from == typeof(float))
- {
- value = Mathf.RoundToInt((float)value);
- return true;
- }
- }
- else if (to == typeof(float) && from == typeof(string) && float.TryParse((string)value, out num2))
- {
- value = num2;
- return true;
- }
- return false;
- }
- [SerializeField]
- private Component mTarget;
- [SerializeField]
- private string mName;
- private FieldInfo mField;
- private PropertyInfo mProperty;
- private static int s_Hash = "PropertyBinding".GetHashCode();
- }
|