BehaviorTreeArgsDict.cs 5.6 KB

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