Ejemplo n.º 1
0
        BehaviorTreeDesc IBehaviorTreeLoader.LoadTree(string path)
        {
            var asset = Resources.Load(path) as BehaviorTreeAsset;

            if (asset == null)
            {
                return(null);
            }

            return(BehaviorTreeSerializer.DeserializeDesc(asset.bytes));
        }
Ejemplo n.º 2
0
        private void Awake()
        {
            BehaviorTreeSerializer.Initialize(new FlatBuffersSerializer());

            treeManager = new BehaviorTreeManager(this);

            if (asset == null)
            {
                return;
            }

            var treeDesc = BehaviorTreeSerializer.DeserializeDesc(asset.bytes);

            tree = treeManager.CreateTree(this, treeDesc);
        }
Ejemplo n.º 3
0
        public void Deserialize()
        {
            if (Domain != null)
            {
                return;
            }

            var repo    = new EditorRepository();
            var handler = new BehaviorTreeCommandHandler();
            var domain  = new EditorDomain(repo, handler);

            domain.OnEventApplied += Domain_OnEventApplied;

            var treeId = BehaviorTreeState.GetId();
            var tree   = new BehaviorTreeState(domain, treeId);

            tree.Asset          = this;
            repo.States[treeId] = tree;

            if (bytes != null && bytes.Length > 0)
            {
                var treeDesc = BehaviorTreeSerializer.DeserializeDesc(bytes);
                tree.Title      = treeDesc.Title;
                tree.Comment    = treeDesc.Comment;
                tree.RootTaskId = treeDesc.RootTaskId;

                if (treeDesc.Tasks != null && treeDesc.Tasks.Length > 0)
                {
                    tree.NextTaskId = treeDesc.Tasks.Max(t => t.Id) + 1;

                    foreach (var taskDesc in treeDesc.Tasks)
                    {
                        var taskId = TaskState.GetId(taskDesc.Id);
                        var task   = new TaskState(domain, taskId);
                        if (taskDesc.Id != treeDesc.RootTaskId)
                        {
                            task.ParentTaskId = EditorHelper.FindParentTask(treeDesc.Tasks, taskDesc.Id).Id;
                        }
                        task.Desc            = taskDesc;
                        repo.States[task.Id] = task;
                    }
                }
            }

            Domain = domain;
            Tree   = tree;
        }
        public void TestSerialization()
        {
            BehaviorTreeSerializer.Initialize(new FlatBuffersSerializer());

            var tree1 = BuildTreeDesc();

            var data1 = BehaviorTreeSerializer.SerializeDesc(tree1);

            Assert.IsTrue(data1.Length > 0);

            var tree2 = BehaviorTreeSerializer.DeserializeDesc(data1);

            var task1 = tree2.Tasks[0];

            Assert.IsTrue(task1.CustomDesc is SequenceTaskDesc);

            var task2 = tree2.Tasks[1];

            Assert.IsTrue(task2.CustomDesc is LogTaskDesc);

            var task3 = tree2.Tasks[2];

            Assert.IsTrue(task3.CustomDesc is WaitTimerTaskDesc);
        }