美文网首页
Unity 加载Assetbundle场景

Unity 加载Assetbundle场景

作者: 114105lijia | 来源:发表于2022-07-13 17:12 被阅读0次

    加载本地AssetBundle场景:需要先将场景打包成AssetBundle,放在项目Assets下面的StreamingAssets目录下。

    using UnityEngine.SceneManagement;
    
    void TestLoadLocalSceneAB() {
            //注意assetBundleName和sceneName可能不同,一个是AssetBundle的名字,一个是Scene的名字
            string assetBundleName = "keshengsquare.unity3d";
            string sceneName = "KeshengSquare";
            string path = Application.dataPath + "/StreamingAssets/" + assetBundleName;
    
            var myLoadedAssetBundle = AssetBundle.LoadFromFile(path);
    
            if (myLoadedAssetBundle != null)
            {
                SceneManager.LoadScene(sceneName);
            }
        }
    

    加载网络AssetBundle场景

    using UnityEngine.Networking;
    using UnityEngine.SceneManagement;
    
    IEnumerator DownLoadSceneAB(string url, string sceneName)
        {
            UnityWebRequest request = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(url);
    
            yield return request.SendWebRequest();
    
            if (request.isDone)
            {
                if (request.result != UnityWebRequest.Result.Success)
                {
                    Debug.Log(request.error);
                }
                else
                {
                    SceneManager.LoadScene(sceneName);
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:Unity 加载Assetbundle场景

          本文链接:https://www.haomeiwen.com/subject/wkdtirtx.html