示例:Unity4.x
using UnityEngine;
using System.Collections;
public class SceneManager : MonoBehaviour {
public void LoadSceneAsyn(string sceneName)
{
StartCoroutine(LoadScene(sceneName));
}
IEnumerator LoadScene(string sceneName)
{
yield return null;
//场景AssetBundle路径
string path = string.Format("file://{0}/res/scenes/{1}.scene", Application.streamingAssetsPath, sceneName);
WWW www = new WWW(path);
while (!www.isDone)
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogError("加载资源失败 " + path + "\n" + www.error);
}
else
{
AssetBundle ab = www.assetBundle;
//注意:场景名是打资源包时的资源名称,不是文件名称。
Application.LoadLevel(sceneName);
//注意:必须等场景成功激活后再调用Unload,否则会出现材质和shader丢失。
ab.Unload(false);//释放镜像资源
}
}
// Unity 2017
private IEnumerator LoadSceneAsync(string sceneName)
{
string url = string.Format("file://{0}/scenes/{1}.scene", Application.streamingAssetsPath, sceneName);
WWW w = new WWW (url);
while (!w.isDone)
yield return w;
if (string.IsNullOrEmpty(w.error))
{
AssetBundle bundle = w.assetBundle;//访问assetBundle时所加载的资源才会解析到内存中
AsyncOperation asy = SceneManager.LoadSceneAsync (sceneName, LoadSceneMode.Single);
while (!asy.isDone)
yield return asy;
Debug.Log("ActiveScene>>>"+SceneManager.GetActiveScene().name);
}
else
{
Debug.LogErrorFormat("Load scene failure. Error={0}. \nurl={1}", w.error, url);
}
}
}
示例:Unity 2019
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
public class Test : MonoBehaviour
{
void Start()
{
string url = string.Format("file://{0}/scene/{1}.scene", Application.streamingAssetsPath, "scene2");
//同步加载 (WWW)
StartCoroutine(LoadScene(url));
//异步加载 (WWW)
//StartCoroutine(LoadSceneAsync(url));
//异步加载 (UnityWebRequest)
//StartCoroutine(RequestSceneAsync(url));
}
IEnumerator LoadScene(string url)
{
yield return null;
WWW www = new WWW(url);
while (!www.isDone)
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogErrorFormat("加载资源失败 {0}\n{1}", www.error, www.url);
yield break;
}
AssetBundle ab = www.assetBundle;
string sceneName = Path.GetFileNameWithoutExtension(url);
SceneManager.LoadScene(sceneName);
ab.Unload(false);//释放镜像资源
Debug.Log("Unload the scene assetBundle");
}
IEnumerator LoadSceneAsync(string url)
{
yield return null;
WWW www = new WWW(url);
while (!www.isDone)
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogErrorFormat("加载资源失败 {0}\n{1}", www.error, www.url);
yield break;
}
AssetBundle ab = www.assetBundle;
string sceneName = Path.GetFileNameWithoutExtension(url);
AsyncOperation async = SceneManager.LoadSceneAsync(sceneName);
async.allowSceneActivation = true;
async.completed += OnLoadSceneAsyncCompleted;
//注意:下面这段代码会导致永远无法往下执行
//while (!async.isDone)
//yield return async;
ab.Unload(false);//释放镜像资源
Debug.Log("Unload the scene assetBundle");
}
private void OnLoadSceneAsyncCompleted(AsyncOperation async)
{
Debug.Log("OnLoadSceneAsyncCompleted()");
async.completed -= OnLoadSceneAsyncCompleted;
}
IEnumerator RequestSceneAsync(string url)
{
yield return null;
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
UnityWebRequestAsyncOperation req_async = request.SendWebRequest();
while (!req_async.isDone)
yield return req_async;
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(request);
string sceneName = Path.GetFileNameWithoutExtension(url);
AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
async.allowSceneActivation = true;
async.completed += OnRequestSceneAsyncCompleted;
//注意:下面这段代码会导致永远无法往下执行
//while (!async.isDone)
//yield return async;
assetBundle.Unload(false);//释放镜像资源
Debug.Log("Unload the scene assetBundle");
}
private void OnRequestSceneAsyncCompleted(AsyncOperation async)
{
Debug.LogFormat("OnRequestSceneAsyncCompleted isDone={0}", async.isDone);
async.completed -= OnRequestSceneAsyncCompleted;
if (async is UnityWebRequestAsyncOperation)
{
var asyncRequest = async as UnityWebRequestAsyncOperation;
UnityWebRequest request = asyncRequest.webRequest;
//TODO::
}
}
}