Esempio n. 1
0
        /// <summary>
        /// [异常]:ArgumentException
        /// </summary>
        Task Deserialize(XmlElement element, BehaviorTree bt)
        {
            if (element.Name != "Task")
            {
                Debug.LogError("wrong data format");
                return(null);
            }

            // 创建task
            string className = element.GetAttribute("ClassName");
            Task   taskObj   = bt.CreateTask(className);

            if (taskObj == null)
            {
                Debug.LogError(string.Format("fail to create task instance for '{0}'.", className));
                return(null);
            }

            // 尝试获取task对应的taskParam元数据
            FieldInfo taskParamsInfo = taskObj.GetType().GetField("taskParams", BindingFlags.Public | BindingFlags.Instance);

            if (taskParamsInfo != null)
            {
                // 获取xml节点属性的TaskParams内容
                string taskParamsVal = element.GetAttribute("TaskParams");
                if (string.IsNullOrEmpty(taskParamsVal))
                {
                    Debug.LogError("invalid TaskParams.");
                }
                else
                {
                    // 反序列化TaskParams
                    System.Type type          = taskParamsInfo.GetValue(taskObj).GetType(); // 注意这里不能用paramsInfo.FieldType,否则只会得到基类的Type
                    object      taskParamsObj = JsonUtility.FromJson(taskParamsVal, type);
                    //HandleShareVariableDeserialization(taskParamsObj, bt);
                    HandleSharedVariableDeserialization(taskParamsObj, bt);
                    taskParamsInfo.SetValue(taskObj, taskParamsObj);
                    HandleExternalBTDeserialization(taskObj); // 注意:要放在taskParamsInfo.SetValue(taskObj, taskParamsObj)后调用
                }
            }

            // 获取task对应的nodeData元数据
            FieldInfo nodeDataInfo = taskObj.GetType().GetField("nodeData", BindingFlags.Public | BindingFlags.Instance);

            if (nodeDataInfo != null)
            {
                // 获取xml节点属性的nodeData内容
                string nodeDataVal = element.GetAttribute("NodeData");
                if (string.IsNullOrEmpty(nodeDataVal))
                {
                    Debug.LogError("invalid NodeData.");
                }
                else
                {
                    // 反序列化NodeData
                    System.Type type        = nodeDataInfo.FieldType;
                    object      nodeDataObj = JsonUtility.FromJson(nodeDataVal, type);
                    nodeDataInfo.SetValue(taskObj, nodeDataObj);
                }
            }
            else
            {
                Debug.LogError(string.Format("fail to get the nodeData field in {0}", taskObj.GetType()));
            }

            // 递归处理子节点
            foreach (XmlElement subEle in element.ChildNodes)
            {
                if (subEle.Name == "Task")
                {
                    (taskObj as ParentTask).AddChild(Deserialize(subEle, bt) as Task);
                    continue;
                }
                else
                {
                    Debug.LogError("wrong data format");
                }
            }

            return(taskObj);
        }
Esempio n. 2
0
 void Init()
 {
     _bt       = ScriptableObject.CreateInstance <BehaviorTree>();
     _bt.Entry = _bt.CreateTask <EntryTask>();
     Serialize();
 }
Esempio n. 3
0
        /// <summary>
        /// [摘要]:从内部XML字符串反序列化BehaviorTree对象
        /// </summary>
        public bool Deserialize(string xml, Transform transform = null)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.LoadXml(xml);
            }
            catch (System.Xml.XmlException)
            {
                return(false);
            }

            _bt           = ScriptableObject.CreateInstance <BehaviorTree>();
            _bt.Transform = transform; // 编辑期可为null

            // 处理bt属性和blackboard
            XmlElement btEle = doc.GetElementsByTagName("BehaviorTree")[0] as XmlElement;

            _bt.btParams = JsonUtility.FromJson <BehaviorTreeParams>(btEle.GetAttribute("BTParams"));

            /*XmlElement blackboardEle = doc.GetElementsByTagName("Blackboard")[0] as XmlElement;
             * List<string> keys = JsonUtility.FromJson<List<string>>(blackboardEle.GetAttribute("Keys"));
             * foreach (var key in keys)
             * {
             *  _bt.Blackboard.Set(key, null);
             * }*/

            // 处理trunk
            XmlElement entryEle = doc.GetElementsByTagName("Trunk")[0].FirstChild as XmlElement;

            try
            {
                _bt.Entry = Deserialize(entryEle, _bt) as EntryTask;
            }
            catch (System.ArgumentException)
            {
                return(false);
            }

            // 处理apartBranches
            XmlElement apartBranchesEle = (XmlElement)doc.GetElementsByTagName("ApartBranches").Item(0);

            foreach (XmlElement apartBranch in apartBranchesEle.ChildNodes)
            {
                try
                {
                    Deserialize(apartBranch, _bt);
                }
                catch (System.ArgumentException)
                {
                    return(false);
                }
            }

            /*if (Dirty)
             * {
             *  // 比如反序列化时因为全局黑板条目的删减修改了Shared变量的name值
             *  Serialize();
             *  Dirty = false;
             * }*/
            return(true);
        }