Exemple #1
0
    public Object LoadResource(string path, int numRefs = 1)
    {
        //Debug.Log("LOAD: " + path);
        LoadedResource r;

        if (loadedResources.TryGetValue(path, out r))
        {
            r.NumReferences++;
            if (!r.Resource)
            {
                Debug.LogError("Found a null resource in cache");
            }
            return(r.Resource);
        }
        else
        {
            Object o = Resources.Load(path);
            if (!o)
            {
                Debug.LogWarning("No resource found at: " + path);
            }
            LoadedResource loadedResource = new LoadedResource()
            {
                Resource = o, NumReferences = numRefs
            };
            loadedResources.Add(path, loadedResource);
            return(o);
        }
    }
Exemple #2
0
        private IEnumerator PreloadAllAssetBundleAsync <T>(string cacheKey, string assetBundleName, string assetName) where T : UnityEngine.Object
        {
            int startFrameCount = Time.frameCount;

            AssetBundleLoadAllAssetRequest <T> request = AssetBundleManager.Instance.LoadAllAssetAsync <T>(assetBundleName, assetName);

            if (null == request)
            {
                yield break;
            }

            yield return(request);

            T[] assets = request.GetAsset();
            if (null == assets || assets.Length == 0)
            {
                yield break;
            }

            LoadedResource res = new LoadedResource(assets);

            m_loadedResources[cacheKey] = res;

            JSLDebug.LogFormat(LOG_LOADALL_ASSET_SUCCEED, assetName, startFrameCount, Time.frameCount);
        }
Exemple #3
0
 public void UnloadResource(string path, System.Action <Object> loadCompleteDelegate)
 {
     //	Debug.Log("UNLOAD: " + path );
     if (loadedResources.ContainsKey(path))
     {
         LoadedResource r = loadedResources[path];
         r.NumReferences--;
         if (r.NumReferences == 0)
         {
             //Debug.Log ("UNLOADING ASSET: " + r.Resource.name);
             Resources.UnloadAsset(r.Resource);
             loadedResources.Remove(path);
         }
     }
     else
     {
         //if (loadQueue.ContainsKey (path)) {
         //	Debug.Log ("LOAD QUEUE BEFORE REMOVAL: " + loadQueue [path].Count);
         loadQueue.Remove(path, loadCompleteDelegate);
         //	if (loadQueue.ContainsKey (path)) {
         //			Debug.Log ("LOAD QUEUE AFTER REMOVAL: " + loadQueue [path].Count);
         //	} else {
         //	Debug.Log ("LOAD QUEUE AFTER REMOVAL: 0");
         //			}
         //	}
         //Debug.LogWarning("No record of any loaded resources at path: " + path );
     }
 }
Exemple #4
0
        private IEnumerator PreloadAllAssetBundleAsync <T>(string assetBundleName, string assetName) where T : UnityEngine.Object
        {
            if (InCache <T>(assetBundleName, assetName))
            {
                yield break;
            }

            var startFrameCount = Time.frameCount;

            AssetBundleLoadAllAssetRequest <T> request = AssetBundleManager.Instance.LoadAllAssetAsync <T>(assetBundleName, assetName);

            if (null == request)
            {
                yield break;
            }

            yield return(StartCoroutine(request));

            var assets = request.GetAsset();

            var res = new LoadedResource(assets);

            if (null == res.m_resources || res.m_resources.Length == 0)
            {
                yield break;
            }

            m_loadedResources[GetCacheKey <T>(assetBundleName, assetName)] = res;

            TEDDebug.LogFormat("[ResourceSystem] - Loading AssetBundle '{0}' has done from frame {1} to frame {2}.", assetName, startFrameCount, Time.frameCount);
        }
Exemple #5
0
        private IEnumerator PreloadResourceAsync <T>(string assetName) where T : UnityEngine.Object
        {
            if (InCache <T>(string.Empty, assetName))
            {
                yield break;
            }

            var startFrameCount = Time.frameCount;

            var resourceRequest = Resources.LoadAsync <T>(assetName);

            while (!resourceRequest.isDone)
            {
                yield return(0);
            }

            var res = new LoadedResource(resourceRequest.asset);

            if (null == res.m_resource)
            {
                yield break;
            }

            m_loadedResources[GetCacheKey <T>(string.Empty, assetName)] = res;

            TEDDebug.LogFormat("[ResourceSystem] - Loading Asset '{0}' has done from frame {1} to frame {2}.", assetName, startFrameCount, Time.frameCount);
        }
        public void Save(string path)
        {
            ResourcePath = path;
            ResourceName = Path.GetFileName(path);

            LoadedResource.Save(path);

            Saved = true;
            UpdateRedactor();
        }
