在Unity 2018中,您可以使用AssetBundle.LoadAsset
方法从AssetBundle加载场景
Assets/StreamingAssets
文件夹中。LoadSceneFromAssetBundle.cs
),并将其附加到一个GameObject上。using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadSceneFromAssetBundle : MonoBehaviour
{
public string assetBundlePath = "file://" + Application.streamingAssetsPath + "/your_asset_bundle_name";
public string sceneName = "YourSceneName";
void Start()
{
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
using (WWW www = new WWW(assetBundlePath))
{
yield return www;
AssetBundle assetBundle = www.assetBundle;
if (assetBundle == null)
{
Debug.LogError("Failed to load AssetBundle!");
yield break;
}
SceneAsset sceneAsset = assetBundle.LoadAsset<SceneAsset>(sceneName);
if (sceneAsset == null)
{
Debug.LogError("Failed to load scene from AssetBundle!");
yield break;
}
SceneManager.LoadScene(sceneAsset.name);
}
}
}
请确保将assetBundlePath
变量设置为AssetBundle文件的正确路径,并将sceneName
变量设置为要加载的场景名称。
领取专属 10元无门槛券
手把手带您无忧上云