加载本地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);
}
}
}
网友评论