Exemple #7
0
        public IEnumerator Load <T>(string resourceName, System.Action <UnityEngine.Object> onComplete)
        {
            //Debug.LogFormat("Start to load {0} at frame {1}", resourceName, Time.frameCount);

            while (inProgressOperations.ContainsKey(resourceName))
            {
                yield return(null);
            }

            if (loadedResources.ContainsKey(resourceName))
            {
                var resource = loadedResources[resourceName];
                if (resource != null && resource.m_Resource != null)
                {
                    resource.m_ReferencedCount++;

                    //Debug.LogFormat("{0} is loaded successfully at frame {1} : [RefCount] {2}", resourceName, Time.frameCount, resource.m_ReferencedCount);

                    if (onComplete != null)
                    {
                        onComplete(resource.m_Resource);
                    }
                }
                else
                {
                    Debug.LogError("Resource already loaded but actual data is null. Name: " + resourceName);
                }
            }
            else
            {
                ResourceRequest request = Resources.LoadAsync(resourceName, typeof(T));
                inProgressOperations.Add(resourceName, request);

                yield return(request);

                inProgressOperations.Remove(resourceName);

                if (request.asset != null)
                {
                    var resource = new LoadedResource(request.asset);
                    loadedResources.Add(resourceName, resource);

                    //Debug.LogFormat("{0} is loaded successfully at frame {1} : [RefCount] {2}", resourceName, Time.frameCount, resource.m_ReferencedCount);

                    if (onComplete != null)
                    {
                        onComplete(request.asset);
                    }
                }
                else
                {
                    Debug.LogError("Asynchronously loaded resource is null. Name: " + resourceName);
                }
            }
        }
Exemple #8
0
        private IEnumerator StartLoadAsync <T>(string assetBundleName, string assetName, Action <T, object> callback, object customData, bool unloadAutomatically) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(assetName))
            {
                JSLDebug.LogErrorFormat(LOG_LOAD_ASSET_FAILED, assetName, assetBundleName);
                callback?.Invoke(null, customData);
                yield break;
            }

            string cacheKey = GetCacheKey <T>(assetBundleName, assetName);

            if (IsPrefab <T>())
            {
                yield return(PreloadAsync <GameObject>(cacheKey, assetBundleName, assetName));
            }
            else
            {
                yield return(PreloadAsync <T>(cacheKey, assetBundleName, assetName));
            }

            if (!InCache(cacheKey))
            {
                JSLDebug.LogErrorFormat(LOG_LOAD_ASSET_FAILED, assetName, assetBundleName);

                callback?.Invoke(null, customData);
                RemoveAsyncLoadingReferencedCounts(cacheKey);

                yield break;
            }

            LoadedResource res = m_loadedResources[cacheKey];

            res.referencedCount++;

            T asset = res.resource as T;

            if (asset == null)
            {
                asset = (res.resource as GameObject).GetComponent <T>();
            }

            callback?.Invoke(asset, customData);
            RemoveAsyncLoadingReferencedCounts(cacheKey);

            if (unloadAutomatically)
            {
                Unload <T>(assetBundleName, assetName);
            }
        }
    public IEnumerator Load <T>(IObserver <Object> obs, string resourceName)
    {
        while (inProgressOperations.ContainsKey(resourceName))
        {
            yield return(null);
        }

        if (loadedResources.ContainsKey(resourceName))
        {
            var resource = loadedResources[resourceName];
            if (resource != null && resource.m_Resource != null)
            {
                resource.m_ReferencedCount++;

                //Debug.LogFormat("{0} is loaded successfully at frame {1} : [RefCount] {2}", resourceName, Time.frameCount, resource.m_ReferencedCount);
                obs.OnNext(resource.m_Resource);
                obs.OnCompleted();
            }
            else
            {
                obs.OnError(new System.Exception("Resource already loaded but actual data is null. Name: " + resourceName));
            }
        }
        else
        {
            ResourceRequest request = Resources.LoadAsync(resourceName, typeof(T));

            inProgressOperations.Add(resourceName, request);

            yield return(request);

            inProgressOperations.Remove(resourceName);

            if (request.asset != null)
            {
                var resource = new LoadedResource(request.asset);
                loadedResources.Add(resourceName, resource);

                //Debug.LogFormat("{0} is loaded successfully at frame {1} : [RefCount] {2}", resourceName, Time.frameCount, resource.m_ReferencedCount);
                obs.OnNext(request.asset);
                obs.OnCompleted();
            }
            else
            {
                obs.OnError(new System.Exception("Asynchronously loaded resource is null. Name: " + resourceName));
            }
        }
    }
