BehaviorTreeArgsDict.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Base;
  5. using UnityEngine;
  6. namespace Model
  7. {
  8. [Serializable]
  9. public class BehaviorTreeArgsDict
  10. {
  11. private readonly Dictionary<string, object> dict = new Dictionary<string, object>();
  12. public void Add(string key, object value)
  13. {
  14. this.dict.Add(key, value);
  15. }
  16. public void Remove(string key)
  17. {
  18. this.dict.Remove(key);
  19. }
  20. public bool ContainsKey(string key)
  21. {
  22. return this.dict.ContainsKey(key);
  23. }
  24. public Dictionary<string, object> Dict()
  25. {
  26. return this.dict;
  27. }
  28. public BehaviorTreeArgsDict Clone()
  29. {
  30. BehaviorTreeArgsDict behaviorTreeArgsDict = new BehaviorTreeArgsDict();
  31. foreach (KeyValuePair<string, object> keyValuePair in this.dict)
  32. {
  33. behaviorTreeArgsDict.Add(keyValuePair.Key, Clone(keyValuePair.Value));
  34. }
  35. return behaviorTreeArgsDict;
  36. }
  37. public static object Clone(object obj)
  38. {
  39. Type vType = obj.GetType();
  40. if (!vType.IsSubclassOf(typeof(Array)))
  41. {
  42. return obj;
  43. }
  44. Array sourceArray = (Array)obj;
  45. Array dest = Array.CreateInstance(vType.GetElementType(), sourceArray.Length);
  46. Array.Copy(sourceArray, dest, dest.Length);
  47. return dest;
  48. }
  49. public void SetKeyValueComp(string fieldName, object value)
  50. {
  51. try
  52. {
  53. this.dict[fieldName] = value;
  54. }
  55. catch (Exception e)
  56. {
  57. Log.Error($"SetKeyValueComp error: {fieldName} {e}");
  58. }
  59. }
  60. public object GetTreeDictValue(string fieldName)
  61. {
  62. if (!this.dict.ContainsKey(fieldName))
  63. {
  64. //Log.Error($"fieldName:{fieldName} 不存在!!!!");
  65. return null;
  66. }
  67. return this.dict[fieldName];
  68. }
  69. /// <summary>
  70. /// judge
  71. /// </summary>
  72. /// <param nodeName="type"></param>
  73. /// <returns></returns>
  74. public static bool IsStringType(Type type)
  75. {
  76. return typeof (string) == type;
  77. }
  78. public static bool IsBoolType(Type type)
  79. {
  80. return typeof (bool) == type;
  81. }
  82. public static bool IsIntType(Type type)
  83. {
  84. return typeof (int) == type;
  85. }
  86. public static bool IsLongType(Type type)
  87. {
  88. return typeof (long) == type;
  89. }
  90. public static bool IsFloatType(Type type)
  91. {
  92. return typeof (float) == type;
  93. }
  94. public static bool IsDoubleType(Type type)
  95. {
  96. return typeof (double) == type;
  97. }
  98. public static bool IsIntArrType(Type type)
  99. {
  100. return typeof (int[]) == type;
  101. }
  102. public static bool IsLongArrType(Type type)
  103. {
  104. return typeof (long[]) == type;
  105. }
  106. public static bool IsFloatArrType(Type type)
  107. {
  108. return typeof (float[]) == type;
  109. }
  110. public static bool IsDoubleArrType(Type type)
  111. {
  112. return typeof (double[]) == type;
  113. }
  114. public static bool IsStringArrType(Type type)
  115. {
  116. return typeof (string[]) == type;
  117. }
  118. public static bool IsObjectType(Type fieldType)
  119. {
  120. Type objecType = typeof (UnityEngine.Object);
  121. if (fieldType == objecType || fieldType.IsSubclassOf(objecType))
  122. {
  123. return true;
  124. }
  125. return false;
  126. }
  127. public static bool IsObjectArrayType(Type fieldType)
  128. {
  129. if (fieldType == typeof (UnityEngine.Object[]) || fieldType == typeof (GameObject[]) || fieldType == typeof (Material[]) ||
  130. fieldType == typeof (Texture[]) || fieldType == typeof (Texture2D[]) || fieldType == typeof (Texture3D[]) || fieldType == typeof (Shader[]) ||
  131. fieldType == typeof (AudioClip[]) || fieldType == typeof (Sprite[]))
  132. {
  133. return true;
  134. }
  135. return false;
  136. }
  137. public static bool IsConvertble(Type type)
  138. {
  139. return type == typeof (IConvertible);
  140. }
  141. public static bool IsAudioClipType(Type fieldType)
  142. {
  143. return fieldType == typeof (AudioClip);
  144. }
  145. public static bool IsMaterialType(Type fieldType)
  146. {
  147. return fieldType == typeof (Material);
  148. }
  149. public static bool IsGameObjectType(Type fieldType)
  150. {
  151. return fieldType == typeof (GameObject);
  152. }
  153. public static bool IsShaderType(Type fieldType)
  154. {
  155. return fieldType == typeof (Shader);
  156. }
  157. public static bool IsTextureType(Type fieldType)
  158. {
  159. return fieldType == typeof (Texture);
  160. }
  161. public static bool IsTexture2DType(Type fieldType)
  162. {
  163. return fieldType == typeof (Texture2D);
  164. }
  165. public static bool IsTexture3DType(Type fieldType)
  166. {
  167. return fieldType == typeof (Texture3D);
  168. }
  169. public static bool IsGameObjectArrayType(Type fieldType)
  170. {
  171. return fieldType == typeof (GameObject[]);
  172. }
  173. public static bool IsMaterialArrayType(Type fieldType)
  174. {
  175. return fieldType == typeof (Material[]);
  176. }
  177. public static bool IsTextureArrayType(Type fieldType)
  178. {
  179. return fieldType == typeof (Texture[]);
  180. }
  181. public static bool IsTexture2DArrayType(Type fieldType)
  182. {
  183. return fieldType == typeof (Texture2D[]);
  184. }
  185. public static bool IsTexture3DArrayType(Type fieldType)
  186. {
  187. return fieldType == typeof (Texture3D[]);
  188. }
  189. public static bool IsShaderArrayType(Type fieldType)
  190. {
  191. return fieldType == typeof (Shader[]);
  192. }
  193. public static bool IsAudioClipArrayType(Type fieldType)
  194. {
  195. return fieldType == typeof (AudioClip[]);
  196. }
  197. public static bool IsUnitConfigArrayType(Type fieldType)
  198. {
  199. return false;
  200. }
  201. public static bool IsSpriteArrayType(Type fieldType)
  202. {
  203. return fieldType == typeof (Sprite[]);
  204. }
  205. public static bool IsEnumType(Type fieldType)
  206. {
  207. Type enumType = typeof (Enum);
  208. if (fieldType == enumType || fieldType.IsSubclassOf(enumType))
  209. {
  210. return true;
  211. }
  212. return false;
  213. }
  214. public static bool SatisfyCondition(GameObject go, Type[] constraintTypes)
  215. {
  216. if (go == null || constraintTypes == null || constraintTypes.Length <= 0)
  217. {
  218. return true;
  219. }
  220. foreach (var constraint in constraintTypes)
  221. {
  222. if (go.GetComponent(constraint) == null)
  223. {
  224. Log.Error($"此GameObject必须包含:{constraint}");
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. }
  231. }