BehaviorTreeComponent.cs 3.1 KB

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