BehaviorTreeComponent.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace Model
  6. {
  7. [ObjectEvent]
  8. public class BehaviorTreeComponentEvent : ObjectEvent<BehaviorTreeComponent>, IAwake, ILoad
  9. {
  10. public void Awake()
  11. {
  12. this.Get().Awake();
  13. }
  14. public void Load()
  15. {
  16. this.Get().Load();
  17. }
  18. }
  19. public class BehaviorTreeComponent : Component
  20. {
  21. public static BehaviorTreeComponent Instance;
  22. private Dictionary<string, Func<NodeProto, Node>> dictionary;
  23. private Dictionary<GameObject, BehaviorTree> treeCache;
  24. public void Awake()
  25. {
  26. Instance = this;
  27. this.Load();
  28. }
  29. public void Load()
  30. {
  31. this.dictionary = new Dictionary<string, Func<NodeProto, Node>>();
  32. this.treeCache = new Dictionary<GameObject, BehaviorTree>();
  33. Type[] types = DllHelper.GetMonoTypes();
  34. foreach (Type type in types)
  35. {
  36. object[] attrs = type.GetCustomAttributes(typeof(NodeAttribute), false);
  37. if (attrs.Length == 0)
  38. {
  39. continue;
  40. }
  41. Type classType = type;
  42. if (this.dictionary.ContainsKey(type.Name))
  43. {
  44. throw new Exception($"已经存在同类节点: {classType.Name}");
  45. }
  46. this.dictionary.Add(type.Name, config =>
  47. {
  48. Node node = (Node)Activator.CreateInstance(classType, config);
  49. try
  50. {
  51. InitFieldValue(ref node, config);
  52. }
  53. catch (Exception e)
  54. {
  55. throw new Exception($"InitFieldValue error, node: {node.Id} {node.Type}", e);
  56. }
  57. return node;
  58. });
  59. }
  60. }
  61. private static void InitFieldValue(ref Node node, NodeProto nodeProto)
  62. {
  63. Type type = ObjectEvents.Instance.Get("Model").GetType("Model." + nodeProto.Name);
  64. foreach (var args_item in nodeProto.Args.Dict())
  65. {
  66. FieldInfo fieldInfo = type.GetField(args_item.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  67. if (fieldInfo == null)
  68. {
  69. continue;
  70. }
  71. NewSetValue(ref node, fieldInfo, args_item.Value);
  72. }
  73. }
  74. private static void NewSetValue(ref Node node, FieldInfo field, object value)
  75. {
  76. // unity enum无法序列化,保存的string形式
  77. if (field.FieldType.IsEnum)
  78. {
  79. value = Enum.Parse(field.FieldType, (string) value);
  80. }
  81. if (field.FieldType.IsArray)
  82. {
  83. if (field.FieldType.GetElementType().IsSubclassOf(typeof(UnityEngine.Object)))
  84. {
  85. Array sourceArray = (Array) value;
  86. Array dest = Array.CreateInstance(field.FieldType.GetElementType(), sourceArray.Length);
  87. Array.Copy(sourceArray, dest, dest.Length);
  88. field.SetValue(node, dest);
  89. value = dest;
  90. }
  91. }
  92. field.SetValue(node, value);
  93. }
  94. private Node CreateOneNode(NodeProto proto)
  95. {
  96. if (!this.dictionary.ContainsKey(proto.Name))
  97. {
  98. throw new KeyNotFoundException($"NodeType没有定义该节点: {proto.Name}");
  99. }
  100. return this.dictionary[proto.Name](proto);
  101. }
  102. private Node CreateTreeNode(NodeProto proto)
  103. {
  104. Node node = this.CreateOneNode(proto);
  105. node.EndInit(this.GetParent<Scene>());
  106. if (proto.Children == null)
  107. {
  108. return node;
  109. }
  110. foreach (NodeProto nodeProto in proto.Children)
  111. {
  112. Node childNode = this.CreateTreeNode(nodeProto);
  113. node.AddChild(childNode);
  114. }
  115. return node;
  116. }
  117. public BehaviorTree CreateTree(Scene scene, GameObject treeGo)
  118. {
  119. return this.CreateTree(scene, 0, treeGo);
  120. }
  121. public BehaviorTree CreateTree(Scene scene, long ownerId, GameObject treeGo)
  122. {
  123. try
  124. {
  125. if (treeGo == null)
  126. {
  127. return null;
  128. }
  129. BehaviorTree tree;
  130. if (this.treeCache.TryGetValue(treeGo, out tree))
  131. {
  132. return tree;
  133. }
  134. BehaviorTreeConfig behaviorTreeConfig = treeGo.GetComponent<BehaviorTreeConfig>();
  135. Node node = this.CreateTreeNode(behaviorTreeConfig.RootNodeProto);
  136. tree = new BehaviorTree(scene, ownerId, node) {GameObjectId = treeGo.GetInstanceID()};
  137. if (Define.IsAsync)
  138. {
  139. this.treeCache.Add(treeGo, tree);
  140. }
  141. return tree;
  142. }
  143. catch (Exception e)
  144. {
  145. throw new Exception($"行为树配置错误: {treeGo.name}", e);
  146. }
  147. }
  148. }
  149. }