Unity3D Editor 中加载移动平台的 AssetBundle 资源显示出错的解决方法

注意:本文的测试环境为 Mac OSX 、Unity 4.6.9,Unity 5 在 AssetBundle 上做了诸多调整,因未实际测试不保证测试效果会相同。


在 Unity3D 项目开发的过程中,我们肯定会遇到需要使用 AssetBundle 的时候,而且这货还确实应用之处满多的,今天咱们不展开聊 AssetBundle 能干嘛了 ,咱们把重点放到 Unity Editor 加载移动平台的 AssetBundle 资源之后,显示出现错误的问题。我们直接来看一下对比图,快速了解一下我们要解决的问题:

asset bundle-miss-shader assetbundle-with-shader

两个效果图一对比,我们马上就明白了,我们碰到的问题是左侧这个显示成粉色/紫色的这个图片呈现出来的,而右侧这个图呈现的就是我们想要的效果。

第一步,让我们来明确这个问题的定义:

  1. 首先需要说明的一点,这个问题只在 Unity Editor 下出现,在 iOS 和 Android 平台的设备上运行并不会出现这个问题,也就是该问题不影响游戏在移动设备真机上运行,只是让我们在开发的过程中感到很迷惑和无奈;
  2. 对于 Unity 系统 built-in 的那些 Shader 也不会出现这个问题,只针对我们自行编写的 Custom Shader 会出现这个问题;

第二步,让我们来确定如何重现这个问题,通常找到重现问题的方法也就找到了问题的根源所在了:

  1. 新建一个 Custom 的 Shader,咱们可以直接把 Unity 官方提供的 Shader 源码包给下载下来,然后直接修改其中的 Diffuse Shader,咱们不做任何其他改动,就简单的改两个 Property 的 Name 就好了;
  2. 将新建的 Custom-Diffuse Shader 应用于场景中某个 3D 对象的 Material 上,然后将该对象保存为 Prefab 文件;
  3. 将该 Prefab 文件打包成 AssetBundle 资源文件,打包选项为 BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets,编译目标设置为 Android 或者 iOS ;
  4. 新建一个空的 Unity 工程,将上一步中打包出来的 AssetBundle 文件放到 Assets/StreamingAssets 目录下,然后通过 WWW 或者通过读取文件内容字节数组后创建 AssetBundle,然后将该 AssetBundle 资源中的 Prefab 读取出来并实例化到场景中,最终我们看到的效果就是上面左侧图片呈现的情况。

第三步,找到重现的方法了,接下来就是探究问题根源了,我们可以先做几个假设,问自己几个问题:

  1. 如果不使用自己编写的 Shader,直接给 Prefab 中的 3D 对象设置一个 built-in 的 Shader,是不是就不会出现这个问题了?
  2. 如果自己编写的 Shader 中加入 Fallback “Diffuse” 代码片段之后,是否可以挽回这个显示成粉色/紫色的情况,转而使用 Unity built-in 的 Diffuse Shader 进行渲染呢?

经过测试之后,上面两个问题的答案如下:

  1. 使用 Unity built-in 的 Shader 确实不会出现丢失 Shader 导致显示错误的问题;
  2. 加入 Fallback “Diffuse” 代码片段后的 Custom Shader 不会出现丢失 Shader 之后显示成粉色/紫色的情况,但是会直接使用 Unity built-in Diffuse 的 Shader 进行渲染,跟我们在 Custom Shader 中编写的渲染逻辑半毛钱关系都木有。

至此,我们大体可以确定就是我们打包到指定 BuildTarget 平台上的 AssetBundle 中的 Prefab 资源中使用的 Shader 是成功被打包进去了,因为从上面的第二个问题中,我们能确定 Fallback 语句是生效的,如果我们想确认得更明确一些,我们可以把 Fallback 中指定的 Shader 更换为其他的 built-in 的 Shader 再试试看效果,相信我木有错滴。

既然我们现在已经确定了 Shader 是成功编译后打包进了 AssetBundle 中,那么我们可以看看直接读取 AssetBundle 资源然后初始化 Prefab 之后,这个出问题的 3D 对象使用的 Shader 是个什么情况,以及正常情况下的 Shader 应该是啥情况:

shader-in-assetbundle-for-ios shader-in-assetbundle-for-osx

上面两个图片是我们已经找到问题之后针对出现渲染错误和正确渲染两个情况打包的两个 Shader 在 Editor 中运行时通过点击3D 对象的 Material 组件 Inspector 面板右侧的 Edit 按钮进入的 Shader 的 Inspector 面板。由此我们可以看到我们在 OSX 系统下的 Unity Editor 中使用针对 iPhone 平台打包的 AssetBundle 中的 Shader 在运行时有一个警告输出,而使用针对 OSX 平台打包的 AssetBundle 中的 Shader 就一切正常。

那么我们再来看看这个警告的信息是啥意思呗,No subshaders can run on this graphics card,这个信息也很明确,就是说在你当前的这个显卡上没法执行这个 AssetBundle 中打包的 Shader 中的 subshader。噢,原来如此,针对移动平台打包的 Shader 应该是针对移动设备显卡进行适配和优化的,因为它们通常都是基于 OpenGL ES 标准的,而 Windows 和 OSX 分别是机遇 Direct X 和 OpenGL 的,这也就说得通了。

但是我们还会问一个问题,那就是我明明已经将我的 Unity Editor 的 Build Setting 设置为了目标移动平台,例如 iOS 或者 Android 了啊,为啥加载 AssetBundle 中的 Shader 进行显示时,这个 Unity Editor 就不能按照在手机上的机制来加载这个 AssetBundle 中的 Shader 资源并进行渲染呢?好吧,你把我问住了,实际上我也并不知道为什么 Unity 没有这么做,跪了。那我们就放狗搜索一下吧,最终我找到了两篇不错的文章,咱们先来看这篇点睛之文,这个讨论中有一个名字叫 superpig (超猪)的 Unity 官方的哥们写了以下两个回复:

Just to be clear: this is not one single bug.

The pink material stuff just means there was a problem setting up the material and/or shader. There’s a bunch of ways this can happen. Two of them include:

  • Loading bundles on one platform that were built for another. For example, if you build your bundles for iOS and then try loading them on PC, it won’t work because PC needs the Direct3D versions of the shaders and the bundle won’t contain those (because there is no Direct3D on iOS). To fix this you just need to make sure that the bundles you are loading were built for the platform you are loading them on.
  • Failing to load the bundle that contains your shader (or loading it after the one with the material in). As of Unity 5.0 the AssetBundle pipeline will calculate the dependencies between the bundles, but it won’t automatically load the bundles for you (because in general it has no idea where to get them from). To fix this you just need to load all the required bundles, and you need to do it in the correct order. The AssetBundleManager demo project on the Asset Store demonstrates how you can use the AssetBundleManifest to implement your own automatic dependency loading.

We can and will improve the error reporting around these scenarios, but in both cases there is no engine ‘bug’ per se – just a relatively unhelpful failure behavior.

So, if you are still facing this issue, and neither of the mistakes described above fix your problem, please file a bug report instead of assuming that somebody else has done it – because it is very likely that their problem is not the same as yours exactly, and their problem will get fixed, but yours will not because it’s actually a different case.

——————– 我是两个不同论坛回复内容的分割线 ——————

Switching the platform to Android won’t work. If your Editor is running on Windows, you need a Windows version of the bundle. If it’s Mac, you need a Mac version. It doesn’t matter which platform is your active build target platform.

我把重点的内容标红了一下,看完了这两段回复之后,我们终于搞清楚了 Unity 对于 AssetBundle 中编译打包的 Shader 是个什么处理机制了,这就因为着我们在 Unity Editor 中如果需要正确加载 AssetBundle 中 Shader 并进行渲染的话,我们就需要使用针对我们 Unity Editor 所在的宿主系统环境进行打包,例如我们使用的是 Windows 系统下的 Unity Editor 那么就需要使用针对 Windows 平台打包的 AssetBundle,对于 OSX 系统也是一样的。

至此我们就可以再赶紧验证一下看看素不素这个鬼样子的,建议在资源很少的工程中单独进行实现,因为我知道你们的 Unity Editor 现在肯定是在某个移动平台的 Build Setting 下的,这样贸然打包一个非移动平台的 AssetBundle 会直接触发 Unity Editor 整个的 Switch Platform 操作,我想你肯定不会想这样的,我们都已经被 Unity Editor 这个切换平台耗费过很多生命了。最终测试通过,确实如这位「超猪」先生所说的。


