示例#1
0
        public void UnloadAsset(string rAssetbundleName)
        {
            ABLoadEntry rAssetLoadEntry = null;

            if (!ABLoaderVersion.Instance.TryGetValue(rAssetbundleName, out rAssetLoadEntry))
            {
                Debug.LogErrorFormat("Can not find assetbundle: -- {0}", rAssetbundleName);
                return;
            }

            // 递归遍历该资源的依赖项
            for (int i = 0; i < rAssetLoadEntry.ABDependNames.Length; i++)
            {
                UnloadAsset(rAssetLoadEntry.ABDependNames[i]);
            }

            // 引用计数减1
            rAssetLoadEntry.RefCount--;

            //确定该Info的引用计数是否为0,如果为0则删除它
            if (rAssetLoadEntry.RefCount == 0)
            {
                if (rAssetLoadEntry.CacheAsset != null)
                {
                    Debug.LogFormat("-- Real unload assetbundle: {0}", rAssetbundleName);
                    rAssetLoadEntry.CacheAsset.Unload(true);
                    rAssetLoadEntry.CacheAsset = null;
                }
                rAssetLoadEntry.IsLoadCompleted = false;
                rAssetLoadEntry.IsLoading       = false;
            }
        }
示例#2
0
        private async Task LoadAsset_Async(LoaderRequest rRequest)
        {
            ABLoadEntry rAssetLoadEntry = null;

            if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
            {
                Debug.LogErrorFormat("Can not find assetbundle: -- {0}", rRequest.Path);
                rRequest.Asset = null;
                return;
            }

            //引用计数加1
            rAssetLoadEntry.RefCount++;

            // 确认未加载完成并且正在被加载、一直等待其加载完成
            while (rAssetLoadEntry.IsLoading && !rAssetLoadEntry.IsLoadCompleted)
            {
                await new WaitForEndOfFrame();
            }

            // 如果该资源加载完成了
            if (!rAssetLoadEntry.IsLoading && rAssetLoadEntry.IsLoadCompleted)
            {
                // 从缓存的Assetbundle里面加载资源
                await LoadAssetObject(rRequest, rAssetLoadEntry, false);

                return;
            }

            // 开始加载资源依赖项
            if (rAssetLoadEntry.ABDependNames != null && !rRequest.IsSimulate)
            {
                for (int i = rAssetLoadEntry.ABDependNames.Length - 1; i >= 0; i--)
                {
                    string rDependABPath = rAssetLoadEntry.ABDependNames[i];
                    string rDependABName = rDependABPath;

                    LoaderRequest rDependAssetRequest = new LoaderRequest(rDependABName, "", false, rRequest.IsSimulate, false);
                    await LoadAsset_Async(rDependAssetRequest);
                }
            }

            //开始加载当前的资源包
            rAssetLoadEntry.IsLoading       = true;
            rAssetLoadEntry.IsLoadCompleted = false;

            // 真正的从AB包里面加载资源
            await LoadAssetObject(rRequest, rAssetLoadEntry, true);

            rAssetLoadEntry.IsLoading       = false;
            rAssetLoadEntry.IsLoadCompleted = true;
        }
        public void AddEntry(LoaderSpace rLoaderSpace, ABVersionEntry rAVEntry)
        {
            ABLoadEntry rABLoadInfo = new ABLoadEntry();

            rABLoadInfo.ABPath = GetABPath_With_Space(rLoaderSpace, rAVEntry.Name);
            rABLoadInfo.ABName = rAVEntry.Name;
            List <string> rDependABs = new List <string>(rAVEntry.Dependencies);

            rABLoadInfo.ABDependNames   = rDependABs.ToArray();
            rABLoadInfo.IsLoading       = false;
            rABLoadInfo.IsLoadCompleted = false;
            rABLoadInfo.CacheAsset      = null;
            rABLoadInfo.RefCount        = 0;

            this.Entries.Add(rAVEntry.Name, rABLoadInfo);
        }
