Example #1
0
        public static string SerliazeRootTask(BehaviorManager.BehaviorTree behaviorTree)
        {
            string serializationFilePath = "";

            if (null == behaviorTree)
            {
#if UNITY_EDITOR
                Debug.LogWarning("RootTaskSerializer.SerliazeRootTask ERROR : behaviorTree is NULL");
#endif
                return(serializationFilePath);
            }
            string behaviorTreeName = BehaviorTreeUtility.GetClassNameFromInstance(behaviorTree);
            string rootTaskName     = BehaviorTreeUtility.GetClassNameFromInstance(behaviorTree.rootTask);

            // 将行为树-根结点键值对存进map
            BehaviorTreeNamewithRootTaskNameMap.AddPairValue(behaviorTreeName, rootTaskName);

            // 将map刷新到外存
            BTNameAndRTNameDic instance = new BTNameAndRTNameDic();
            instance.BTNamRTNameDic = BehaviorTreeNamewithRootTaskNameMap.BTNameMapRTNameDic;
            ProtoSerializer.SerializeToFile(instance, ProtoSerializer.CONFIG_FILE_NAME);

            // TODO: 其实序列化的时候不需要泛型,只有反序列化才需要知道
            serializationFilePath = ProtoSerializer.SerializeToFile(behaviorTree.rootTask, behaviorTreeName);
            return(serializationFilePath);
        }
        /// <summary>
        /// 传入带命名空间的类名,反序列化出该类
        /// </summary>
        /// <param name="behaviorName"></param>
        /// <param name="behaviorTree"></param>
        public static void Load(string behaviorName, ref BehaviorManager.BehaviorTree behaviorTree)
        {
            if (null == behaviorTree.rootTask)
            {
                // 读取BTName-RTName配置信息
                string             ConfigDicFilePath = ProtoSerializer.SERIALIZATION_CACHE_DATA_PATH + ProtoSerializer.CONFIG_FILE_NAME + ProtoSerializer.DATA_POSTFIX;
                BTNameAndRTNameDic instance          = new BTNameAndRTNameDic();
                ProtoSerializer.DeserializeFromFile(ConfigDicFilePath, typeof(BTNameAndRTNameDic), ref instance);
                BehaviorTreeNamewithRootTaskNameMap.BTNameMapRTNameDic = instance.BTNamRTNameDic;

                // 反序列化根结点
                string filePath     = ProtoSerializer.SERIALIZATION_CACHE_DATA_PATH + behaviorName + ProtoSerializer.DATA_POSTFIX;
                string rootTaskName = "";

                if (BehaviorTreeNamewithRootTaskNameMap.BTNameMapRTNameDic.ContainsKey(behaviorName))
                {
                    rootTaskName = BehaviorTreeNamewithRootTaskNameMap.BTNameMapRTNameDic[behaviorName];
                }
                Type type = Type.GetType(rootTaskName);
                ProtoSerializer.DeserializeFromFile(filePath, type, ref behaviorTree.rootTask);

                // 构建行为树结构
                behaviorTree.InitalBTStructureData(behaviorTree.rootTask);
            }

            if (BehaviorManager.instance.isEnableLog)
            {
                LogChildren(behaviorTree.rootTask);
            }
        }