游戏中使用了多个Unity场景,每个Unity场景都对应有NavMesh和LightMap数据。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


当使用 SceneManager.LoadScene 的时候,会自动载入LightMap 和 NavMesh的数据。然后再对MeshRender 进行指定 LightMapIndex 以及 LightMapScaleOffset 就可以了。

这真是很方便的一个功能。


以上是前提。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

这两个礼拜都断断续续在弄Assetbundle 以及资源更新,每天出包测试的时候大家都会反馈,游戏APK很大,现在游戏内容还没有完成但是APK居然超过了200MB,这简直不可忍受。我也腾不出时间去看这个问题,就一直拖着。今天过来加班,修改了T4M 在Assetbundle 模式下 LightMap没有效果的问题后,就来看这个问题。


我拿出的PC包来,打开 xx_Data 这个目录,这里是存放 Resources里面的资源的。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢

首先,这个 level 命名的就是场景了,现在我们游戏里面有这么多场景了……

每个level 文件很小,只有 5kb ,也难怪,场景都是空的。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


然后就发现,还有很多 assets 以及 assets.resS 文件,而且数目正好是和 level 数量相等,看这个体积,我想这是把 NavMesh 和LightMap 也打包进来了。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

这个意思就是说 在 BuildSetting 中添加的 Scene ,都会被打包到游戏包里面。

那问题来了……我打AssetBundle的时候,把LightMap 和NavMesh 以及场景文件也打包了一遍。所以就是说,场景信息被打包了两次,主要就是LightMap,这是个大户。


下面是我打Assetbundle的代码(注意场景文件以及 LightMap、NavMesh的 assetbundlename 要清除)

[MenuItem("BuildAssetbundle/BuildAll")]static void Build()//打包场景 string[] tmpEditorBuildSettingsScene =  {  "Assets/R/Scene/1.unity",  "Assets/R/Scene/2.unity",  "Assets/R/Scene/3.unity",  "Assets/R/Scene/4.unity",  "Assets/R/Scene/5.unity",  "Assets/R/Scene/6.unity",  "Assets/R/Scene/7.unity",  "Assets/R/Scene/8.unity",  "Assets/R/Scene/9.unity",  "Assets/R/Scene/10.unity",  "Assets/R/Scene/11.unity",  "Assets/R/Scene/12.unity",  "Assets/R/Scene/13.unity",  "Assets/R/Scene/14.unity",  "Assets/R/Scene/15.unity",  "Assets/R/Scene/16.unity", }; EditorUtility.DisplayProgressBar("BuildPlayer", "BuildSceneAssetbundle", 0f); for (int i=0;i<tmpEditorBuildSettingsScene.Length;i++) {  string tmpSceneAssetbundleFilePath = Application.dataPath + "/../Assetbundle/" + tmpEditorBuildSettingsScene[i].ToLower();  string tmpSceneAssetbundleDirectoryPath = Path.GetDirectoryName(tmpSceneAssetbundleFilePath);  if (Directory.Exists(tmpSceneAssetbundleDirectoryPath)==false)  {   Directory.CreateDirectory(tmpSceneAssetbundleDirectoryPath);  }  string[] tmpLevels = { tmpEditorBuildSettingsScene[i] };  BuildPipeline.BuildPlayer(tmpLevels, tmpSceneAssetbundleFilePath, EditorUserBuildSettings.activeBuildTarget, BuildOptions.BuildAdditionalStreamedScenes);  EditorUtility.DisplayProgressBar("BuildPlayer", "BuildSceneAssetbundle", i*1.0f/ tmpEditorBuildSettingsScene.Length); } EditorUtility.ClearProgressBar(); //打包资源 BuildPipeline.BuildAssetBundles(Application.dataPath + "/../Assetbundle", BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.DeterministicAssetBundle,EditorUserBuildSettings.activeBuildTarget);}

打包出来的场景Assetbundle 。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


所以呢,只好在打包的时候 从BuildSetting 中去掉各个场景,留下最开始的StartGame场景。

这样就不会打包两次场景文件了。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢

从Assetbundle中加载场景代码如下

if(GUILayout.Button("LoadScene")){ ResourcesManager.GetSingleton().Load("Scene/1.unity", (varObject) => {  UnityEngine.SceneManagement.SceneManager.LoadScene("1"); });}

加载完Assetbundle之后,就可以直接调用 LoadScene 了,需要注意的是 ,LoadScene 参数传递的是 场景名字,而不是相对路径了。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

从BuildSetting 中删除所有的场景后,打出一个PC包,又发现问题,LightMap 全部丢失了!!

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


这真是……

想了一下,我把BuildSetting中的场景全部删掉了,所以才出现了这个情况。不仅LightMap 没有了效果,而且本来场景中有一个Light,也不见了。


一开始我以为是LightMap没有打进到Assetbundle,所以从Profile找,发现Lightmap是已经加载了的。

所以可能是引用丢失了?而且场景中的灯也不见了是怎么回事呢?


迷惑了一下午+一晚上。

在Graphic 设定面板上看到一个 Shader stripping设置……

这让我想起了Unity刚推出 IL2CPP的时候,对没有使用过的Class 进行裁剪,然后导致的各种莫名其妙的Bug……


对的,这个Shader stripping 默认是开启的,Automatic 自动模式。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


本着万马奔腾而过的心理尝试了去修改这个设置转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢


然后重新打包,果然就可以了。

Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

相关文章: