Example #1
0
        /// <summary>
        /// 从指定文件(完整路径名)反序列化出指定类型的对象
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool DeserializeFromFile<T>(string filePath, Type type, ref T obj)
        {
            bool result = true;

            if ("" == filePath)
            {
#if UNITY_EDITOR
                Debug.LogWarning("ProtoSerializer.Deserialize: The filePath is not allowed empty when serializing from filestream");
#endif
                return false;
            }

            try
            {
                using (FileStream fileStream = File.OpenRead(filePath))
                {
                    // 由于泛型未知,只能根据Type实例来调用ProtoBuf的Serlizer类的泛型函数Deserialize
                    obj = (T)(BehaviorTreeUtility.CallGenericFunctionByType(typeof(Serializer), "Deserialize", new object[] { fileStream }, type));             
                }
            }
            catch (System.Exception)
            {
#if UNITY_EDITOR
#if UNITY_EDITOR
                Debug.LogWarning("ProtoSerializer.Serialize: Can NOT ReadData " + filePath +
#endif
                    ", because exception is thrown when finding or reading the file.");
#endif
            }

            return result;
        }
Example #2
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);
        }
Example #3
0
        // Just Serialize the listed BehaviorTree
        void OnWizardOtherButton()
        {
            // If list is empty, then exit
            if (0 == m_behaviorTreesList.Count)
            {
#if UNITY_EDITOR
                Debug.LogWarning("NO BehaviorTree IS SELECTED");
                helpString = "NO BehaviorTree IS SELECTED";
#endif
                return;
            }

            // Cycle and  Serlialize
            for (int i = 0; i < m_behaviorTreesList.Count; i++)
            {
                if (null == m_behaviorTreesList[i])
                {
                    continue;
                }
                TextAsset asset = m_behaviorTreesList[i];

                className = BehaviorTreeUtility.GetClassNameFromTextAsset(asset);
                Type type = BehaviorTreeUtility.GetAssemblyQualifiedType(className);
                SerlalizeBT(type);
            }
        }
Example #4
0
        /// <summary>
        /// 序列化对象到指定文件内,成功将返回序列化文件路径,失败返回空路径
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string SerializeToFile(object obj, string fileName)
        {
            string serializationFilePath = "";

            if (null == obj)
            {
#if UNITY_EDITOR
                Debug.LogWarning("ProtoSerializer.Serialize : The object to serialize is NULL");
#endif
                return serializationFilePath;
            }

            // 目录路径
            string directoryPath = BehaviorTreeUtility.GetDirectorPathFromCompleteFilePath(fileName);
            if ("" == directoryPath)
            {
                directoryPath = SERIALIZATION_CACHE_DATA_PATH;
            }
            else
            {
                // 配置文件多一层目录
                directoryPath = SERIALIZATION_CACHE_DATA_PATH + directoryPath;
            }

            serializationFilePath = SERIALIZATION_CACHE_DATA_PATH + fileName + DATA_POSTFIX;

            byte[] messageArr;

            // 获取待序列化对象的类名
            string className = BehaviorTreeUtility.GetClassNameFromInstance(obj);
            // 获取待序列化对象的类型
            Type type = BehaviorTreeUtility.GetAssemblyQualifiedType(className);

            if (false == Directory.Exists(directoryPath))
            {
                try
                {
                    Directory.CreateDirectory(directoryPath);
                }
                catch (System.Exception)
                {
                    serializationFilePath = "";
#if UNITY_EDITOR
                    Debug.LogWarning("ProtoSerializer.Serialize : Can NOT SaveData because exception is thrown when creating folder!");
#endif
                }
            }

            // try to read the serilization data
            try
            {
                using (FileStream fileStream = File.Create(serializationFilePath))
                {
                    using (MemoryStream messageStream = new MemoryStream())
                    {
                        // 由于泛型未知,只能根据Type实例来调用ProtoBuf的Serlizer类的泛型函数Serialize
                        BehaviorTreeUtility.CallGenericFunctionByType(typeof(Serializer), "Serialize", new object[] { messageStream, obj }, type);
                        messageArr = messageStream.ToArray();

                        fileStream.Write(messageArr, 0, messageArr.Length);
                        fileStream.Close();
                    }
                }
	        }
            catch (System.Exception)
            {
#if UNITY_EDITOR
#if UNITY_EDITOR
                Debug.LogWarning("ProtoSerializer.Serialize : Can NOT SaveData " + serializationFilePath +
#endif
                    ", because exception is thrown when creating the file.");
#endif

                serializationFilePath = "";
            }
            return serializationFilePath;
        }
Example #5
0
        private void SerlalizeBT(Type type)
        {
            if (typeof(BehaviorManager.BehaviorTree) == type.BaseType)
            {
                BehaviorManager.BehaviorTree instance = Activator.CreateInstance(type) as BehaviorManager.BehaviorTree;

                // the type is invalide
                if (null == instance)
                {
#if UNITY_EDITOR
                    Debug.Log("instance is null");
#endif
                    BehaviorManager.BehaviorTree instanceWithAQN = Activator.CreateInstance(BehaviorTreeUtility.GetAssemblyQualifiedType(type.FullName)) as BehaviorManager.BehaviorTree;
                    if (null != instanceWithAQN)
                    {
#if UNITY_EDITOR
                        Debug.Log("Serilize the BehaviorTree : " + type.FullName);
                        BehaviorTreeSerialize.Save(instanceWithAQN);
#endif
                    }
                }
                else
                {
#if UNITY_EDITOR
                    Debug.Log("Serilize the BehaviorTree : " + type.FullName);
                    BehaviorTreeSerialize.Save(instance);
#endif
                }
            }
        }