Beispiel #1
0
 void Init()
 {
     _bt       = ScriptableObject.CreateInstance <BehaviorTree>();
     _bt.Entry = _bt.CreateTask <EntryTask>();
     Serialize();
 }
Beispiel #2
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);
        }