예제 #1
0
 void Cleanup()
 {
     if (mRuntime != null)
     {
         StopService();
         mRuntime.Services.Clear();
         BehaviourTreeAsset.DestroyAsset(mRuntime);
         mRuntime = null;
     }
 }
            public BTNodeBase Instantiate(BehaviourTreeRunner btree, BehaviourTreeAsset asset)
            {
                BTNodeBase node = null;

                if (m_Type == EBTNodeType.task)
                {
                    node = new BTTask(m_Id, BehaviourLibrary.NewTask(m_Name, m_Id));
                }
                else if (m_Type == EBTNodeType.controller)
                {
                    node = BehaviourLibrary.NewController(m_Name, m_Id);
                }
                if (node != null)
                {
                    node.InitData(btree, m_JsonData);
                    node.InitDecoratorSize(m_Conditions == null ? 0 : m_Conditions.Length, m_Children == null ? 0 : m_Children.Length, m_Services == null ? 0 : m_Services.Length);
                    for (int i = 0; i < node.ConditionLength; i++)
                    {
                        BTData data = asset.GetDataById(m_Conditions[i]);
                        if (data == null)
                        {
                            continue;
                        }
                        node.SetNotFlag(i, data.m_NotFlag);
                        BTConditionBase cond = BehaviourLibrary.NewCondition(data.m_Name, data.m_Id);
                        if (cond != null)
                        {
                            cond.OnInitData(btree, data.m_JsonData);
                            node.SetCondition(i, cond);
                        }
                    }
                    for (int i = 0; i < node.ServiceLength; i++)
                    {
                        BTData data = asset.GetDataById(m_Services[i]);
                        if (data == null)
                        {
                            continue;
                        }
                        BTServiceBase serv = BehaviourLibrary.NewService(data.m_Name, data.m_Id);
                        if (serv != null)
                        {
                            serv.OnInitData(btree, data.m_JsonData);
                            node.SetService(i, serv);
                        }
                    }
                }
                return(node);
            }
예제 #3
0
        public override void OnInitData(BehaviourTreeRunner btree, string jsonData)
        {
            JObject obj = JsonConvert.DeserializeObject <JObject>(jsonData);

            mAssetPath = obj.Value <string>("asset");

            BehaviourTreeAsset asset = Resources.Load <BehaviourTreeAsset>(mAssetPath);

            if (!asset)
            {
                Debug.LogError("Can't load behaviour asset: " + mAssetPath);
                return;
            }
            BTNodeBase tree = asset.CreateBehaviourTree(btree);

            mLooper = new BehaviourLooper(tree);
        }
예제 #4
0
 public void SetAsset(BehaviourTreeAsset behaviourAsset)
 {
     if (behaviourAsset == mAsset)
     {
         return;
     }
     mAsset = behaviourAsset;
     if (mAsset != null)
     {
         mRootNode = mAsset.CreateBehaviourTree(this);
         mLooper   = new BehaviourLooper(mRootNode);
     }
     else
     {
         mRootNode = null;
         mLooper   = null;
     }
 }
예제 #5
0
        public BTNode Copy(BehaviourTreeAsset tree, int newid, Vector2 deltaPosition)
        {
            BTNode newnode = new BTNode();

            newnode.id         = newid;
            newnode.moduleName = moduleName;
            newnode.position   = position + deltaPosition;
            if (asset != null)
            {
                newnode.asset           = Object.Instantiate(asset);
                newnode.asset.name      = string.Format("{0}_{1}", asset.GetType().Name, newid);
                newnode.asset.TreeAsset = tree;
            }
            else
            {
                newnode.asset = null;
            }
            return(newnode);
        }
