Beispiel #1
0
        public static IEnumerator LoadAssets(bool simple, LoadHandler onProgress = null, LoadHandler onComplete = null)
        {
            var watcher = TimeWatcher.Watch("AssetManager.LoadAssets");

            m_downloadAssetHashMap = HashListFile.ParseAssetHashListFile(hashFileName);

            var main = LoadManifest();

            yield return(main);

            watcher.See("LoadManifest");

            if (main != null)
            {
                m_manifest = main.GetAsset <AssetBundleManifest>();
            }

            var assets = simple ? m_preloadAssets : m_baseAssets;
            int i = 1, count = assets.Length + 1;

            foreach (var asset in assets)
            {
                var op = LoadAssetAsync(asset, typeof(Object));

                onProgress?.Invoke(asset, 0, count, ++i);
                yield return(op);

                watcher.See("Load {0}", asset);
            }
            onComplete?.Invoke("", 1.0f, count, count);
            watcher.See("Load Assets");
            watcher.UnWatch(false);

            Logger.LogDetail($"AssetManager: Assets load complete! {m_assets.Count} assets loaded.");
            Logger.LogDetail($"AssetManager: Current Data Hash: <b><color=#EE6600>{dataHash}</color></b>");
        }
Beispiel #2
0
        /// <summary>
        ///     Loads data using identifier.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="defaultValue">Default Value.</param>
        /// <param name="encode">Load encrypted data? (set it to true if you have used encryption in save)</param>
        /// <param name="password">Encryption Password.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="encoder">Encoder.</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="path">Path.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static T Load <T>(string identifier, T defaultValue, bool encode, string password,
                                 ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding, SaveGamePath path)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (serializer == null)
            {
                serializer = Serializer;
            }
            if (encoding == null)
            {
                encoding = DefaultEncoding;
            }
            if (defaultValue == null)
            {
                defaultValue = default(T);
            }
            var result   = defaultValue;
            var filePath = "";

            if (!IsFilePath(identifier))
            {
                switch (path)
                {
                default:
                case SaveGamePath.PersistentDataPath:
                    filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier);
                    break;

                case SaveGamePath.DataPath:
                    filePath = string.Format("{0}/{1}", Application.dataPath, identifier);
                    break;
                }
            }
            else
            {
                filePath = identifier;
            }
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
            if (!Exists(filePath, path))
#else
            if (!Exists(filePath, path))
#endif
            {
                Debug.LogWarningFormat(
                    "The specified identifier ({1}) does not exists. please use Exists () to check for existent before calling Load.\n" +
                    "returning the default(T) instance.",
                    filePath,
                    identifier);
                return(result);
            }

            Stream stream = null;
            if (encode)
            {
                var data = "";
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    data = encoding.GetString(UnityEngine.Windows.File.ReadAllBytes(filePath));
#else
                    data = File.ReadAllText(filePath, encoding);
#endif
                }
                else
                {
                    data = PlayerPrefs.GetString(filePath);
                }
#else
                data = PlayerPrefs.GetString(filePath);
#endif
                var decoded = encoder.Decode(data, password);
                stream = new MemoryStream(encoding.GetBytes(decoded), true);
            }
            else
            {
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    stream = new MemoryStream(UnityEngine.Windows.File.ReadAllBytes(filePath));
#else
                    stream = File.OpenRead(filePath);
#endif
                }
                else
                {
                    var data = PlayerPrefs.GetString(filePath);
                    stream = new MemoryStream(encoding.GetBytes(data));
                }
#else
                string data = PlayerPrefs.GetString(filePath);
                stream = new MemoryStream(encoding.GetBytes(data));
#endif
            }

            result = serializer.Deserialize <T>(stream, encoding);
            stream.Dispose();
            if (result == null)
            {
                result = defaultValue;
            }
            if (LoadCallback != null)
            {
                LoadCallback.Invoke(
                    result,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
            if (OnLoaded != null)
            {
                OnLoaded(
                    result,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
            return(result);
        }