BehaviorTreeComponent.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. private Dictionary<string, Func<NodeProto, Node>> dictionary;
  22. private Dictionary<GameObject, BehaviorTree> treeCache;
  23. public void Awake()
  24. {
  25. this.Load();
  26. }
  27. public void Load()
  28. {
  29. this.dictionary = new Dictionary<string, Func<NodeProto, Node>>();
  30. this.treeCache = new Dictionary<GameObject, BehaviorTree>();
  31. Type[] types = DllHelper.GetMonoTypes();
  32. foreach (Type type in types)
  33. {
  34. object[] attrs = type.GetCustomAttributes(typeof(NodeAttribute), false);
  35. if (attrs.Length == 0)
  36. {
  37. continue;
  38. }
  39. Type classType = type;
  40. if (this.dictionary.ContainsKey(type.Name))
  41. {
  42. throw new Exception($"已经存在同类节点: {classType.Name}");
  43. }
  44. this.dictionary.Add(type.Name, config =>
  45. {
  46. Node node = (Node)Activator.CreateInstance(classType, config);
  47. try
  48. {
  49. InitFieldValue(ref node, config);
  50. }
  51. catch (Exception e)
  52. {
  53. throw new Exception($"InitFieldValue error, node: {node.Id} {node.Type}", e);
  54. }
  55. return node;
  56. });
  57. }
  58. }
  59. private static void InitFieldValue(ref Node node, NodeProto nodeProto)
  60. {
  61. Type type = AssemblyManager.Instance.Get("Model").GetType("Model." + nodeProto.Name);
  62. foreach (var args_item in nodeProto.Args.Dict())
  63. {
  64. FieldInfo fieldInfo = type.GetField(args_item.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  65. if (fieldInfo == null)
  66. {
  67. continue;
  68. }
  69. NewSetValue(ref node, fieldInfo, args_item.Value);
  70. }
  71. }
  72. private static void NewSetValue(ref Node node, FieldInfo field, object value)
  73. {
  74. // unity enum无法序列化,保存的string形式
  75. if (field.FieldType.IsEnum)
  76. {
  77. value = Enum.Parse(field.FieldType, (string) value);
  78. }
  79. field.SetValue(node, value);
  80. }
  81. private Node CreateOneNode(NodeProto proto)
  82. {
  83. if (!this.dictionary.ContainsKey(proto.Name))
  84. {
  85. throw new KeyNotFoundException($"NodeType没有定义该节点: {proto.Name}");
  86. }
  87. return this.dictionary[proto.Name](proto);
  88. }
  89. private Node CreateTreeNode(NodeProto proto)
  90. {
  91. Node node = this.CreateOneNode(proto);
  92. node.EndInit(this.GetOwner<Scene>());
  93. if (proto.Children == null)
  94. {
  95. return node;
  96. }
  97. foreach (NodeProto nodeProto in proto.Children)
  98. {
  99. Node childNode = this.CreateTreeNode(nodeProto);
  100. node.AddChild(childNode);
  101. }
  102. return node;
  103. }
  104. public BehaviorTree CreateTree(Scene scene, GameObject treeGo)
  105. {
  106. try
  107. {
  108. BehaviorTree tree;
  109. if (this.treeCache.TryGetValue(treeGo, out tree))
  110. {
  111. return tree;
  112. }
  113. BehaviorTreeConfig behaviorTreeConfig = treeGo.GetComponent<BehaviorTreeConfig>();
  114. Node node = this.CreateTreeNode(behaviorTreeConfig.RootNodeProto);
  115. tree = new BehaviorTree(scene, node);
  116. if (Define.IsAsync)
  117. {
  118. this.treeCache.Add(treeGo, tree);
  119. }
  120. return tree;
  121. }
  122. catch (Exception e)
  123. {
  124. throw new Exception($"行为树配置错误: {treeGo.name}", e);
  125. }
  126. }
  127. }
  128. }