public static void CheckHasName(BehaviorTreeConfig config, string path, string nodeName) { if (HasNode(config.RootNodeProto, nodeName)) { Log.Info($"{nodeName}: {path}"); } }
public BTEditorTree(BehaviorTreeConfig config) { Type rootType = typeof(Game).Assembly.GetType($"Model.{config.RootNodeProto.Name}"); Node root = (Node)Activator.CreateInstance(rootType, config.RootNodeProto); root.Id = BTEditor.NodeIdStartIndex; Queue <NodeProto> protoStack = new Queue <NodeProto>(); Queue <Node> nodeStack = new Queue <Node>(); protoStack.Enqueue(config.RootNodeProto); nodeStack.Enqueue(root); while (protoStack.Count > 0) { NodeProto p = protoStack.Dequeue(); Node node = nodeStack.Dequeue(); foreach (KeyValuePair <string, object> argsItem in p.Args.Dict()) { FieldInfo fieldInfo = node.GetType().GetField(argsItem.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); fieldInfo.SetValue(node, argsItem.Value); } foreach (NodeProto child in p.Children) { Type t = typeof(Game).Assembly.GetType($"Model.{child.Name}"); Node childNode = (Node)Activator.CreateInstance(t, child); AddChild(childNode, node); protoStack.Enqueue(child); nodeStack.Enqueue(childNode); } } this.BTConfig = config; _root = root; }
public TreeEditor() { InitializeComponent(); activeConfig = new BehaviorTreeConfig(); RefreshAddControls(); }
public static void Serialize(this Node root, BehaviorTreeConfig config) { config.Clear(); BehaviorNodeConfig rootNp = config.AddRootNode(root.GetType().Name); Queue <Node> queue = new Queue <Node>(); Queue <BehaviorNodeConfig> npQue = new Queue <BehaviorNodeConfig>(); rootNp.describe = root.Description; queue.Enqueue(root); npQue.Enqueue(rootNp); while (queue.Count > 0) { Node cur = queue.Dequeue(); BehaviorNodeConfig np = npQue.Dequeue(); foreach (Node child in cur.GetChildren) { BehaviorNodeConfig childNp = GetNodeConfigFromNode(child); queue.Enqueue(child); npQue.Enqueue(childNp); config.AddChild(np, childNp); } } // PrintNode(root); // PrintConfigNode(config.RootNodeConfig); }
public BehaviorTreeData BehaviorTreeConfigToTreeData(BehaviorTreeConfig config) { BehaviorTreeData tree = new BehaviorTreeData(); tree.Root = NodeConfigToNodeData(config.RootNodeConfig); return(tree); }
private static BehaviorNodeConfig CreateNodeConfig(this BehaviorTreeConfig treeConfig, string name) { NodeMeta proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); GameObject go = new GameObject() { name = name }; go.transform.parent = treeConfig.gameObject.transform; BehaviorNodeConfig node = go.AddComponent <BehaviorNodeConfig>(); node.name = name; node.describe = proto.describe; foreach (NodeFieldDesc args in proto.new_args_desc) { Type type = BTTypeManager.GetBTType(args.type); UnityEngine.Component comp = go.AddComponent(type); FieldInfo info = type.GetField("fieldName"); info.SetValue(comp, args.name); FieldInfo info1 = type.GetField("fieldValue"); info1.SetValue(comp, args.value); } return(node); }
public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, string name) { BehaviorNodeConfig child = treeConfig.CreateNodeConfig(name); AddChild(treeConfig, parent, child); return(child); }
public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, BehaviorNodeConfig child) { child.transform.parent = parent.transform; child.transform.SetAsLastSibling(); child.GetComponent <BehaviorNodeConfig>().id = treeConfig.RootNodeId + treeConfig.AutoId; return(child.GetComponent <BehaviorNodeConfig>()); }
public BehaviorTreeConfig BehaviorTreeDataToConfig(BehaviorTreeData tree) { GameObject curTreeGo = Object.Instantiate(CurTreeGO); BehaviorTreeConfig config = curTreeGo.GetComponent <BehaviorTreeConfig>(); if (config == null) { config = curTreeGo.AddComponent <BehaviorTreeConfig>(); } foreach (Transform child in config.gameObject.transform) { Object.DestroyImmediate(child.gameObject); } try { config.RootNodeConfig = this.BehaviorNodeDataToNodeConfig(tree.Root); } catch (Exception e) { Log.Error($"tree name : {tree.BehaviorNodeData.Name} {e}"); } config.RootNodeConfig.gameObject.transform.parent = config.gameObject.transform; return(config); }
private void SavePrefabTree() { ResetTreeId(); BehaviorTreeConfig config = BehaviorTreeDataToConfig(CurTree); RenameTree(config); Object.DestroyImmediate(config.gameObject); }
public static BehaviorNodeConfig AddRootNode(this BehaviorTreeConfig treeConfig, string rootName) { BehaviorNodeConfig go = treeConfig.CreateNodeConfig(rootName); treeConfig.RootNodeConfig = go.GetComponent <BehaviorNodeConfig>(); treeConfig.RootNodeConfig.id = BTEditor.NodeIdStartIndex; go.gameObject.name = rootName; return(go); }
public static BehaviourTreeNodeProxy <T> AddNodeToLast <T>(BehaviorTreeConfig tree) where T : Node { BehaviorNodeConfig parent = tree.RootNodeConfig; string name = typeof(T).Name; BehaviorNodeConfig p = tree.AddChild(parent, name); BehaviourTreeNodeProxy <T> proxy = new BehaviourTreeNodeProxy <T>(p.ToNodeProto()); return(proxy); }
public static BTEditorTree OpenFromGameObject(GameObject source) { BehaviorTreeConfig sourceTree = source.GetComponent <BehaviorTreeConfig>(); if (sourceTree == null) { throw new Exception($"{source.name}预制中不包含行为树"); } return(new BTEditorTree(sourceTree)); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); BehaviorTreeConfig config = target as BehaviorTreeConfig; if (GUILayout.Button("打开行为树")) { BTEntity.Instance.OpenBehaviorEditor(config.gameObject); } EditorUtility.SetDirty(config); }
public static BTEditorTree CreateTree <T>(GameObject source) { BehaviorTreeConfig sourceTree = source.GetComponent <BehaviorTreeConfig>(); if (sourceTree == null) { sourceTree = source.AddComponent <BehaviorTreeConfig>(); } Node root = (Node)Activator.CreateInstance(typeof(T), new NodeProto()); BTEditorTree tree = new BTEditorTree(root, sourceTree); return(tree); }
public void Save() { if (IsPrefab(this.BTConfig.gameObject)) { GameObject go = UnityEngine.Object.Instantiate(this.BTConfig.gameObject); go.name = this.BTConfig.gameObject.name; BehaviorTreeConfig newConfig = go.GetComponent <BehaviorTreeConfig>(); _root.Serialize(newConfig); PrefabUtility.ReplacePrefab(go, this.BTConfig, ReplacePrefabOptions.ReplaceNameBased); UnityEngine.Object.DestroyImmediate(go); } else { _root.Serialize(this.BTConfig); } }
public void RenameTree(BehaviorTreeConfig config) { string newName = $"{config.RootNodeConfig.describe}"; if (!config.RootNodeConfig.describe.StartsWith("BT_")) { newName = $"BT_{config.RootNodeConfig.describe}"; } config.gameObject.name = newName; CurTreeGO = PrefabUtility.ReplacePrefab(config.gameObject, CurTreeGO, ReplacePrefabOptions.ReplaceNameBased); string prefabPath = AssetDatabase.GetAssetPath(CurTreeGO); string result = AssetDatabase.RenameAsset(prefabPath, newName); if (result.Contains("Invalid file name")) { Log.Error(result); } EditorUtility.SetDirty(config.gameObject); }
public static GameObject CreateTree(string path, string rootNodeName, string desc = "默认行为树") { GameObject prefab = null; try { GameObject go = new GameObject(); go.name = desc; prefab = PrefabUtility.CreatePrefab($"{path}/BT_{desc}.prefab", go, ReplacePrefabOptions.ReplaceNameBased); BehaviorTreeConfig config = prefab.AddComponent <BehaviorTreeConfig>(); config.AddRootNode(rootNodeName); EditorUtility.SetDirty(config); EditorUtility.SetDirty(prefab); Object.DestroyImmediate(go); } catch (Exception ex) { Log.Error($"创建行为树失败|{ex}"); } return(prefab); }
public static BTEditorTree Open(GameObject go) { BehaviorTreeConfig config = go.GetComponent <BehaviorTreeConfig>(); return(new BTEditorTree(config)); }
public static void SaveOneTree(BehaviorTreeConfig treeConfig, string treePath, params object[] paramList) { EditorUtility.SetDirty(treeConfig.gameObject); AssetDatabase.SaveAssets(); }
public void NewLoadPrefabTree() { BehaviorTreeConfig config = CurTreeGO.GetComponent <BehaviorTreeConfig>(); CurTree = BehaviorTreeConfigToTreeData(config); }
private BTEditorTree(Node root, BehaviorTreeConfig config) { _root = root; _root.Id = BTEditor.NodeIdStartIndex; this.BTConfig = config; }
public void SaveToBehaviorTreeConfig(BehaviorTreeConfig config) { _root.Serialize(config); }