Exemple #10
0
        private IEnumerator PreloadAllResourceAsync <T>(string cacheKey, string assetName) where T : UnityEngine.Object
        {
            int startFrameCount = Time.frameCount;

            T[] assets = Resources.LoadAll <T>(assetName);

            LoadedResource res = new LoadedResource(assets);

            if (null == res.resources || res.resources.Length == 0)
            {
                yield break;
            }

            m_loadedResources[cacheKey] = res;

            JSLDebug.LogFormat(LOG_LOADALL_ASSET_SUCCEED, assetName, startFrameCount, Time.frameCount);
        }
Exemple #11
0
        public void Unload <T>(string assetBundleName, string assetName, bool forceUnload = false) where T : UnityEngine.Object
        {
            string cacheKey = GetCacheKey <T>(assetBundleName, assetName);

            if (InCache(cacheKey))
            {
                LoadedResource res = m_loadedResources[cacheKey];
                if (forceUnload)
                {
                    res.referencedCount = 0;
                }
                else
                {
                    res.referencedCount--;
                }

                Release();
            }
        }
Exemple #12
0
        private IEnumerator PreloadAllAsync <T>(string assetBundleName, string assetName) where T : UnityEngine.Object
        {
            AddAsyncLoadingReferencedCounts(GetCacheKey <T>(assetBundleName, assetName));

            //Preload the asset from AssetBundle async
            if (!string.IsNullOrEmpty(assetBundleName))
            {
                yield return(StartCoroutine(PreloadAllAssetBundleAsync <T>(assetBundleName, assetName)));
            }

            //Preload the asset from Resources async
            if (!InCache <T>(assetBundleName, assetName))
            {
                T[] assets = Resources.LoadAll <T>(assetName);
                var res    = new LoadedResource(assets);
                m_loadedResources[GetCacheKey <T>(assetBundleName, assetName)] = res;
            }

            RemoveAsyncLoadingReferencedCounts(GetCacheKey <T>(assetBundleName, assetName));
        }
Exemple #13
0
        private IEnumerator StartLoadAllAsync <T>(string assetBundleName, string assetName, Action <List <T>, object> callback, object customData, bool unloadAutomatically) where T : UnityEngine.Object
        {
            string cacheKey = GetCacheKey <T>(assetBundleName, assetName);

            yield return(PreloadAllAsync <T>(cacheKey, assetBundleName, assetName));

            if (!InCache(cacheKey))
            {
                JSLDebug.LogWarningFormat(LOG_LOADALL_ASSET_FAILED, assetName, assetBundleName);

                callback?.Invoke(null, customData);
                RemoveAsyncLoadingReferencedCounts(cacheKey);

                yield break;
            }

            LoadedResource res = m_loadedResources[cacheKey];

            res.referencedCount++;

            List <T> assets = new List <T>();
            T        cache  = null;

            for (int i = 0; i < res.resources.Length; i++)
            {
                cache = (T)res.resources[i];
                if (cache)
                {
                    assets.Add(cache);
                }
            }

            callback?.Invoke(assets, customData);
            RemoveAsyncLoadingReferencedCounts(cacheKey);

            if (unloadAutomatically)
            {
                Unload <T>(assetBundleName, assetName);
            }
        }
Exemple #14
0
        private IEnumerator PreloadResourceAsync <T>(string cacheKey, string assetName) where T : UnityEngine.Object
        {
            int startFrameCount = Time.frameCount;

            ResourceRequest resourceRequest = Resources.LoadAsync <T>(assetName);

            while (!resourceRequest.isDone)
            {
                yield return(0);
            }

            LoadedResource res = new LoadedResource(resourceRequest.asset);

            if (null == res.resource)
            {
                yield break;
            }

            m_loadedResources[cacheKey] = res;

            JSLDebug.LogFormat(LOG_LOAD_ASSET_SUCCEED, assetName, startFrameCount, Time.frameCount);
        }
 public void SetLoadedResource(LoadedResource loaded, int index)
 {
     indexText.text          = index + "";
     referenceCountText.text = loaded.referencedCount + "";
     pathText.text           = "[" + loaded.objType + "]" + loaded.path;
 }
Exemple #16
0
 public void AddHoldersFrom(LoadedResource resource) => holders.UnionWith(resource.holders);