예제 #1
0
    private static IEnumerator LoadSceneByCoroutine(string bundlePath, string sceneName, Action <int> onLoadFinishCallBack)
    {
        //todo 这里应该继续加载所有依赖项
        string[] dependencies = manifest.GetAllDependencies(bundlePath);

        string assetURL = PathDefine.GetAssetUrl(bundlePath);
        var    req      = AssetBundle.LoadFromFileAsync(assetURL);

        yield return(req);

        if (req == null)
        {
            Debug.LogError("加载场景Bundle失败  " + assetURL);
        }
        else
        {
            AssetBundle ab = req.assetBundle;
        }

        AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);

        yield return(ao);

        if (ao.isDone && onLoadFinishCallBack != null)
        {
            onLoadFinishCallBack.Invoke((int)LocalCode.SUCCESS);
        }

        req.assetBundle.Unload(false);
    }
예제 #2
0
    private static void CheckInit()
    {
        if (manifest != null)
        {
            return;
        }

        string      mainBundleUrl = PathDefine.GetAssetUrl("AssetsBundle");
        AssetBundle mainBundle    = AssetBundle.LoadFromFile(mainBundleUrl, 0);

        if (mainBundle == null)
        {
            Debug.LogError("找不到AssetsBundle  " + mainBundleUrl);
            Debug.LogError(LocalCode.DOWNLOAD_ASSETBUNDLEFILE_FAULT);
        }

        manifest = mainBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
        mainBundle.Unload(false);
    }
예제 #3
0
    private static IEnumerator LoadAssetByCoroutine(string bundlePath, string name, Action <UnityEngine.Object> onLoadFinishCallBack)
    {
        string assetURL = PathDefine.GetAssetUrl(bundlePath);

        AssetBundle ab = null;

        AssetBundleCreateRequest req = null;

        if (abDict.ContainsKey(bundlePath))
        {
            ab = abDict[bundlePath];
        }
        else
        {
            Debug.LogError("assetURL  " + assetURL);
            req = AssetBundle.LoadFromFileAsync(assetURL);
            ab  = req.assetBundle;
            abDict.Add(bundlePath, ab);
        }

        yield return(ab);

        if (ab == null)
        {
            Debug.LogError("加载  " + bundlePath + "  失败----");
            onLoadFinishCallBack.Invoke(null);
        }
        else
        {
            UnityEngine.GameObject o = null;
            if (gameObjectPool.ContainsKey(name))
            {
                o = gameObjectPool[name];
            }
            else
            {
                o = ab.LoadAsset <GameObject>(name);
                gameObjectPool.Add(name, o);
            }

            //这里应该继续加载所有依赖项
            string[]           dependencies = manifest.GetAllDependencies(bundlePath);
            List <AssetBundle> abList       = new List <AssetBundle>();
            if (dependencies.Length > 0)
            {
                for (int i = 0; i < dependencies.Length; i++)
                {
                    int index = i;

                    string depPath = PathDefine.GetAssetUrl(dependencies[i]);
                    var    de      = AssetBundle.LoadFromFileAsync(depPath);
                    yield return(de);

                    abList.Add(de.assetBundle);
                }
                onLoadFinishCallBack.Invoke(o);

                for (int i = 0; i < abList.Count; i++)
                {
                    abList[i].Unload(false);
                }
            }
            else
            {
                onLoadFinishCallBack.Invoke(o);
            }
        }
    }