コード例 #1
0
ファイル: AssetLoader.cs プロジェクト: gh201200/Game
    private void Update()
    {
        //print(Progress + "    " + curIndex + "/" + totalIndex);
        //print(curIndex + "/" + totalIndex);

        //while (callbackToRemoveQue.Count > 0) callbackDict.Remove(callbackToRemoveQue.Dequeue());

        //if (!ready) return;
        //if (loadingDict.Count > 0)
        //{
        //    StartCoroutine(CoLoad());
        //}

        //if (Input.GetKeyDown(KeyCode.Q))
        //{
        //    Debug.Log("---------------abTemp-----------------");
        //    if (abTemp.Count != 0) foreach (var k in abTemp.Keys) Debug.Log(k);
        //    else Debug.Log("abTemp's count is 0");
        //    Debug.Log("----------------abTemp----------------");
        //    Debug.Log("----------------Cached----------------");
        //    if (cacheDict.Count != 0) foreach (var k in cacheDict.Keys) Debug.Log(k);
        //    else Debug.Log("abTemp's count is 0");
        //    Debug.Log("----------------Cached----------------");
        //}

        //if (Input.GetKey(KeyCode.W)) print(Progress * 100 + "%" + "\t" + curIndex + "/" + totalIndex);

        if (loadingDict.Count <= 0)
        {
            return;
        }

#if PerFile
        foreach (var p in loadingDict)
        {
            if (mainAssetLoadingDict.ContainsKey(p.Key))
            {
                continue;
            }
            foreach (var tag in p.Value.Dependencies)
            {
                if (p.Value.LoadedDependencies.Contains(tag))
                {
                    continue;
                }
                if (abTemp.ContainsKey(tag))
                {
                    if (abTemp[tag].assetBundleReq.isDone)
                    {
                        p.Value.FinishNum++;
                        p.Value.LoadedDependencies.Add(tag);
                        curIndex++;
                    }
                }
                else
                {
                    Debug.LogError("dependence assets must preload!" + " assets: \n" + p.Value.RelativePath + "\ndependecies: \n" + tag);
                }
            }
            if (abTemp.ContainsKey(p.Key) && abTemp[p.Key].assetBundleReq.isDone)
            {
                if (!mainAssetLoadingDict.ContainsKey(p.Key))
                {
                    p.Value.FinishNum++;
                    var temp = abTemp[p.Key].assetBundleReq.assetBundle.LoadAssetAsync(p.Value.AssetName, GameUtil.GetAssetType(p.Value.AssetType));
                    mainAssetLoadingDict.Add(p.Key, temp);
                    abTemp[p.Key].AddRefrenceCount();
                    curIndex++;
                }
            }
            else
            {
                if (p.Value.FinishNum >= p.Value.Dependencies.Length && !abTemp.ContainsKey(p.Key))
                {
                    var req = AssetBundle.LoadFromFileAsync(p.Value.FullPath);
                    abTemp.Add(p.Key, new BundleDes(req, p.Value.FullPath));
                }
            }
            p.Value.Progress = (float)p.Value.FinishNum / (p.Value.Dependencies.Length + 1);
        }

        foreach (var p in mainAssetLoadingDict)
        {
            if (p.Value.isDone)
            {
                if (!cacheDict.ContainsKey(p.Key))
                {
                    cacheDict.Add(p.Key, p.Value.asset);
                }
                if (callbackDict.ContainsKey(p.Key))
                {
                    foreach (var cb in callbackDict[p.Key])
                    {
                        cb(p.Value.asset);
                    }
                    callbackDict[p.Key] = new List <Action <object> >();
                }
                else
                {
                    Debug.LogError(p.Key + " -> callback is none!");
                }
                abTemp[p.Key].ReduceRefrenceCount();
                if (abTemp[p.Key].refrenceCount <= 0)
                {
                    abUnloadQue.Enqueue(p.Key);
                }
                foreach (var key in loadingDict[p.Key].Dependencies)
                {
                    abTemp[key].ReduceRefrenceCount();
                    if (abTemp[key].refrenceCount <= 0)
                    {
                        abUnloadQue.Enqueue(key);
                    }
                }
            }
        }

        while (abUnloadQue.Count > 0)
        {
            var str = abUnloadQue.Dequeue();
            if (abTemp.ContainsKey(str))
            {
                abTemp[str].assetBundleReq.assetBundle.Unload(false);
                abTemp.Remove(str);
                //Debug.Log("remove from abTemp -> " + str);
            }
            if (loadingDict.ContainsKey(str))
            {
                loadingDict.Remove(str);
                //Debug.Log("remove from loadingDict -> " + str);
            }
            if (mainAssetLoadingDict.ContainsKey(str))
            {
                mainAssetLoadingDict.Remove(str);
                //Debug.Log("remove from mainAssetLoadingDict -> " + str);
            }
        }
#else
        foreach (var p in loadingDict)
        {
            if (mainAssetLoadingDict.ContainsKey(p.Key))
            {
                continue;
            }
            foreach (var tag in p.Value.Dependencies)
            {
                if (p.Value.LoadedDependencies.Contains(tag))
                {
                    continue;
                }
                if (abTemp.ContainsKey(tag))
                {
                    if (abTemp[tag].assetBundleReq.isDone)
                    {
                        p.Value.FinishNum++;
                        p.Value.LoadedDependencies.Add(tag);
                        curIndex++;
                    }
                }
                else
                {
                    Debug.LogError("dependence assets must preload!" + " assets: \n" + p.Value.RelativePath + "\ndependecies: \n" + tag);
                }
            }
            if (abTemp.ContainsKey(p.Value.AssetBundleTag) && abTemp[p.Value.AssetBundleTag].assetBundleReq.isDone)
            {
                if (!mainAssetLoadingDict.ContainsKey(p.Key))
                {
                    p.Value.FinishNum++;
                    var temp = abTemp[p.Value.AssetBundleTag].assetBundleReq.assetBundle.LoadAssetAsync(p.Value.AssetName, GameUtil.GetAssetType(p.Value.AssetType));
                    mainAssetLoadingDict.Add(p.Key, temp);
                    abTemp[p.Value.AssetBundleTag].AddRefrenceCount();
                    curIndex++;
                }
            }
            else
            {
                if (p.Value.FinishNum >= p.Value.Dependencies.Length && !abTemp.ContainsKey(p.Value.AssetBundleTag))
                {
                    var req = AssetBundle.LoadFromFileAsync(p.Value.FullPath);
                    abTemp.Add(p.Value.AssetBundleTag, new BundleDes(req, p.Value.FullPath));
                }
            }
            p.Value.Progress = (float)p.Value.FinishNum / (p.Value.Dependencies.Length + 1);
        }

        foreach (var p in mainAssetLoadingDict)
        {
            if (p.Value.isDone)
            {
                if (!cacheDict.ContainsKey(p.Key))
                {
                    cacheDict.Add(p.Key, p.Value.asset);
                }
                if (callbackDict.ContainsKey(p.Key))
                {
                    foreach (var cb in callbackDict[p.Key])
                    {
                        cb(p.Value.asset);
                    }
                    callbackDict[p.Key] = new List <Action <object> >();
                }
                else
                {
                    Debug.LogError(p.Key + " -> callback is none!");
                }
                abUnloadQue.Enqueue(p.Key);
                string tag = AssetDesc.GetAssetBundleTag(p.Key);
                abTemp[tag].ReduceRefrenceCount();
                if (abTemp[tag].refrenceCount <= 0)
                {
                    abUnloadQue.Enqueue(tag);
                }
                foreach (var key in loadingDict[p.Key].Dependencies)
                {
                    abTemp[key].ReduceRefrenceCount();
                    if (abTemp[key].refrenceCount <= 0)
                    {
                        abUnloadQue.Enqueue(key);
                    }
                }
            }
        }

        while (abUnloadQue.Count > 0)
        {
            var str = abUnloadQue.Dequeue();
            if (abTemp.ContainsKey(str))
            {
                abTemp[str].assetBundleReq.assetBundle.Unload(false);
                abTemp.Remove(str);
                //Debug.Log("remove from abTemp -> " + str);
            }
            if (loadingDict.ContainsKey(str))
            {
                loadingDict.Remove(str);
                //Debug.Log("remove from loadingDict -> " + str);
            }
            if (mainAssetLoadingDict.ContainsKey(str))
            {
                mainAssetLoadingDict.Remove(str);
                //Debug.Log("remove from mainAssetLoadingDict -> " + str);
            }
        }
#endif
    }