// 自动检查卸载点资源包
    // UI 资源暂时不卸载
    // 已经加载但是还有缓存资源没有加载点不进行卸载
    // 正在加载点资源也不进行卸载
    private static void CheckUnloadPackage()
    {
        List <string> unloadPackageList = new List <string>();

        using (var i = _packageCacheDic.GetEnumerator())
        {
            RGPackage package = null;
            while (i.MoveNext())
            {
                package = i.Current.Value;
                if (package != null)
                {
                    if (package.IsLoadPackage)
                    {
                        if (!package.IsCacheNeedLoad)
                        {
                            if (!package.IsUI && package.IsAutoRelease && !package.IsForverBundle)
                            {
                                RGLog.DebugResLoad("<color=red> Auto Unload bundle</color> -> {0}", package.PackageName);
                                package.UnloadAll();;
                                unloadPackageList.Add(package.PackageName);
                            }
                        }
                    }
                }
            }
        }
        for (int i = 0; i < unloadPackageList.Count; i++)
        {
            _packageCacheDic.Remove(unloadPackageList[i]);
        }
    }
    public static string GetPackageName(string path)
    {
        string[] model = path.ToLower().Split(RGResource.PATH_SEPARATOR);

        // 包路径
        string packageUrl = "";

        if (model.Length > 0)
        {
            if (model[0].Equals("effect"))
            {
                //effect
                packageUrl = "effect";
            }
            else if (model[0].Equals("prefabs"))
            {
                // prefabs
                packageUrl = "prefabs";
            }

            RGLog.DebugResLoad("<color=yellow>Package Name = " + packageUrl + "</color>");
            return(packageUrl.ToLower());
        }

        RGLog.DebugError("GetPackagePath Error ! Path is Empty");

        return(string.Empty);
    }
示例#3
0
    IEnumerator LoadCache()
    {
        string assetName = "";
        Action <UnityEngine.Object, LoadEventData> loadComplete = null;
        LoadEventData evData = null;

        for (int i = 0; i < _cacheAssetNameList.Count; i++)
        {
            assetName    = _cacheAssetNameList[i];
            loadComplete = _cacheCompleteList[i];
            evData       = _cacheEvDataList[i];

            RGLog.DebugResLoad("Load Cache -> {0}", assetName);

            if (IsUI || IsForverBundle)
            {
                if (loadComplete != null)
                {
                    loadComplete(_bundle, evData);
                }
            }
            else
            {
                LoadAssetAsync(assetName, loadComplete, evData);
            }
            yield return(0);
        }

        _cacheAssetNameList.Clear();
        _cacheCompleteList.Clear();
        _cacheEvDataList.Clear();
    }
示例#4
0
    public void UnloadAll()
    {
        if (_bundle != null)
        {
            RGLog.DebugResLoad("<color=red> unload all bundle </color> -> {0}", _packageName);
            _bundle.Unload(false);
            _bundle = null;
        }

        RGRes res = null;

        for (int i = 0; i < _cacheRes.Count; i++)
        {
            res = _cacheRes[i];
            if (res != null)
            {
                RGLog.DebugResLoad("{0}:{1} \tRef:{2}", _packageName, res.ResName, res.RefCount);

                res.UnLoad();
            }
        }

        _cacheRes.Clear();
        // 清除list所占的内存
        _cacheRes.TrimExcess();

        Unloaded = true;
    }
示例#5
0
    public bool UnloadRes(string assetName)
    {
        var res = FindRes(assetName);

        if (res == null)
        {
            RGLog.DebugResLoad("不用重复释放资源:{0}", assetName);
            return(false);
        }

        var refCount = res.DecRef();

        {
            RGLog.DebugResLoad("<color=red>Unlod</color> {0}:{1} \tRef:{2}", _packageName, res.ResName, res.RefCount);

            if (refCount <= 0)
            {
                res.UnLoad();
                RemoveRes(res);
                return(true);
            }
        }

        return(false);
    }
示例#6
0
    private IEnumerator IELoadBundleAsync(string assetName, Action <UnityEngine.Object, LoadEventData> loadComplete, LoadEventData evData)
    {
        bool isLoadCache = false;

        if (!IsLoadPackage)
        {
            var bRequest = AssetBundle.LoadFromFileAsync(PackagePath);

            IsLoading = true;

            yield return(bRequest);

            var abRequest = bRequest.assetBundle;

            if (abRequest == null)
            {
                yield break;
            }
            if (!bRequest.isDone)
            {
                yield break;
            }

            _bundle = abRequest;

            IsLoading = false;

            // 加载缓存资源
            isLoadCache = _cacheAssetNameList.Count > 0;
        }

        if (IsUI || IsForverBundle)
        {
            RGLog.DebugResLoad("<color=red>LoadUI</color> {0}", _packageName);
            if (loadComplete != null)
            {
                loadComplete(_bundle, evData);
            }
        }
        else
        {
            CoroutineManager.Instance.StartCoroutine(IELoadAssetAsync(assetName, loadComplete, evData));
            yield return(0);
        }

        // 加载缓存
        CoroutineManager.Instance.StartCoroutine(LoadCache());

        yield return(null);
    }
示例#7
0
    private static void LoadAssetAsync(string packageName, string assetName, Action <UnityEngine.Object, LoadEventData> loadComplete, LoadEventData evData = null)
    {
        var package = PackageManager.CreatePackage(packageName);

        if (evData != null)
        {
            if (evData.data.Length > 0)
            {
                if (evData.data[0].ToString().Equals(RGResource.PRESTRAIN_FLAG))
                {
                    package.IsAutoRelease = true;
                }
                else if (evData.data[0].ToString().Equals(RGResource.LOAD_BUNDLE_FOREVER_FLAG))
                {
                    package.IsAutoRelease = false;
                }
            }
        }

        // 场景
        if (package.IsScene)
        {
            package.LoadSceneAsync(loadComplete, evData);
            return;
        }

        // 获取资源,如果已经缓存,就直接返回
        var asset = package.GetAsset(assetName);

        if (asset != null)
        {
            if (loadComplete != null)
            {
                loadComplete(asset, evData);
                RGLog.DebugResLoad("<color=yellow>[ Read Cache Res]</color>" + assetName);
                return;
            }
        }
        RGLog.Log(" 111111111111 " + loadComplete);
        //bundle没有加载到内存,需要先加载bundle
        if (!package.IsLoadPackage)
        {
            package.LoadBundleAsync(assetName, loadComplete, evData);
            RGLog.DebugResLoad("<color=yellow>[ load bundle ]</color>" + assetName);
        }
        else
        {
            //bundle已经加载了,资源还没有加载
            if (package.IsUI || package.IsForverBundle)
            {
                if (loadComplete != null)
                {
                    loadComplete(package.GetBundle(), evData);
                    RGLog.DebugResLoad("<color=yellow>[ Read Cache Res ]</color>" + assetName);
                    return;
                }
            }
            package.LoadAssetAsync(assetName, loadComplete, evData);
            RGLog.DebugResLoad("<color=yellow>[ Load Asset ] </color>" + assetName);
        }
    }