示例#4
0
        private IEnumerator LoadAsset_Async(LoaderRequest rRequest)
        {
            ABLoadEntry rAssetLoadEntry = null;

            if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
            {
                Debug.LogErrorFormat("Can not find assetbundle: -- {0}", rRequest.Path);
                rRequest.Asset = null;
                yield break;
            }

            // 确认该资源是否已经加载完成或者正在被加载
            while (rAssetLoadEntry.IsLoading && !rAssetLoadEntry.IsLoadCompleted)
            {
                yield return(0);
            }

            //引用计数加1
            rAssetLoadEntry.RefCount++;

            // 开始加载资源依赖项
            if (rAssetLoadEntry.ABDependNames != null && !rRequest.IsSimulate)
            {
                for (int i = rAssetLoadEntry.ABDependNames.Length - 1; i >= 0; i--)
                {
                    string rDependABPath = rAssetLoadEntry.ABDependNames[i];
                    string rDependABName = rDependABPath;

                    LoaderRequest rDependAssetRequest = new LoaderRequest(rDependABName, "", false, rRequest.IsSimulate);
                    yield return(rDependAssetRequest.Start(LoadAsset_Async(rDependAssetRequest)));
                }
            }

            // 如果该资源加载完成了
            if (!rAssetLoadEntry.IsLoading && rAssetLoadEntry.IsLoadCompleted)
            {
                if (!string.IsNullOrEmpty(rRequest.AssetName))
                {
                    AssetBundleRequest rABRequest = rAssetLoadEntry.CacheAsset.LoadAssetAsync(rRequest.AssetName);
                    yield return(rABRequest);

                    rRequest.Asset = rABRequest.asset;
                }
                yield break;
            }

            //开始加载当前的资源包
            rAssetLoadEntry.IsLoading       = true;
            rAssetLoadEntry.IsLoadCompleted = false;

            string rAssetLoadUrl = rAssetLoadEntry.ABPath;

            if (rRequest.IsSimulate)
            {
                Debug.Log("---Simulate Load ab: " + rAssetLoadUrl);
#if UNITY_EDITOR
                if (!string.IsNullOrEmpty(rRequest.AssetName) && !rRequest.IsScene)
                {
                    string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(rAssetLoadEntry.ABName, rRequest.AssetName);
                    if (rAssetPaths.Length == 0)
                    {
                        Debug.LogError("There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName);
                        yield break;
                    }
                    Object rTargetAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(rAssetPaths[0]);
                    rRequest.Asset = rTargetAsset;
                }
#endif
            }
            else
            {
                Debug.Log("---Real Load ab: " + rAssetLoadUrl);

                var rAssetbundleCreateRequest = AssetBundle.LoadFromFileAsync(rAssetLoadUrl);
                yield return(rAssetbundleCreateRequest);

                // 如果是一个直接的资源,将资源的对象取出来
                rAssetLoadEntry.CacheAsset = rAssetbundleCreateRequest.assetBundle;

                // 加载Object
                if (!string.IsNullOrEmpty(rRequest.AssetName))
                {
                    if (!rRequest.IsScene)
                    {
                        AssetBundleRequest rABRequest = rAssetLoadEntry.CacheAsset.LoadAssetAsync(rRequest.AssetName);
                        yield return(rABRequest);

                        rRequest.Asset = rABRequest.asset;
                    }
                    else
                    {
                        rAssetLoadEntry.CacheAsset.GetAllScenePaths();
                    }
                }
            }
            rAssetLoadEntry.IsLoading       = false;
            rAssetLoadEntry.IsLoadCompleted = true;
        }
示例#5
0
        private async Task LoadAssetObject(LoaderRequest rRequest, ABLoadEntry rAssetLoadEntry, bool bRealLoad)
        {
            string rAssetLoadUrl = rAssetLoadEntry.ABPath;

            if (rRequest.IsSimulate)
            {
                Debug.Log("---Simulate Load ab: " + rAssetLoadUrl);
#if UNITY_EDITOR
                if (!string.IsNullOrEmpty(rRequest.AssetName) && !rRequest.IsScene)
                {
                    if (!rRequest.IsLoadAllAssets)
                    {
                        string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(rAssetLoadEntry.ABName, rRequest.AssetName);
                        if (rAssetPaths.Length == 0)
                        {
                            Debug.LogError("There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName);
                            return;
                        }
                        Object rTargetAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(rAssetPaths[0]);
                        rRequest.Asset = rTargetAsset;
                    }
                    else
                    {
                        string[] rAssetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(rAssetLoadEntry.ABName);
                        if (rAssetPaths.Length == 0)
                        {
                            Debug.LogError("There is no asset with name \"" + rRequest.AssetName + "\" in " + rAssetLoadEntry.ABName);
                            return;
                        }
                        rRequest.AllAssets = new Object[rAssetPaths.Length];
                        for (int i = 0; i < rAssetPaths.Length; i++)
                        {
                            Object rAssetObj = UnityEditor.AssetDatabase.LoadAssetAtPath(rAssetPaths[i], typeof(Object));
                            if (rAssetObj != null)
                            {
                                rRequest.AllAssets[i] = rAssetObj;
                            }
                        }
                    }
                }
#endif
            }
            else
            {
                if (bRealLoad)
                {
                    Debug.Log("---Real Load ab: " + rAssetLoadUrl);
                    // 如果是一个直接的资源,将资源的对象取出来
                    rAssetLoadEntry.CacheAsset = await AssetBundle.LoadFromFileAsync(rAssetLoadUrl);
                }
                else
                {
                    Debug.Log("---Load asset: " + rAssetLoadUrl);
                }

                // 加载Object
                if (!string.IsNullOrEmpty(rRequest.AssetName))
                {
                    if (!rRequest.IsScene)
                    {
                        if (!rRequest.IsLoadAllAssets)
                        {
                            rRequest.Asset = await rAssetLoadEntry.CacheAsset.LoadAssetAsync(rRequest.AssetName);
                        }
                        else
                        {
                            await LoadAllAssets_ByAssetbundle(rRequest, rAssetLoadEntry.CacheAsset);
                        }
                    }
                    else
                    {
                        rAssetLoadEntry.CacheAsset.GetAllScenePaths();
                    }
                }
            }
        }
 public bool TryGetValue(string rABName, out ABLoadEntry rABLoadEntry)
 {
     return(this.Entries.TryGetValue(rABName, out rABLoadEntry));
 }