News Forums Sample Projects, How To’s, and Code Samples Importing behavior tree as XML in Unityscript or C#?

This topic contains 1 reply, has 2 voices, and was last updated by  Sigil 3 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #31764

    Jester
    Keymaster

    Someone on Facebook wrote:

    “Hello, we have seen that in one of the latests actualizations of Rain you have added the option of importing the behavior tree as XML. This action (importing a behavior tree) can be done programatically with a method in Unityscript or C#?”

    #31765

    Sigil
    Keymaster

    The most straightforward way of loading a tree straight from XML would be to create a BTAsset and set the XML on it, then you can assign that to any AI’s BasicMind.

    I made this custom script to load arbitrary tree XML (I generated my test XML from an actual tree in the editor):

    
    using RAIN.BehaviorTrees;
    using RAIN.Core;
    using RAIN.Minds;
    using System.Collections.Generic;
    using UnityEngine;
    public class CustomTreeLoader : MonoBehaviour
    {
        [SerializeField]
        private TextAsset _treeXML;
        void Start()
        {
            // Create the asset
            BTAsset tCustomAsset = ScriptableObject.CreateInstance<BTAsset>();
            // Assign the tree xml, mine was exported from another tree and saved within my project
            tCustomAsset.SetTreeData(_treeXML.text);
            // You don't have to set these if they are empty, just here for example
            tCustomAsset.SetTreeBindings(new string[0] { });
            // My rig was on the same object
            AIRig tRig = GetComponentInChildren<AIRig>();
            ((BasicMind)tRig.AI.Mind).SetBehavior(tCustomAsset, new List<BTAssetBinding>());
        }
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.