예제 #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
        public void UnloadAsset(string rABPath)
        {
            if (ABPlatform.Instance.IsDevelopeMode())
            {
                return;
            }

            ABLoadEntry rAssetLoadEntry = null;

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

            // 得到该资源的所有依赖项
            var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry);

            for (int i = 0; i < rABAllDependenceEntries.Count; i++)
            {
                rABAllDependenceEntries[i].RefCount--;
                if (rABAllDependenceEntries[i].RefCount < 0)
                {
                    rABAllDependenceEntries[i].RefCount = 0;
                }
            }
        }
예제 #3
0
        private List <ABLoadEntry> GetABEntryAllDependencies(ABLoadEntry rABLoadEntry)
        {
            var rABAllDependenceEntries = new List <ABLoadEntry>();

            this.GetABEntryAllDependencies(rABLoadEntry, ref rABAllDependenceEntries);
            return(rABAllDependenceEntries);
        }
예제 #4
0
        private IEnumerator LoadAsset_Async(AssetLoaderRequest rRequest)
        {
            ABLoadEntry rAssetLoadEntry = null;

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

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

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

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

                rRequest.SetResult(rRequest);
                yield break;
            }

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

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

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

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

            rRequest.SetResult(rRequest);

            rAssetLoadEntry.IsLoading       = false;
            rAssetLoadEntry.IsLoadCompleted = true;
        }
예제 #5
0
        private void LoadAssetSync_OneEntry(AssetLoaderRequest rRequest, ABLoadEntry rABLoadEntry)
        {
            // 从缓存的Assetbundle里面加载资源
            rABLoadEntry.IsLoading       = true;
            rABLoadEntry.IsLoadCompleted = false;
            this.LoadAssetObjectSync(rRequest, rABLoadEntry);
            rABLoadEntry.IsLoading       = false;
            rABLoadEntry.IsLoadCompleted = true;

            // 如果判断此时的RefCount为0的话,那么就unload掉该项资源
            this.AutoCheckUnloadAsset(rABLoadEntry);
        }
예제 #6
0
 private void GetABEntryAllDependencies(ABLoadEntry rABLoadEntry, ref List <ABLoadEntry> rABAllDependenceEntries)
 {
     for (int i = 0; i < rABLoadEntry.ABDependNames.Length; i++)
     {
         ABLoadEntry rDependenceEntry = null;
         if (ABLoaderVersion.Instance.TryGetValue(rABLoadEntry.ABDependNames[i], out rDependenceEntry))
         {
             this.GetABEntryAllDependencies(rDependenceEntry, ref rABAllDependenceEntries);
         }
     }
     rABAllDependenceEntries.Add(rABLoadEntry);
 }
예제 #7
0
        private IEnumerator LoadAssetAsync(AssetLoaderRequest rRequest)
        {
            this.mIsLoadingRefCount++;

            ABLoadEntry rAssetLoadEntry = null;

            if (!rRequest.IsSimulate)
            {
                if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
                {
                    Debug.LogErrorFormat("---Can not find assetbundle: -- {0}", rRequest.Path);
                    rRequest.Asset = null;
                    this.mIsLoadingRefCount--;
                    rRequest.SetResult(rRequest);
                    yield break;
                }
            }
            else
            {
                rAssetLoadEntry = new ABLoadEntry()
                {
                    ABName        = rRequest.Path,
                    ABPath        = ABLoaderVersion.Instance.GetABPath_With_Space(LoaderSpace.Streaming, rRequest.Path),
                    ABDependNames = new string[0],
                };
            }

            // 得到该资源的所有依赖项
            var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry);

            this.mTempEntries.Clear();
            for (int i = 0; i < rABAllDependenceEntries.Count; i++)
            {
                rABAllDependenceEntries[i].RefCount++;
                if (!this.mTempEntries.Contains(rABAllDependenceEntries[i]))
                {
                    this.mTempEntries.Add(rABAllDependenceEntries[i]);
                }
            }
            for (int i = 0; i < this.mTempEntries.Count; i++)
            {
                // 构建依赖项的Request
                var rItem = this.mTempEntries[i];
                var rDependenceLoaderRequest = new AssetLoaderRequest(
                    rItem.ABPath, string.Empty, true, rRequest.IsSimulate, rRequest.IsLoadAllAssets);
                yield return(this.LoadAssetAsync_OneEntry(rDependenceLoaderRequest, rItem));
            }
            yield return(this.LoadAssetAsync_OneEntry(rRequest, rABAllDependenceEntries[rABAllDependenceEntries.Count - 1]));

            this.mIsLoadingRefCount--;
            rRequest.SetResult(rRequest);
        }
