NodeProto.cs 3.5 KB

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