BehaviorTreeArgsDict.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. using System;
  2. using System.Collections.Generic;
  3. using Base;
  4. using UnityEngine;
  5. namespace Model
  6. {
  7. [Serializable]
  8. public class BehaviorTreeArgsDict: Dictionary<string, ValueBase>
  9. {
  10. public void SetKeyValueComp(Type type, string fieldName, object value)
  11. {
  12. if (IsStringType(type))
  13. {
  14. SetStrValue(fieldName, (string) value);
  15. }
  16. else if (IsObjectType(type))
  17. {
  18. SetObjectValue(fieldName, type, (UnityEngine.Object) value);
  19. }
  20. else if (IsIntType(type))
  21. {
  22. int intValue = 0;
  23. int.TryParse(value.ToString(), out intValue);
  24. SetIntValue(fieldName, intValue);
  25. }
  26. else if (IsLongType(type))
  27. {
  28. long longValue = 0;
  29. long.TryParse(value.ToString(), out longValue);
  30. SetLongValue(fieldName, longValue);
  31. }
  32. else if (IsFloatType(type))
  33. {
  34. float floatValue = 0;
  35. float.TryParse(value.ToString(), out floatValue);
  36. SetFloatValue(fieldName, floatValue);
  37. }
  38. else if (IsDoubleType(type))
  39. {
  40. double doubleValue = 0;
  41. double.TryParse(value.ToString(), out doubleValue);
  42. SetDoubleValue(fieldName, doubleValue);
  43. }
  44. else if (IsBoolType(type))
  45. {
  46. bool boolValue = false;
  47. bool.TryParse(value.ToString(), out boolValue);
  48. SetBoolValue(fieldName, boolValue);
  49. }
  50. else if (IsStringArrType(type))
  51. {
  52. SetStrArrValue(fieldName, (string[]) value);
  53. }
  54. else if (IsIntArrType(type))
  55. {
  56. SetIntArrValue(fieldName, (int[]) value);
  57. }
  58. else if (IsLongArrType(type))
  59. {
  60. SetLongArrValue(fieldName, (long[]) value);
  61. }
  62. else if (IsFloatArrType(type))
  63. {
  64. SetFloatArrValue(fieldName, (float[]) value);
  65. }
  66. else if (IsDoubleArrType(type))
  67. {
  68. SetDoubleArrValue(fieldName, (double[]) value);
  69. }
  70. else if (IsEnumType(type))
  71. {
  72. SetEnumValue(fieldName, value.ToString());
  73. }
  74. else if (IsObjectArrayType(type))
  75. {
  76. SetObjectArrayValue(fieldName, type, (UnityEngine.Object[]) value);
  77. }
  78. else
  79. {
  80. Log.Error($"行为树节点暂时未支持此类型:{type}!");
  81. }
  82. }
  83. private void SetConvertebleValue(string fieldName, float value)
  84. {
  85. if (this.ContainsKey(fieldName))
  86. {
  87. this[fieldName].Convertivle = value;
  88. }
  89. else
  90. {
  91. ValueBase valueBase = new ValueBase();
  92. valueBase.Convertivle = value;
  93. this.Add(fieldName, valueBase);
  94. }
  95. }
  96. public object GetTreeDictValue(Type fieldType, string fieldName)
  97. {
  98. if (!this.ContainsKey(fieldName))
  99. {
  100. Log.Error($"fieldName:{fieldName} 不存在!!!!");
  101. return null;
  102. }
  103. ValueBase obj = this[fieldName];
  104. return obj.GetValueByType(fieldType);
  105. }
  106. /// <summary>
  107. /// judge
  108. /// </summary>
  109. /// <param nodeName="type"></param>
  110. /// <returns></returns>
  111. public static bool IsStringType(Type type)
  112. {
  113. return typeof (string) == type;
  114. }
  115. public static bool IsBoolType(Type type)
  116. {
  117. return typeof (bool) == type;
  118. }
  119. public static bool IsIntType(Type type)
  120. {
  121. return typeof (int) == type;
  122. }
  123. public static bool IsLongType(Type type)
  124. {
  125. return typeof (long) == type;
  126. }
  127. public static bool IsFloatType(Type type)
  128. {
  129. return typeof (float) == type;
  130. }
  131. public static bool IsDoubleType(Type type)
  132. {
  133. return typeof (double) == type;
  134. }
  135. public static bool IsIntArrType(Type type)
  136. {
  137. return typeof (int[]) == type;
  138. }
  139. public static bool IsLongArrType(Type type)
  140. {
  141. return typeof (long[]) == type;
  142. }
  143. public static bool IsFloatArrType(Type type)
  144. {
  145. return typeof (float[]) == type;
  146. }
  147. public static bool IsDoubleArrType(Type type)
  148. {
  149. return typeof (double[]) == type;
  150. }
  151. public static bool IsStringArrType(Type type)
  152. {
  153. return typeof (string[]) == type;
  154. }
  155. public static bool IsObjectType(Type fieldType)
  156. {
  157. Type objecType = typeof (UnityEngine.Object);
  158. if (fieldType == objecType || fieldType.IsSubclassOf(objecType))
  159. {
  160. return true;
  161. }
  162. return false;
  163. }
  164. public static bool IsObjectArrayType(Type fieldType)
  165. {
  166. if (fieldType == typeof (UnityEngine.Object[]) || fieldType == typeof (GameObject[]) || fieldType == typeof (Material[]) ||
  167. fieldType == typeof (Texture[]) || fieldType == typeof (Texture2D[]) || fieldType == typeof (Texture3D[]) || fieldType == typeof (Shader[]) ||
  168. fieldType == typeof (AudioClip[]) || fieldType == typeof (Sprite[]))
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. public static bool IsConvertble(Type type)
  175. {
  176. return type == typeof (IConvertible);
  177. }
  178. public static bool IsAudioClipType(Type fieldType)
  179. {
  180. return fieldType == typeof (AudioClip);
  181. }
  182. public static bool IsMaterialType(Type fieldType)
  183. {
  184. return fieldType == typeof (Material);
  185. }
  186. public static bool IsGameObjectType(Type fieldType)
  187. {
  188. return fieldType == typeof (GameObject);
  189. }
  190. public static bool IsShaderType(Type fieldType)
  191. {
  192. return fieldType == typeof (Shader);
  193. }
  194. public static bool IsTextureType(Type fieldType)
  195. {
  196. return fieldType == typeof (Texture);
  197. }
  198. public static bool IsTexture2DType(Type fieldType)
  199. {
  200. return fieldType == typeof (Texture2D);
  201. }
  202. public static bool IsTexture3DType(Type fieldType)
  203. {
  204. return fieldType == typeof (Texture3D);
  205. }
  206. public static bool IsGameObjectArrayType(Type fieldType)
  207. {
  208. return fieldType == typeof (GameObject[]);
  209. }
  210. public static bool IsMaterialArrayType(Type fieldType)
  211. {
  212. return fieldType == typeof (Material[]);
  213. }
  214. public static bool IsTextureArrayType(Type fieldType)
  215. {
  216. return fieldType == typeof (Texture[]);
  217. }
  218. public static bool IsTexture2DArrayType(Type fieldType)
  219. {
  220. return fieldType == typeof (Texture2D[]);
  221. }
  222. public static bool IsTexture3DArrayType(Type fieldType)
  223. {
  224. return fieldType == typeof (Texture3D[]);
  225. }
  226. public static bool IsShaderArrayType(Type fieldType)
  227. {
  228. return fieldType == typeof (Shader[]);
  229. }
  230. public static bool IsAudioClipArrayType(Type fieldType)
  231. {
  232. return fieldType == typeof (AudioClip[]);
  233. }
  234. public static bool IsUnitConfigArrayType(Type fieldType)
  235. {
  236. return false;
  237. }
  238. public static bool IsSpriteArrayType(Type fieldType)
  239. {
  240. return fieldType == typeof (Sprite[]);
  241. }
  242. public static bool IsEnumType(Type fieldType)
  243. {
  244. Type enumType = typeof (Enum);
  245. if (fieldType == enumType || fieldType.IsSubclassOf(enumType))
  246. {
  247. return true;
  248. }
  249. return false;
  250. }
  251. /// <summary>
  252. /// Set Value
  253. /// </summary>
  254. /// <param nodeName="fieldName"></param>
  255. /// <param nodeName="value"></param>
  256. public void SetStrValue(string fieldName, string value)
  257. {
  258. if (this.ContainsKey(fieldName))
  259. {
  260. this[fieldName].StringValue = value;
  261. }
  262. else
  263. {
  264. ValueBase valueBase = new ValueBase();
  265. valueBase.StringValue = value;
  266. this.Add(fieldName, valueBase);
  267. }
  268. }
  269. public void SetIntValue(string fieldName, int value)
  270. {
  271. if (this.ContainsKey(fieldName))
  272. {
  273. this[fieldName].Int32Value = value;
  274. }
  275. else
  276. {
  277. ValueBase valueBase = new ValueBase();
  278. valueBase.Int32Value = value;
  279. this.Add(fieldName, valueBase);
  280. }
  281. }
  282. public void SetLongValue(string fieldName, long value)
  283. {
  284. if (this.ContainsKey(fieldName))
  285. {
  286. this[fieldName].Int64Value = value;
  287. }
  288. else
  289. {
  290. ValueBase valueBase = new ValueBase();
  291. valueBase.Int64Value = value;
  292. this.Add(fieldName, valueBase);
  293. }
  294. }
  295. public void SetFloatValue(string fieldName, float value)
  296. {
  297. if (this.ContainsKey(fieldName))
  298. {
  299. this[fieldName].SingleValue = value;
  300. }
  301. else
  302. {
  303. ValueBase valueBase = new ValueBase();
  304. valueBase.SingleValue = value;
  305. this.Add(fieldName, valueBase);
  306. }
  307. }
  308. public void SetDoubleValue(string fieldName, double value)
  309. {
  310. if (this.ContainsKey(fieldName))
  311. {
  312. this[fieldName].DoubleValue = value;
  313. }
  314. else
  315. {
  316. ValueBase valueBase = new ValueBase();
  317. valueBase.DoubleValue = value;
  318. this.Add(fieldName, valueBase);
  319. }
  320. }
  321. public void SetBoolValue(string fieldName, bool value)
  322. {
  323. if (this.ContainsKey(fieldName))
  324. {
  325. this[fieldName].BooleanValue = value;
  326. }
  327. else
  328. {
  329. ValueBase valueBase = new ValueBase();
  330. valueBase.BooleanValue = value;
  331. this.Add(fieldName, valueBase);
  332. }
  333. }
  334. public void SetStrArrValue(string fieldName, string[] value)
  335. {
  336. if (this.ContainsKey(fieldName))
  337. {
  338. this[fieldName].StringArray = value;
  339. }
  340. else
  341. {
  342. ValueBase valueBase = new ValueBase();
  343. valueBase.StringArray = value;
  344. this.Add(fieldName, valueBase);
  345. }
  346. }
  347. public void SetIntArrValue(string fieldName, int[] value)
  348. {
  349. if (this.ContainsKey(fieldName))
  350. {
  351. this[fieldName].Int32Array = value;
  352. }
  353. else
  354. {
  355. ValueBase valueBase = new ValueBase();
  356. valueBase.Int32Array = value;
  357. this.Add(fieldName, valueBase);
  358. }
  359. }
  360. public void SetLongArrValue(string fieldName, long[] value)
  361. {
  362. if (this.ContainsKey(fieldName))
  363. {
  364. this[fieldName].Int64Array = value;
  365. }
  366. else
  367. {
  368. ValueBase valueBase = new ValueBase();
  369. valueBase.Int64Array = value;
  370. this.Add(fieldName, valueBase);
  371. }
  372. }
  373. public void SetFloatArrValue(string fieldName, float[] value)
  374. {
  375. if (this.ContainsKey(fieldName))
  376. {
  377. this[fieldName].SingleArray = value;
  378. }
  379. else
  380. {
  381. ValueBase valueBase = new ValueBase();
  382. valueBase.SingleArray = value;
  383. this.Add(fieldName, valueBase);
  384. }
  385. }
  386. public void SetDoubleArrValue(string fieldName, double[] value)
  387. {
  388. if (this.ContainsKey(fieldName))
  389. {
  390. this[fieldName].DoubleArray = value;
  391. }
  392. else
  393. {
  394. ValueBase valueBase = new ValueBase();
  395. valueBase.DoubleArray = value;
  396. this.Add(fieldName, valueBase);
  397. }
  398. }
  399. public void SetObjectValue(string fieldName, Type type, UnityEngine.Object value)
  400. {
  401. if (this.ContainsKey(fieldName))
  402. {
  403. this[fieldName]?.SetValueByType(type, value);
  404. }
  405. else
  406. {
  407. ValueBase valueBase = new ValueBase();
  408. if (value != null)
  409. {
  410. valueBase.SetValueByType(type, value);
  411. }
  412. this.Add(fieldName, valueBase);
  413. }
  414. }
  415. public void SetEnumValue(string fieldName, string value)
  416. {
  417. if (this.ContainsKey(fieldName))
  418. {
  419. this[fieldName].enumValue = value;
  420. }
  421. else
  422. {
  423. ValueBase valueBase = new ValueBase();
  424. valueBase.enumValue = value;
  425. this.Add(fieldName, valueBase);
  426. }
  427. }
  428. public void SetObjectArrayValue(string fieldName, Type type, UnityEngine.Object[] value)
  429. {
  430. if (this.ContainsKey(fieldName))
  431. {
  432. this[fieldName]?.SetValueByType(type, value);
  433. }
  434. else
  435. {
  436. ValueBase valueBase = new ValueBase();
  437. if (value != null)
  438. {
  439. valueBase.SetValueByType(type, value);
  440. }
  441. this.Add(fieldName, valueBase);
  442. }
  443. }
  444. public static GameObject[] ConvertToGameObjectArray(UnityEngine.Object[] objectArray)
  445. {
  446. if (objectArray == null)
  447. {
  448. return null;
  449. }
  450. GameObject[] newObjectArray = new GameObject[objectArray.Length];
  451. for (int i = 0; i < objectArray.Length; i++)
  452. {
  453. newObjectArray[i] = (GameObject) objectArray[i];
  454. }
  455. return newObjectArray;
  456. }
  457. public static Material[] ConvertToMaterialArray(UnityEngine.Object[] objectArray)
  458. {
  459. if (objectArray == null)
  460. {
  461. return null;
  462. }
  463. Material[] newObjectArray = new Material[objectArray.Length];
  464. for (int i = 0; i < objectArray.Length; i++)
  465. {
  466. newObjectArray[i] = (Material) objectArray[i];
  467. }
  468. return newObjectArray;
  469. }
  470. public static Texture[] ConvertToTextureArray(UnityEngine.Object[] objectArray)
  471. {
  472. if (objectArray == null)
  473. {
  474. return null;
  475. }
  476. Texture[] newObjectArray = new Texture[objectArray.Length];
  477. for (int i = 0; i < objectArray.Length; i++)
  478. {
  479. newObjectArray[i] = (Texture) objectArray[i];
  480. }
  481. return newObjectArray;
  482. }
  483. public static Texture2D[] ConvertToTexture2DArray(UnityEngine.Object[] objectArray)
  484. {
  485. if (objectArray == null)
  486. {
  487. return null;
  488. }
  489. Texture2D[] newObjectArray = new Texture2D[objectArray.Length];
  490. for (int i = 0; i < objectArray.Length; i++)
  491. {
  492. newObjectArray[i] = (Texture2D) objectArray[i];
  493. }
  494. return newObjectArray;
  495. }
  496. public static Texture3D[] ConvertToTexture3DArray(UnityEngine.Object[] objectArray)
  497. {
  498. if (objectArray == null)
  499. {
  500. return null;
  501. }
  502. Texture3D[] newObjectArray = new Texture3D[objectArray.Length];
  503. for (int i = 0; i < objectArray.Length; i++)
  504. {
  505. newObjectArray[i] = (Texture3D) objectArray[i];
  506. }
  507. return newObjectArray;
  508. }
  509. public static Shader[] ConvertToShaderArray(UnityEngine.Object[] objectArray)
  510. {
  511. if (objectArray == null)
  512. {
  513. return null;
  514. }
  515. Shader[] newObjectArray = new Shader[objectArray.Length];
  516. for (int i = 0; i < objectArray.Length; i++)
  517. {
  518. newObjectArray[i] = (Shader) objectArray[i];
  519. }
  520. return newObjectArray;
  521. }
  522. public static AudioClip[] ConvertToAudioClipArray(UnityEngine.Object[] objectArray)
  523. {
  524. if (objectArray == null)
  525. {
  526. return null;
  527. }
  528. AudioClip[] newObjectArray = new AudioClip[objectArray.Length];
  529. for (int i = 0; i < objectArray.Length; i++)
  530. {
  531. newObjectArray[i] = (AudioClip) objectArray[i];
  532. }
  533. return newObjectArray;
  534. }
  535. public static Sprite[] ConvertToSpriteArray(UnityEngine.Object[] objectArray)
  536. {
  537. if (objectArray == null)
  538. {
  539. return null;
  540. }
  541. Sprite[] newObjectArray = new Sprite[objectArray.Length];
  542. for (int i = 0; i < objectArray.Length; i++)
  543. {
  544. newObjectArray[i] = (Sprite) objectArray[i];
  545. }
  546. return newObjectArray;
  547. }
  548. public static bool SatisfyCondition(GameObject go, Type[] constraintTypes)
  549. {
  550. if (go == null || constraintTypes == null || constraintTypes.Length <= 0)
  551. {
  552. return true;
  553. }
  554. foreach (var constraint in constraintTypes)
  555. {
  556. if (go.GetComponent(constraint) == null)
  557. {
  558. Log.Error($"此GameObject必须包含:{constraint}");
  559. return false;
  560. }
  561. }
  562. return true;
  563. }
  564. }
  565. }