예제 #8
0
 /// <summary>
 /// 自动检测引用计数
 /// </summary>
 private void AutoCheckUnloadAsset(ABLoadEntry rAssetLoadEntry)
 {
     if (rAssetLoadEntry.RefCount == 0)
     {
         if (rAssetLoadEntry.CacheAsset != null)
         {
             Debug.LogFormat("---Auto Real unload assetbundle: {0}", rAssetLoadEntry.ABName);
             rAssetLoadEntry.CacheAsset.Unload(true);
             rAssetLoadEntry.CacheAsset      = null;
             rAssetLoadEntry.IsLoadCompleted = false;
             rAssetLoadEntry.IsLoading       = false;
         }
     }
 }
예제 #9
0
        /// <summary>
        /// 计算引用计数
        /// </summary>
        private void CalcRefCount(string rABName, int nDeltaRefCount)
        {
            ABLoadEntry rABLoadEntry = null;

            if (!ABLoaderVersion.Instance.TryGetValue(rABName, out rABLoadEntry))
            {
                return;
            }
            rABLoadEntry.RefCount += nDeltaRefCount;
            for (int i = 0; i < rABLoadEntry.ABDependNames.Length; i++)
            {
                this.CalcRefCount(rABLoadEntry.ABDependNames[i], nDeltaRefCount);
            }
        }
예제 #10
0
        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);
        }
예제 #11
0
        private void LoadAssetSync(AssetLoaderRequest rRequest)
        {
            this.mIsLoadingRefCount++;

            ABLoadEntry rAssetLoadEntry = null;

            if (!rRequest.IsSimulate)
            {
                if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
                {
                    Debug.LogErrorFormat("---Can not find assetbundle: -- {0}", rRequest.Path);
                    rRequest.Asset = null;
                    this.mIsLoadingRefCount--;
                    return;
                }
            }
            else
            {
                rAssetLoadEntry = new ABLoadEntry()
                {
                    ABName        = rRequest.Path,
                    ABPath        = ABLoaderVersion.Instance.GetABPath_With_Space(LoaderSpace.Streaming, rRequest.Path),
                    ABDependNames = new string[0],
                };
            }

            // 得到该资源的所有依赖项
            var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry);

            for (int i = 0; i < rABAllDependenceEntries.Count; i++)
            {
                rABAllDependenceEntries[i].RefCount++;
            }

            for (int i = 0; i < rABAllDependenceEntries.Count - 1; i++)
            {
                // 构建依赖项的Request
                var rDependenceLoaderRequest = new AssetLoaderRequest(
                    rABAllDependenceEntries[i].ABPath, string.Empty, true, rRequest.IsSimulate, rRequest.IsLoadAllAssets);
                this.LoadAssetSync_OneEntry(rDependenceLoaderRequest, rABAllDependenceEntries[i]);
            }
            this.LoadAssetSync_OneEntry(rRequest, rABAllDependenceEntries[rABAllDependenceEntries.Count - 1]);

            this.mIsLoadingRefCount--;
        }
예제 #12
0
        public void UnloadAsset(string rABPath)
        {
            if (ABPlatform.Instance.IsDevelopeMode())
            {
                return;
            }

            ABLoadEntry rAssetLoadEntry = null;

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

            // 得到该资源的所有依赖项
            this.CalcRefCount(rAssetLoadEntry.ABName, -1);
        }
