Unity整合Android , 错误Fatal signal 11 (SIGSEGV)

Unity整合Android时异常

把Unity内容整合到Android工程后出现so文件

出现类似异常:

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4ab01 in tid 28051 (UnityHardwareCa)

或者

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x3c in tid 4674 (UnityGfxDeviceW)

Unity整合Android , 错误Fatal signal 11 (SIGSEGV)_第1张图片

看描述貌似是指 libunity.so库引用指针0x4ab01 时出现signal, 网络很多人说是u3d没有使用“多线程渲染”,但是查看u3d人员构建时确实选择多线程渲染, 所以这理由又胎死腹中。。

最后经过多方咨询 和 试验 得出结论, 如果是包含有原生Activity跳转相关操作时, 会对UnityPlayer控件执行相应的pause()resume()操作, 当频繁地执行pause和resume切换时就会出现以上异常。

请看封装Activity源码:

public class UnityPlayerActivity extends Activity
{
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

    @Override protected void onNewIntent(Intent intent)
    {
        setIntent(intent);
    }

    // Quit Unity
    @Override protected void onDestroy ()
    {
        mUnityPlayer.quit();
        super.onDestroy();
    }

    // Pause Unity
    @Override protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }

    // Resume Unity
    @Override protected void onResume()
    {
        super.onResume();
        mUnityPlayer.resume();
    }

    //............
}

以上源码可以看出,UnityPlayer的生命周期是与Acitivty生命周期绑定的,我要说的是:问题就出在这!

解决方案:
1. 创建Activity时就调用UnityPlayer.resume();
2. 界面重启时先从界面中移除、再重新add;
3. Activity执行pause和resume时不需要调用其pause和resume。

重写onCreate()、onRestart()、 onPause()、onResume() :

public class UnityPlayerActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.RGBX_8888);
        setContentView(R.layout.activity_main2);

        mUnityPlayer = new UnityPlayer(this);
        root = (ViewGroup)findViewById(R.id.root_layout);

        //注: 创建Activity时就调用UnityPlayer.resume()
        if (mUnityPlayer != null) {
            root.addView(mUnityPlayer, 0);
            mUnityPlayer.requestFocus();
            mUnityPlayer.resume();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //注: 界面重启时先从界面中移除, 再重新add
        if (mUnityPlayer != null && root != null) {
            root.removeView(mUnityPlayer);
            mUnityPlayer.requestFocus();
        }
        if (mUnityPlayer != null) {
            root.addView(mUnityPlayer, 0);
        }
    }

   //注: 不需要调用其pause和resume
   /*@Override protected void onPause() {
        super.onPause();
        mUnityPlayer.pause();
    }

    @Override protected void onResume() {
        super.onResume();
        mUnityPlayer.resume();
    }*/
}

你可能感兴趣的:(Android总结,unity)