NodeProto.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Model
  5. {
  6. [Serializable]
  7. public class StrValueBasePair
  8. {
  9. public string key;
  10. public ValueBase value;
  11. }
  12. [Serializable]
  13. public class NodeProto: ISerializationCallbackReceiver
  14. {
  15. public int nodeId;
  16. public string name;
  17. public string describe = "";
  18. public List<int> nodeIdList = new List<int>();
  19. public List<StrValueBasePair> args_list = new List<StrValueBasePair>();
  20. [NonSerialized]
  21. public BehaviorTreeArgsDict args_dict = new BehaviorTreeArgsDict();
  22. [NonSerialized]
  23. public List<NodeProto> children = new List<NodeProto>();
  24. public List<NodeProto> Children
  25. {
  26. get
  27. {
  28. return this.children;
  29. }
  30. set
  31. {
  32. this.children = value;
  33. }
  34. }
  35. public void SetValue<T>(string key, T value)
  36. {
  37. Type type = typeof (T);
  38. object newValue = value;
  39. if (type == typeof (Enum) || type.IsSubclassOf(typeof (Enum)))
  40. {
  41. type = typeof (string);
  42. newValue = value.ToString();
  43. }
  44. int index = args_list.FindIndex(item => { return item.key == key; });
  45. bool hasKey = index != -1;
  46. if (hasKey)
  47. {
  48. args_list[index].value.SetValueByType(typeof (T), newValue);
  49. }
  50. else
  51. {
  52. StrValueBasePair pair = new StrValueBasePair();
  53. pair.key = key;
  54. pair.value = new ValueBase();
  55. pair.value.SetValueByType(typeof (T), newValue);
  56. args_list.Add(pair);
  57. }
  58. if (!args_dict.ContainsKey(key))
  59. {
  60. args_dict.Add(key, new ValueBase());
  61. }
  62. args_dict[key].SetValueByType(typeof (T), newValue);
  63. }
  64. public T GetValue<T>(string key)
  65. {
  66. T value = default(T);
  67. foreach (var item in args_list)
  68. {
  69. if (item.key == key)
  70. {
  71. value = (T) item.value.GetValueByType(typeof (T));
  72. }
  73. }
  74. return value;
  75. }
  76. public void BuildChild(Dictionary<long, NodeProto> nodeDict)
  77. {
  78. this.children.Clear();
  79. foreach (long nid in nodeIdList)
  80. {
  81. if (nodeDict.ContainsKey(nid))
  82. {
  83. this.children.Add(nodeDict[nid]);
  84. }
  85. }
  86. foreach (var child in this.children)
  87. {
  88. child.BuildChild(nodeDict);
  89. }
  90. }
  91. public void OnAfterDeserialize()
  92. {
  93. this.args_dict.Clear();
  94. foreach (var item in this.args_list)
  95. {
  96. try
  97. {
  98. this.args_dict.Add(item.key, item.value);
  99. }
  100. catch (Exception e)
  101. {
  102. throw new ConfigException($"key 已存在: {this.name} {this.nodeId} {item.key}", e);
  103. }
  104. }
  105. }
  106. public void OnBeforeSerialize()
  107. {
  108. this.args_list.Clear();
  109. foreach (var item in this.args_dict)
  110. {
  111. StrValueBasePair value = new StrValueBasePair();
  112. value.key = item.Key;
  113. value.value = item.Value;
  114. this.args_list.Add(value);
  115. }
  116. }
  117. }
  118. }