本文原本到这儿呢,就应该结束了,对不对捏?嗯,是的,但是我还有些话需要说,那就是我自己在碰到这个问题的时候我第一时间也放狗搜索了,找到了很多类似的解决方案,大部分都说可以通过在加载 AssetBundle 资源成功之后,将其中的 Material 资源读取出来,然后将这些 Material 使用的 Shader 重新设置一遍,然后就可以了(其实在放狗之前,我自己已经发现可以通过自己手动地在 Editor 中对这些显示出错的 Material 指定一下 Shader 就可以让其正确显示了)。那么这个问题的根源是啥呢?把这个归结为 Unity 的 Bug?显然,这样是一种很不负责任的做法,因为我只用了一个很简单的测试就把这个 Hack Fix 给推翻了,并且确定了这并非 Unity 的问题。

测试这个 Hack Fix 是否真正触及到问题的根源很简单。

首先,我们准备一个工程用于创建 AssetBundle 资源,名为 CreateAssetBundle,在这个工程里头我们只放我们需要打包的 Model、Texture、Material 和 Shader 等资源文件,然后创建一个使用了这些资源的 Prefab 文件,再将我们创建出来的 Prefab 文件打包成为 AssetBundle 文件,这样我们就准备好了一个可用的 AssetBundle 文件了,对伐。

接下来,我们创建一个新的空工程,只把上一步中打包出来的 AssetBundle 资源放到 Assets/StreamingAssets 目录下,然后我们读取这个 AssetBundle 资源,取出里头的 Prefab 资源,在场景中实例化一下,并且使用下面这段 Hack Fix 代码:

#if UNITY_EDITOR_OSX || UNITY_EDITOR
		var mats = bundle.assetBundle.LoadAll (typeof (Material));
		foreach (Material mat in mats) {
			var shaderName = mat.shader.name;
			var shaderInRuntime = Shader.Find (shaderName);
			if (shaderInRuntime != null) {
				mat.shader = shaderInRuntime;
				Debug.Log (string.Format ("Found the shader: {0} used in mat: {1}", shaderName, mat.name));
			} else {
				Debug.Log (string.Format ("Cant not find the shader: {0} used in mat: {1}", shaderName, mat.name));
			}
		}
#endif

然后你会发现,WTF,根本就是然并卵嘛,说好的自行车呢?那么我们再来问问自己,这段代码最核心的是啥?

就是使用 Shader.Find (string name) 方法重新按照我们 Prefab 中指定使用的 Shader 名称在整个程序内容空间中查找了一次 Shader,然后如果成功找到的话,就将其重新设置给我们从 AssetBundle 中读出来的 Material 对象,对伐?那么为何网上那么多的人都说这个解决了他们的问题呢?而且然后就没有然后了呢?

咱们来想想我们为啥会碰到出现在 Editor 中出现加载了 iOS 或者 Android 平台的 AssetBundle 资源呢?也就是我们还是在开发的过程中,我们已经将某一部分资源针对移动平台打包了,这些资源中会把我们在工程中的 Shader 文件一并编译打包,但是我们的工程中的 Shader 文件并不会移除,所以在 Editor 中运行游戏时,当我们出现了因为 Editor Host 不支持针对移动平台编译的 AssetBundle 中的 Shader 渲染的情况时,我们通过了一个上面代码中的 Hack 的手段,使用了 Shader.Find 方法,而且这个方法查找到的就只是工程中未编译的 Shader,根本就不会去我们加载的 AssetBundle 中加载,实际上是在运行时将游戏中的 Materail 中使用的 Shader 偷换成了我们工程中的 Shader,而这些 Shader 并未编译成某个移动平台的,在 Editor Host 环境下肯定是可以正常渲染的,否则我们早都发现渲染不对了。这就是为何我需要创建一个新的空的工程来测试我们打包出来的 AssetBundle 资源,这样我们就可以保证我们在工程中不会有同名的Shader,如此一来这个 Shader.Find 方法肯定是找不到同名的 Shader 了,因为我们这个工程就只有一个 AssetBundle 资源,由此我们就找到了整个问题的各个根源,同时也对网上很多解决方案中提出来的 Hack 方法也提出了疑问并做出了自己的解答。

不过话再说回来,如果为了省事避免维护多个不会使用到的平台的 AssetBundle (通常我们的游戏都只会在 Android 和 iOS 平台发布,实际上 Windows 和 OSX 平台下的 AssetBundle 就没有什么必要维护了),这个 Hack 还是可以用的,但是我们还是要搞清楚为啥这个东西是可用的,下次如果碰到类似的问题或者不是 Shader 的问题,至少也可以提供一个正确的思考方式和参考。

Unity3D Editor 中加载移动平台的 AssetBundle 资源显示出错的解决方法》有2个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据