예제 #13
0
        private IEnumerator LoadAssetAsync_OneEntry(AssetLoaderRequest rRequest, ABLoadEntry rABLoadEntry)
        {
            // 确认未加载完成并且正在被加载、一直等待其加载完成
            while (rABLoadEntry.IsLoading && !rABLoadEntry.IsLoadCompleted)
            {
                // 如果两个都为false,则断开协程停下来
                if (rABLoadEntry.RefCount == 0)
                {
                    yield break;
                }
                yield return(0);
            }

            // 从缓存的Assetbundle里面加载资源
            rABLoadEntry.IsLoading       = true;
            rABLoadEntry.IsLoadCompleted = false;
            yield return(LoadAssetObjectAsync(rRequest, rABLoadEntry));

            rABLoadEntry.IsLoading       = false;
            rABLoadEntry.IsLoadCompleted = true;

            // 如果判断此时的RefCount为0的话,那么就unload掉该项资源
            this.AutoCheckUnloadAsset(rABLoadEntry);
        }
예제 #14
0
        private IEnumerator LoadAssetObject(AssetLoaderRequest 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);
                            yield break;
                        }
                        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);
                            yield break;
                        }
                        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);

                    // 如果是一个直接的资源,将资源的对象取出来
                    var rABCreateRequest = AssetBundle.LoadFromFileAsync(rAssetLoadUrl);
                    yield return(rABCreateRequest);

                    rAssetLoadEntry.CacheAsset = rABCreateRequest.assetBundle;
                }
                else
                {
                    Debug.Log("---Load asset: " + rAssetLoadUrl);
                }

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

                            rRequest.Asset = rABRequest.asset;
                        }
                        else
                        {
                            yield return(LoadAllAssets_ByAssetbundle(rRequest, rAssetLoadEntry.CacheAsset));
                        }
                    }
                    else
                    {
                        rAssetLoadEntry.CacheAsset.GetAllScenePaths();
                    }
                }
            }
        }
예제 #15
0
        private void LoadAssetObjectSync(AssetLoaderRequest rRequest, ABLoadEntry rAssetLoadEntry)
        {
            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;
                            }
                        }
                    }
                }
                else
                {
                    UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(
                        rRequest.AssetName, new LoadSceneParameters()
                    {
                        loadSceneMode = rRequest.SceneMode
                    });

                    string rSceneName = Path.GetFileNameWithoutExtension(rRequest.AssetName);
                    rRequest.Scene = SceneManager.GetSceneByName(rSceneName);
                    SceneManager.SetActiveScene(rRequest.Scene);
                }
#endif
            }
            else
            {
                if (rAssetLoadEntry.CacheAsset == null)
                {
                    Debug.Log("---Real Load ab: " + rAssetLoadUrl);
                    // 如果是一个直接的资源,将资源的对象取出来
                    rAssetLoadEntry.CacheAsset = AssetBundle.LoadFromFile(rAssetLoadUrl);
                }
                else
                {
                    Debug.Log("---Load asset: " + rAssetLoadUrl);
                }

                // 加载Object
                if (!string.IsNullOrEmpty(rRequest.AssetName))
                {
                    if (!rRequest.IsScene)
                    {
                        if (!rRequest.IsLoadAllAssets)
                        {
                            rRequest.Asset = rAssetLoadEntry.CacheAsset.LoadAsset(rRequest.AssetName);
                        }
                        else
                        {
                            LoadAllAssets_ByAssetbundle_Sync(rRequest, rAssetLoadEntry.CacheAsset);
                        }
                    }
                    else
                    {
                        rAssetLoadEntry.CacheAsset.GetAllScenePaths();

                        string rSceneName = Path.GetFileNameWithoutExtension(rRequest.AssetName);
                        SceneManager.LoadScene(rSceneName, rRequest.SceneMode);

                        rRequest.Scene = SceneManager.GetSceneByName(rSceneName);
                        SceneManager.SetActiveScene(rRequest.Scene);
                    }
                }
            }
        }
예제 #16
0
 public bool TryGetValue(string rABName, out ABLoadEntry rABLoadEntry)
 {
     return(this.Entries.TryGetValue(rABName, out rABLoadEntry));
 }