宕机bug日志分析:
通过logcat观察到的宕机堆栈
案例一
ParticleSystemRenderer::UpdateCachedMesh()atParticleSystemRenderer.cpp:
0x2053c8c:push{r4,r5,r6,r7,lr}
0x2053c8e:add r7,sp,#0xc
0x2053c90:push.w{r8,r10,r11}
0x2053c94:subsp,#0x8
...
0x2053c9a:mov.w r10,#0x0
...
0x2053d16:str.w r10,[sp,#4]
...
0x2053d42:blx0x294c5d8;symbol stubfor:memcpy
0x2053d46:ldr.w r10,[sp,#4] ; The game crashes here
0x2053d4a:b0x2053db0;ParticleSystemRenderer::UpdateCachedMesh()+292[inlined]ListElement::GetNext()constatLinkedList.h:71
案例二:
09-29 22:14:48.257 3093-3093/? A/DEBUG: r0 be2aabb0 r1 00000000 r2 0000000c r3 00000000
09-29 22:14:48.257 3093-3093/? A/DEBUG: r4 cc83fc10 r5 cc0ce8d0 r6 be2aabb0 r7 0000000c
09-29 22:14:48.257 3093-3093/? A/DEBUG: r8 cc83fdac r9 00000000 sl 00000006 fp 00000000
09-29 22:14:48.257 3093-3093/? A/DEBUG: ip 80000000 sp eeb72290 lr e0d8111c pc f6df87cc cpsr a0010010
09-29 22:14:48.307 3093-3093/? A/DEBUG: backtrace:
09-29 22:14:48.307 3093-3093/? A/DEBUG: #00 pc 000177cc /system/lib/
libc.so (__memcpy_base+272)
libunity.so (_ZN22ParticleSystemRenderer16UpdateCachedMeshEv+684)
libunity.so (_ZN18AwakeFromLoadQueue30PersistentManagerAwakeFromLoadEi17AwakeFromLoadMode+176)
libunity.so (_ZN18AwakeFromLoadQueue30PersistentManagerAwakeFromLoadEv+128)
libunity.so (_ZN18LoadSceneOperation21CompleteAwakeSequenceEv+132)
libunity.so (_ZN18LoadSceneOperation25PlayerLoadSceneFromThreadEv+316)
libunity.so (_ZN18LoadSceneOperation19IntegrateMainThreadEv+184)
libunity.so (_ZN14PreloadManager26UpdatePreloadingSingleStepENS_21UpdatePreloadingFlagsEi+388)
libunity.so (_ZN14PreloadManager16UpdatePreloadingEv+328)
libunity.so (_Z10PlayerLoopbbP10IHookEvent+616)
/data/app/com.funova.metaworld-2/lib/arm/
libunity.so (_Z15UnityPlayerLoopv+880)
libunity.so (_Z12nativeRenderP7_JNIEnvP8_jobject+264)
2/oat/arm/base.odex (offset 0x22000) (boolean com.unity3d.player.UnityPlayer.nativeRender()+76)
2/oat/arm/base.odex (offset 0x22000) (boolean com.unity3d.player.UnityPlayer.a(com.unity3d.player
从报错信息字面意思来看,是在渲染过程中,更新缓存的网格信息时,内存拷贝失败,导致宕机。
可以初步判断是mesh这类资源的出的问题,并且是在需要mesh进行内存拷贝的时候。带着这个疑问去搜索了官方资料。
在Models 导入的说明文档中到找到了一个选项:Read/Write Enabled 如果设置为enabled的话,那就可以进行内存拷贝。 文档连接
修复该宕机bug,就是需要把 Read/Write Enabled 勾上选项
Read/Write Enabled 选项说明
Enables the mesh to be written at runtime so you can modify the data; it makes a copy in memory. When this option is turned off, it saves memory since Unity can unload a copy of mesh data in the game. However, if you are scaling or instantiating meshes at runtime with a non-uniform scale, you may have to enable “Read/Write Enabled” in their import settings. The reason is that non-uniform scaling requires the mesh data to be kept in memory. Normally this is detected at build time, but when meshes are scaled or instantiated at runtime you need to set this manually. Otherwise they might not be rendered in game builds correctly. The same applies if you need to create MeshColliders at runtime.
如果导入的mesh需要做 non-uniform scale (不均匀缩放),那就需要进行一次拷贝操作,那么对于mesh导入时,需要设定为可以读写,否者就会出现宕机或渲染的对象不正确的情况。
补充说明,mesh出现这类问题的时候,对MeshColliders也需要做相同的设置操作。
从内存优化角度来说,最好不好在有多份的mesh存在,那样会浪费内存
说明在导入时 Scale Factor 参数的设置会比较重要
Unity’s physics system expects 1 meter in the game world to be 1 unit in the imported file. If you prefer to model at a different scale then you can compensate for it here. Defaults for different 3D packages are as follows .fbx, .max, .jas, .c4d = 0.01, .mb, .ma, .lxo, .dxf, .blend, .dae = 1 .3ds = 0.1
以上说明根据导入mesh的文件的格式,与unity物理系统的之间1m与1个单位的对应关系
尽量在导入时机会做好标准缩放了,那么就不需要在scale 或 instantiating 时进行多份内存拷贝了。
// 检测工具
public static void CheckFXBReadWriteEnable()
{
string artPath = "Assets/Resourcex/art";
var paths = Directory.GetFiles(artPath, "*.meta", SearchOption.AllDirectories).Where(s => s.ToLower().EndsWith("fbx.meta"));
foreach (string path in paths)
{
using (StreamReader fsRead = new StreamReader(path))
{
int isReadable = 0;
float globalScale = 1f;
while (!fsRead.EndOfStream)
{
string line = fsRead.ReadLine();
if (line.IndexOf("isReadable") != -1)
{
string[] items = line.Split(':');
if (items.Length==2)
{
isReadable = Int32.Parse(items[1]);
}
}
else if (line.IndexOf("globalScale") != -1)
{
string[] items = line.Split(':');
if (items.Length == 2)
{
globalScale = float.Parse(items[1]);
}
}
}
if (globalScale != 1 && isReadable == 0)
{
Debug.LogErrorFormat("import model [ {0} ] scale factor is [{1}] must set read/write enable",
path, globalScale);
}
}
}
}