BehaviorTreeArgsDict.cs 18 KB

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