This method cannot be used on a streamed scene AssetBundle

不能用

//伪代码
asssetbundle.LoadAsset()

代码讲的是,加载了一个包含*.unity的资源包,然后加载资源包中的BG 1纹理,这个BG 1纹理是存在的。结果纹理加载失败了~

会抛出异常:InvalidOperationException: This method cannot be used on a streamed scene AssetBundle.表示LoadXX方法不能应用在动态场景资源包上。

  • 起因

任何一个包含*.unity的资源包都属于动态场景资源包,对应assetBundle.isStreamedSceneAssetBundle = true,动态场景资源包是不允许手动去访问其内部的资源的。

  • 重点
    • 如果你的资源需要通过代码访问,那么在打包时不要与*.unity文件捆绑为一个文件

但我们又需要.unity,如何办?

	/// <summary>
	/// 拓展思考:一个舍近求远的操作,怎么就不用AssetBundle.Load()??
	/// </summary>
	/// <param name="relativePath"></param>
	/// <param name="sceneName"></param>
	/// <param name="mode"></param>
	/// <param name="onComplete"></param>
	/// <returns></returns>
	static IEnumerator CorLoadLevel(string relativePath,string sceneName,LoadSceneMode mode,Action onComplete) {
		//string path = "file://" + Application.dataPath + "/Resources/" + sceneAssetBundle;
		string path = "file://" + Application.persistentDataPath + "/AssetBundle/Android/" + relativePath;
		sceneName = Path.GetFileNameWithoutExtension(sceneName);
		WWW www = WWW.LoadFromCacheOrDownload(path, 0);

		yield return www;
		if (www.error == null)
		{

			//下面一行的对象ab貌似没用到,但是不写的话,会报错如下:

			//Scene 'sceneAssetBundleTest'(-1) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.

			//To add a scene to the build settings use the menu File->Build Settings...

			//UnityEngine.SceneManagement.SceneManager:LoadSceneAsync(String)

			//场景没加到Build Settings的场景列表中或AssetBundle未加载。

			AssetBundle ab = www.assetBundle; //将场景通过AssetBundle方式加载到内存中

			//Application.LoadLevelAsync(sceneName); 弃用的

			AsyncOperation asy = SceneManager.LoadSceneAsync(sceneName,mode); //sceneName不能加后缀,只是场景名称
			onComplete();
			yield return asy;

		}

		else
		{
			
			Debug.LogError(www.error);
			onComplete();
		}
	}

调用方法

//GameRoot请自行找一个monobehaviour,或者GameFacade替代
GameRoot.Instance.StartCoroutine(CorLoadLevel(assetPath, assetName, mode, onComplete));

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值