Exemple #1
0
        private IEnumerator waitForBundlesToLoad <TAsset>(string key, string assetPath, List <AssetRequest <AssetBundle> > bundleRequests, bool pinBundles, AsyncAssetBundleRequest <TAsset> assetRequest, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            yield return(new CompositeCoroutineReturn(bundleRequests.ToArray()));

            _ = bundleRequests[0];
            BundleMount[]         bundleMounts               = new BundleMount[bundleRequests.Count];
            bool                  isMountSuccessful          = true;
            HashSet <BundleMount> successfullyMountedBundles = new HashSet <BundleMount>();

            for (int i = 0; i < bundleRequests.Count; i++)
            {
                AssetRequest <AssetBundle> assetRequest2 = bundleRequests[i];
                if (!bundleManager.IsMounted(assetRequest2.Key))
                {
                    if (assetRequest2.Asset != null)
                    {
                        successfullyMountedBundles.Add(bundleMounts[i] = bundleManager.MountBundle(assetRequest2.Key, assetRequest2.Asset, pinBundles));
                    }
                    else
                    {
                        isMountSuccessful = false;
                        Log.LogErrorFormatted(this, "Bundle asset for bundle {0} was null", assetRequest2.Key);
                    }
                }
                else
                {
                    bundleMounts[i] = bundleManager.GetBundle(assetRequest2.Key);
                }
                activeMountRequests.Remove(assetRequest2.Key);
            }
            if (isMountSuccessful)
            {
                BundleMount rootMount = bundleMounts[0];
                if (!assetPath.EndsWith(".unity"))
                {
                    assetRequest.Request = rootMount.LoadAsync <TAsset>(key, assetPath).Request;
                    yield return(assetRequest);
                }
                else
                {
                    rootMount.IsPinned    = true;
                    assetRequest.Finished = true;
                }
            }
            else
            {
                Log.LogErrorFormatted(this, "Mount was not successful, asset {0} will be null", key);
                foreach (BundleMount item in successfullyMountedBundles)
                {
                    bundleManager.UnmountBundle(item.Key, unloadAllLoadedObjects: true);
                }
            }
            handler?.Invoke(key, assetRequest.Asset);
            for (int i = 0; i < bundleRequests.Count; i++)
            {
                AssetRequest <AssetBundle> assetRequest2 = bundleRequests[i];
                assetRequest2.Dispose();
            }
        }
        private IEnumerator waitForTextAssetToLoad <TAsset>(AsyncAssetRequest <TAsset> assetRequest, AssetRequest <TextAsset> textRequest, string key, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            yield return(textRequest);

            byte[] bytes = textRequest.Asset.bytes;
            assetRequest.Request = new IndexedAssetRequest <TAsset>(key, (TAsset)(object)bytes);
            yield return(assetRequest);

            handler?.Invoke(key, (TAsset)(object)bytes);
        }
Exemple #3
0
        private IEnumerator waitForStringToLoad <TAsset>(AsyncAssetRequest <TAsset> assetRequest, AssetRequest <string> stringRequest, string key, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            yield return(stringRequest);

            string      jsonString = stringRequest.Asset;
            JsonService json       = Service.Get <JsonService>();
            TAsset      jsonAsset  = json.Deserialize <TAsset>(jsonString);

            assetRequest.Request = new IndexedAssetRequest <TAsset>(key, jsonAsset);
            handler?.Invoke(key, jsonAsset);
        }
        private IEnumerator loadBundleFromStreamingAssets <TAsset>(ContentManifest.AssetEntry entry, StreamingAssetBundleWrapper wrapper, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            string key = entry.Key;

            wrapper.LoadFromDownload(Path.Combine(streamingAssetsPath, entry.Key + ".txt"));
            yield return(wrapper.WebRequest);

            AssetBundle assetBundle = wrapper.AssetBundle;

            handler?.Invoke(key, (TAsset)(object)assetBundle);
            yield return(null);
        }
        private IEnumerator waitForFileStreamAssetToLoad <TAsset>(AsyncAssetRequest <TAsset> assetRequest, string key, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            string fileText;

            using (StreamReader streamReader = new StreamReader(key))
            {
                fileText = streamReader.ReadToEnd();
            }
            assetRequest.Request = new IndexedAssetRequest <TAsset>(key, (TAsset)(object)fileText);
            yield return(assetRequest);

            handler?.Invoke(key, (TAsset)(object)fileText);
        }
        private IEnumerator waitForAssetToLoad <TAsset>(string key, string assetPath, AsyncAssetBundleRequest <TAsset> assetRequest, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            AssetBundleRequest unityRequest = (assetRequest.Request = Bundle.LoadAssetAsync <TAsset>(assetPath));

            yield return(unityRequest);

            activeRequests.Remove(key);
            handler?.Invoke(key, (TAsset)(object)unityRequest.asset);
            if (this.EFinishedLoading != null && activeRequests.Count == 0)
            {
                yield return(null);

                this.EFinishedLoading();
            }
        }
        private IEnumerator waitForFileStreamAssetToLoad <TAsset>(AsyncAssetRequest <TAsset> assetRequest, string key, AssetLoadedHandler <TAsset> handler) where TAsset : class
        {
            FileStream fileStream = new FileStream(key, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, useAsync: true);

            byte[] bytes;
            try
            {
                bytes = new byte[fileStream.Length];
                IAsyncResult asyncResult = fileStream.BeginRead(bytes, 0, bytes.Length, null, null);
                while (!asyncResult.IsCompleted)
                {
                    yield return(null);
                }
                fileStream.EndRead(asyncResult);
            }
            finally
            {
                fileStream.Close();
            }
            assetRequest.Request = new IndexedAssetRequest <TAsset>(key, (TAsset)(object)bytes);
            yield return(assetRequest);

            handler?.Invoke(key, (TAsset)(object)bytes);
        }