Unity Android Crash 简单定位

官方文档Symbolicate Android Crash

unity安卓项目有时候会出现安卓闪退,崩溃出来的堆栈类似如下

2020-01-13 15:40:47.554 12602-12739/xx.xx.xx.xx E/CRASH:

#00 pc 000000000000069c [vdso] () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#01 pc 000000000001ceb8 /system/lib64/libc.so (memset+136) 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#02 pc 0000000000a5abe4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#03 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#04 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#05 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#06 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#07 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#08 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#09 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#10 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#11 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#12 pc 0000000000a5ac00 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#13 pc 0000000000a38fc4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#14 pc 0000000000a4ac54 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#15 pc 0000000000a4aaa8 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#16 pc 00000000004efae0 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libunity.so (_ZN11Marshalling17ArrayUnmarshallerINS_18StringArrayElementES1_E18ArrayFromContainerISt6vectorIN4core12basic_stringIcNS5_20Stri ngStorageDefaultIcEEEESaIS9_EELb1EE14UnmarshalArrayERKSB_+148) 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#17 pc 00000000004ef170 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libunity.so (_Z48AssetBundleManifest_CUSTOM_GetDirectDependenciesP12Il2CppObjectP12Il2CppString+244) 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#18 pc 0000000000f6a378 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#19 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#20 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#21 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#22 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#23 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#24 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#25 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#26 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#27 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#28 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#29 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#30 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so () 2020-01-13 15:40:47.555 12602-12739/xx.xx.xx.xx E/CRASH:

#31 pc 0000000000f6a3f4 /data/app/xx.xx.xx.xx-tcfkPS-N3WH5aQuqMs6lVA==/lib/arm64/libil2cpp.so ()

unity的符号表文件一般在导出安卓项目时生成的symbols.zip文件里。
可以通过使用addr2line工具将上面崩溃堆栈的内存地址转成具体的方法,addr2line在ndk的文件夹下找到(以mac版本r16b为例):
64位:android-ndk-r16b/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin/aarch64-linux-android-addr2line
32位:android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-addr2line

例如

./aarch64-linux-android-addr2line -f -C -e   libil2cpp.sym 0000000000f6a378 0000000000f6a3f4

执行结果类似如下

BundleManager_LoadTask_mD57E35306E924ABDF527B9ABC7E4C1C412484CAA
...../StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:39598
BundleManager_LoadTask_mD57E35306E924ABDF527B9ABC7E4C1C412484CAA
..../StagingArea/Il2Cpp/il2cppOutput/Bulk_Assembly-CSharp_0.cpp:39653

unity 异步加载动画与AlwaysAnimate裁剪冲突

最近项目发现一个奇怪问题,就是异步加载某个模型,然后模型本身又带有动画的时候,会出现动画刚加载好时不是直接进入Entry指向的状态,而是会卡一帧。经查发现这些动画的裁剪方式都是AlwaysAnimate,没有找到原因,默默将模型默认的裁剪改回CullUpdateTransforms,然后在加载完后延迟一帧再设置裁剪方式就解决。

unity接入firebase分析和google登录冲突解决

unity版本2018.4
play-services-resolver_v1.2.124.0

gradle版本3.4
targetSdkVersion 28

unity要接入sdk,先在Firebase去下载了unity版本的Analysis的package导入,测试apk通过,希望接入G+登录时,因为没有专门给unity的版本,所以直接去google的官网,在安卓工程的build.gradle里添加了

implementation 'com.google.android.gms:play-services-auth:17.0.0'

然后发现这个库和unity那边导出的一堆jar和aar出现了类似于Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core.core-1.0.0.aar) and classes.jar (androidx.core:core:1.0.0)的报错等。

可以通过删除unity导出的对应aar和jar来解决,也可以通过在implementation google的库的时候,exclude掉冲突的部分解决,如下

implementation ('com.google.android.gms:play-services-auth:17.0.0') {
    exclude group: 'androidx.core',module:'core'
    exclude group: 'androidx.annotation',module:'annotation'
    exclude group: 'androidx.arch.core',module:'core-runtime'
    exclude group: 'androidx.asynclayoutinflater',module:'asynclayoutinflater'
    exclude group: 'androidx.arch.core',module:'core-common'
    exclude group: 'androidx.collection',module:'collection'
    exclude group: 'androidx.coordinatorlayout',module:'coordinatorlayout'
    exclude group: 'androidx.cursoradapter',module:'cursoradapter'
    exclude group: 'androidx.documentfile',module:'documentfile'
    exclude group: 'androidx.drawerlayout',module:'drawerlayout'
    exclude group: 'androidx.fragment',module:'fragment'
    exclude group: 'androidx.interpolator',module:'interpolator'
    exclude group: 'androidx.legacy',module:'legacy-support-core-ui'
    exclude group: 'androidx.legacy',module:'legacy-support-core-utils'
    exclude group: 'androidx.lifecycle',module:'lifecycle-viewmodel'
    exclude group: 'androidx.lifecycle',module:'lifecycle-common'
    exclude group: 'androidx.lifecycle',module:'lifecycle-livedata'
    exclude group: 'androidx.loader',module:'loader'
    exclude group: 'com.google.android.gms',module:'play-services-basement'
    exclude group: 'com.google.android.gms',module:'play-services-base'
    exclude group: 'com.google.android.gms',module:'play-services-tasks'
}

修改Unity的UI的默认shader

本文学习自宣雨松大大的《Unity 3D游戏开发》。

Unity的UI元素例如Image是不需要指定材质,UGUI提供了默认的材质和Shader,这个Shader是比较全面各种情况都适用。但是其实实际开发中可以进行一些取舍,例如去掉颜色的乘法和背面渲染。我们可以取出Unity的内置Shader源码,对其进行一些修改。但是注意这个修改会不仅仅对图片生效,也会对Text生效,如果Text没有做成通用Prefab,则建议不要进行修改,或者重写一下生成Text的菜单。

  • 1、在Unity的下载页选择对应版本的Build in Shaders,我下的版本是builtin_shaders-2018.4.2f1,解压缩后,UI的默认Shader就是DefaultResourcesExtra/UI/UI-Default.shader

  • 2、找到Shader里的Cull Off,改成Cull Back,表示剔除背面裁剪

  • 3、在片段着色器方法frag里找到下面代码
    half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;改成 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);

  • 4、在Unity的Project Settings下面的Graphic栏目,在Always include shaders里的UI/Default修改成绑定上面的Shader,重启Unity后生效。

  • 5、新建一个空白材质球,把原本的UI-Default.shader赋予他,但是注意修改一下该Shader的路径,例如我的修改成Default_Unity。将该Shader也绑定到Always include shaders。

附上Text创建重写代码


[MenuItem("GameObject/UI/Text")] static void CreateText() { if(Selection.activeTransform) { if(Selection.activeTransform.GetComponentInParent<Canvas>()) { Text text = new GameObject("text").AddComponent<Text>(); //Change to your shader setting path /* Shader "UI/Default_Unity" { Properties { ... */ Material mat = new Material(Shader.Find("UI/Default_Unity")); text.material = mat; text.transform.SetParent(Selection.activeTransform, false); Selection.activeTransform = text.transform; } } }

上图中两张图片(感谢主子免费出场),左边的是用的Unity的原始的Shader,即自己设置了材质球,材质球绑定了的UI/Default_Unity,右边的是修改过的默认的Unity的UI shader,文本控件也是自行设置了材质球。