PropertyReference.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using UnityEngine;
  5. [Serializable]
  6. public class PropertyReference
  7. {
  8. public PropertyReference()
  9. {
  10. }
  11. public PropertyReference(Component target, string fieldName)
  12. {
  13. this.mTarget = target;
  14. this.mName = fieldName;
  15. }
  16. public Component target
  17. {
  18. get
  19. {
  20. return this.mTarget;
  21. }
  22. set
  23. {
  24. this.mTarget = value;
  25. this.mProperty = null;
  26. this.mField = null;
  27. }
  28. }
  29. public string name
  30. {
  31. get
  32. {
  33. return this.mName;
  34. }
  35. set
  36. {
  37. this.mName = value;
  38. this.mProperty = null;
  39. this.mField = null;
  40. }
  41. }
  42. public bool isValid
  43. {
  44. get
  45. {
  46. return this.mTarget != null && !string.IsNullOrEmpty(this.mName);
  47. }
  48. }
  49. public bool isEnabled
  50. {
  51. get
  52. {
  53. if (this.mTarget == null)
  54. {
  55. return false;
  56. }
  57. MonoBehaviour monoBehaviour = this.mTarget as MonoBehaviour;
  58. return monoBehaviour == null || monoBehaviour.enabled;
  59. }
  60. }
  61. public Type GetPropertyType()
  62. {
  63. if (this.mProperty == null && this.mField == null && this.isValid)
  64. {
  65. this.Cache();
  66. }
  67. if (this.mProperty != null)
  68. {
  69. return this.mProperty.PropertyType;
  70. }
  71. if (this.mField != null)
  72. {
  73. return this.mField.FieldType;
  74. }
  75. return typeof(void);
  76. }
  77. public override bool Equals(object obj)
  78. {
  79. if (obj == null)
  80. {
  81. return !this.isValid;
  82. }
  83. if (obj is PropertyReference)
  84. {
  85. PropertyReference propertyReference = obj as PropertyReference;
  86. return this.mTarget == propertyReference.mTarget && string.Equals(this.mName, propertyReference.mName);
  87. }
  88. return false;
  89. }
  90. public override int GetHashCode()
  91. {
  92. return PropertyReference.s_Hash;
  93. }
  94. public void Set(Component target, string methodName)
  95. {
  96. this.mTarget = target;
  97. this.mName = methodName;
  98. }
  99. public void Clear()
  100. {
  101. this.mTarget = null;
  102. this.mName = null;
  103. }
  104. public void Reset()
  105. {
  106. this.mField = null;
  107. this.mProperty = null;
  108. }
  109. public override string ToString()
  110. {
  111. return PropertyReference.ToString(this.mTarget, this.name);
  112. }
  113. public static string ToString(Component comp, string property)
  114. {
  115. if (!(comp != null))
  116. {
  117. return null;
  118. }
  119. string text = comp.GetType().ToString();
  120. int num = text.LastIndexOf('.');
  121. if (num > 0)
  122. {
  123. text = text.Substring(num + 1);
  124. }
  125. if (!string.IsNullOrEmpty(property))
  126. {
  127. return text + "." + property;
  128. }
  129. return text + ".[property]";
  130. }
  131. [DebuggerHidden]
  132. [DebuggerStepThrough]
  133. public object Get()
  134. {
  135. if (this.mProperty == null && this.mField == null && this.isValid)
  136. {
  137. this.Cache();
  138. }
  139. if (this.mProperty != null)
  140. {
  141. if (this.mProperty.CanRead)
  142. {
  143. return this.mProperty.GetValue(this.mTarget, null);
  144. }
  145. }
  146. else if (this.mField != null)
  147. {
  148. return this.mField.GetValue(this.mTarget);
  149. }
  150. return null;
  151. }
  152. [DebuggerHidden]
  153. [DebuggerStepThrough]
  154. public bool Set(object value)
  155. {
  156. if (this.mProperty == null && this.mField == null && this.isValid)
  157. {
  158. this.Cache();
  159. }
  160. if (this.mProperty == null && this.mField == null)
  161. {
  162. return false;
  163. }
  164. if (value == null)
  165. {
  166. try
  167. {
  168. if (this.mProperty == null)
  169. {
  170. this.mField.SetValue(this.mTarget, null);
  171. return true;
  172. }
  173. if (this.mProperty.CanWrite)
  174. {
  175. this.mProperty.SetValue(this.mTarget, null, null);
  176. return true;
  177. }
  178. }
  179. catch (Exception)
  180. {
  181. return false;
  182. }
  183. }
  184. if (!this.Convert(ref value))
  185. {
  186. if (Application.isPlaying)
  187. {
  188. UnityEngine.Debug.LogError(string.Concat(new object[]
  189. {
  190. "Unable to convert ",
  191. value.GetType(),
  192. " to ",
  193. this.GetPropertyType()
  194. }));
  195. }
  196. }
  197. else
  198. {
  199. if (this.mField != null)
  200. {
  201. this.mField.SetValue(this.mTarget, value);
  202. return true;
  203. }
  204. if (this.mProperty.CanWrite)
  205. {
  206. this.mProperty.SetValue(this.mTarget, value, null);
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. [DebuggerHidden]
  213. [DebuggerStepThrough]
  214. private bool Cache()
  215. {
  216. if (this.mTarget != null && !string.IsNullOrEmpty(this.mName))
  217. {
  218. Type type = this.mTarget.GetType();
  219. this.mField = type.GetField(this.mName);
  220. this.mProperty = type.GetProperty(this.mName);
  221. }
  222. else
  223. {
  224. this.mField = null;
  225. this.mProperty = null;
  226. }
  227. return this.mField != null || this.mProperty != null;
  228. }
  229. private bool Convert(ref object value)
  230. {
  231. if (this.mTarget == null)
  232. {
  233. return false;
  234. }
  235. Type propertyType = this.GetPropertyType();
  236. Type from;
  237. if (value == null)
  238. {
  239. if (!propertyType.IsClass)
  240. {
  241. return false;
  242. }
  243. from = propertyType;
  244. }
  245. else
  246. {
  247. from = value.GetType();
  248. }
  249. return PropertyReference.Convert(ref value, from, propertyType);
  250. }
  251. public static bool Convert(Type from, Type to)
  252. {
  253. object obj = null;
  254. return PropertyReference.Convert(ref obj, from, to);
  255. }
  256. public static bool Convert(object value, Type to)
  257. {
  258. if (value == null)
  259. {
  260. value = null;
  261. return PropertyReference.Convert(ref value, to, to);
  262. }
  263. return PropertyReference.Convert(ref value, value.GetType(), to);
  264. }
  265. public static bool Convert(ref object value, Type from, Type to)
  266. {
  267. if (to.IsAssignableFrom(from))
  268. {
  269. return true;
  270. }
  271. if (to == typeof(string))
  272. {
  273. value = ((value == null) ? "null" : value.ToString());
  274. return true;
  275. }
  276. if (value == null)
  277. {
  278. return false;
  279. }
  280. float num2;
  281. if (to == typeof(int))
  282. {
  283. if (from == typeof(string))
  284. {
  285. int num;
  286. if (int.TryParse((string)value, out num))
  287. {
  288. value = num;
  289. return true;
  290. }
  291. }
  292. else if (from == typeof(float))
  293. {
  294. value = Mathf.RoundToInt((float)value);
  295. return true;
  296. }
  297. }
  298. else if (to == typeof(float) && from == typeof(string) && float.TryParse((string)value, out num2))
  299. {
  300. value = num2;
  301. return true;
  302. }
  303. return false;
  304. }
  305. [SerializeField]
  306. private Component mTarget;
  307. [SerializeField]
  308. private string mName;
  309. private FieldInfo mField;
  310. private PropertyInfo mProperty;
  311. private static int s_Hash = "PropertyBinding".GetHashCode();
  312. }