예제 #1
0
        /// <summary>
        /// Entry-point for deserializing data from an previously serialized xml formatted string.
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="maxSupportedVer"></param>
        /// <returns></returns>
        public static object Deserialize(string xml, int maxSupportedVer, ITypeConverter typeConverter)
        {
            var deserializer = new XmlDeserializer(typeConverter);

            deserializer.Doc.LoadXml(xml);
            deserializer.DeferredIds = new Dictionary <int, List <DeferedReferenceMap> >();

            //preload built-in unity resources manifest lookup
            GameObject builtinGo = Resources.Load("BuiltinResources") as GameObject;

            SerializerBase.UnityResources = builtinGo.GetComponent <BuiltinResources>();

            deserializer.SetupStandardSurrogates();

            //run the deserializer
            string version = deserializer.Doc.DocumentElement.GetAttribute("version");

            if (maxSupportedVer < Convert.ToInt32(version))
            {
                return(null);
            }
            string culture = deserializer.Doc.DocumentElement.GetAttribute("culture");

            deserializer.cult = new CultureInfo(culture);
            object o = deserializer.DeserializeCore(deserializer.Doc.DocumentElement);

            deserializer.AssignDeferedData();

            //remove temporary information
            SerializerBase.ClearTempSurrogates();
            SerializerBase.UnityResources = null;
            Resources.UnloadUnusedAssets();//we may have loaded some manifests during serialization. Release now
            return(o);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <param name="maxSupportedVer"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public static T DeserializeComponent <T>(string xml, int maxSupportedVer, GameObject root) where T : Component
        {
            if (root == null)
            {
                throw new UnityException("Null GameObject parameter.");
            }
            //throw new UnityException("DeserilizeComponent<T> is not yet implemented.");

            var deserializer = new XmlDeserializer(null);

            deserializer.Doc.LoadXml(xml);
            deserializer.DeferredIds = new Dictionary <int, List <DeferedReferenceMap> >();

            //preload built-in unity resources manifest lookup
            GameObject builtinGo = Resources.Load("BuiltinResources") as GameObject;

            SerializerBase.UnityResources = builtinGo.GetComponent <BuiltinResources>();

            deserializer.SetupStandardSurrogates();

            T comp = root.GetComponent <T>();

            if (comp == null)
            {
                comp = root.AddComponent <T>();
            }

            //run the deserializer
            string version = deserializer.Doc.DocumentElement.GetAttribute("version");

            if (maxSupportedVer < Convert.ToInt32(version))
            {
                return(null);
            }
            string culture = deserializer.Doc.DocumentElement.GetAttribute("culture");

            deserializer.cult = new CultureInfo(culture);
            deserializer.DeserializeInPlace(comp, deserializer.Doc.DocumentElement);

            deserializer.AssignDeferedData();

            //remove temporary information
            SerializerBase.ClearTempSurrogates();
            SerializerBase.UnityResources = null;
            Resources.UnloadUnusedAssets();//we may have loaded some manifests during serialization. Release now
            return(comp);
        }