예제 #6
0
 public void GetBinders(BehaviourTreeAsset asset, ICollection <AssetBinder> binders)
 {
     if (asset == null || mAssetBinder == null || binders == null)
     {
         return;
     }
     mSearchStack.Push(mAssetBinder);
     while (mSearchStack.Count > 0)
     {
         var bind = mSearchStack.Pop();
         if (bind.Source == asset || bind.RuntimeTree == asset)
         {
             binders.Add(bind);
         }
         for (int i = 0; i < bind.SubAssetCount; i++)
         {
             mSearchStack.Push(bind.SubAssetAt(i));
         }
     }
 }
 public void EditorMergeTo(string path, BehaviourTreeAsset asset)
 {
     if (asset == this || asset == null)
     {
         return;
     }
     if (!string.IsNullOrEmpty(path))
     {
         asset.m_BTNodes.Clear();
         var assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(path);
         foreach (var t in assets)
         {
             if (t != asset)
             {
                 DestroyImmediate(t, true);
             }
         }
     }
     else
     {
         foreach (var t in asset.m_BTNodes)
         {
             if (t.Asset != null)
             {
                 DestroyImmediate(t.Asset, true);
             }
         }
         asset.m_BTNodes.Clear();
     }
     for (int i = 0; i < m_BTNodes.Count; i++)
     {
         var node = m_BTNodes[i].Clone(asset);
         asset.m_BTNodes.Add(node);
     }
     asset.m_Comment      = m_Comment;
     asset.m_RootId       = m_RootId;
     asset.m_Sorted       = m_Sorted;
     asset.m_RootPosition = m_RootPosition;
     UnityEditor.EditorUtility.SetDirty(asset);
 }
        public static void DestroyAsset(BehaviourTreeAsset btree, bool immediate = false)
        {
            if (btree == null)
            {
                return;
            }
#if UNITY_EDITOR
            var path = UnityEditor.AssetDatabase.GetAssetPath(btree);
#endif
            if (immediate)
            {
                foreach (var t in btree.m_BTNodes)
                {
                    if (t.Asset != null)
                    {
                        Object.DestroyImmediate(t.Asset, true);
                    }
                }
                DestroyImmediate(btree, true);
            }
            else
            {
                foreach (var t in btree.m_BTNodes)
                {
                    if (t.Asset != null)
                    {
                        Object.Destroy(t.Asset);
                    }
                }
                Destroy(btree);
            }
#if UNITY_EDITOR
            if (!string.IsNullOrEmpty(path))
            {
                UnityEditor.AssetDatabase.Refresh();
            }
#endif
        }
예제 #9
0
        public BTNode Clone(BehaviourTreeAsset tree)
        {
            BTNode newnode = new BTNode();

            newnode.id         = this.id;
            newnode.moduleName = moduleName;
            newnode.parentId   = this.parentId;
            if (children.Count > 0)
            {
                newnode.children.AddRange(children);
            }
            newnode.position = position;
            if (asset != null)
            {
                newnode.asset           = Object.Instantiate(asset);
                newnode.asset.name      = string.Format("{0}_{1}", asset.GetType().Name, id);
                newnode.asset.TreeAsset = tree;
            }
            else
            {
                newnode.asset = null;
            }
            return(newnode);
        }
예제 #10
0
 public void BindAsset(BehaviourTreeAsset asset, EResetMode loopMode)
 {
     if (mRunner == null)
     {
         return;
     }
     if (asset == null)
     {
         mAssetName = null;
         Cleanup();
         return;
     }
     mLoopMode = loopMode;
     if (asset == mSource)
     {
         return;
     }
     Cleanup();
     mAssetName = asset.name;
     mSource    = asset;
     mRuntime   = asset.Clone();
     mRuntime.Prepare(this);
     mLooper.SetBehaviuor(mRuntime.Root);
 }
예제 #11
0
        public void SetAsset(BehaviourTreeAsset behaviourAsset)
        {
            var bind = AssetBinder.GetBinder(this);

            bind.BindAsset(behaviourAsset, m_LoopMode);
        }
예제 #12
0
 public AssetBinder GetBinder(BehaviourTreeAsset asset)
 {
     return(GetBinder((x) => x.Source == asset || x.RuntimeTree == asset));
 }