为了账号安全,请及时绑定邮箱和手机立即绑定

Android内嵌unity 从MainActivity跳转至UnityActivity再finish回来会crash的错误

/ 猿问

Android内嵌unity 从MainActivity跳转至UnityActivity再finish回来会crash的错误

Cain_Archfiend 2016-07-21 16:41:02

我新建了一个简单的unity工程,导出为简单的不可直接用的android工程。然后新建一个安卓工程,将unity导出的资源一一拷贝进去,然后在mainActivity中跳转至unityActivity,但是每次finish当前unityactivity都会crash掉,下面是我的安卓代码,求大神们帮忙看看这是为什么,感激不尽!

Manifests:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cain.unitytest">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true" />

    <uses-feature android:glEsVersion="0x00020000" />

    <application
        android:name=".tool.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.MainActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.UnityPlayerActivity"
            android:screenOrientation="portrait"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
            <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
        </activity>
    </application>

</manifest>

UnityPlayerActivity:

package com.cain.unitytest.activity;

import com.cain.unitytest.R;
import com.cain.unitytest.inject.ContentView;
import com.cain.unitytest.inject.ViewInject;
import com.unity3d.player.*;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressWarnings("unused")
@ContentView(R.layout.activity_unity)
public class UnityPlayerActivity extends BaseActivity {
    private static final String TAG = UnityPlayerActivity.class.getSimpleName();

    protected UnityPlayer mUnityPlayer;     // don't change the name of this variable; referenced from native code

    @ViewInject(R.id.unity_test_view)
    private LinearLayout unityPanel;

    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUnityPlayer = new UnityPlayer(this);

        unityPanel.addView(mUnityPlayer);
    }

    /**
     * 改变背景底色,调用unity函数
     * @param v 发起组件
     */
    public void changeBackgroundColor(View v) {
        UnityPlayer.UnitySendMessage("AndroidInterface", "ChangeCameraBackground", "#fafafa");
    }

    /**
     * 显示toast,被unity调用
     * @param message toast内容
     */
    public void showToast(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(UnityPlayerActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 返回上个页面
     */
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        //UnityPlayer.UnitySendMessage("AndroidInterface", "Finish", "");
        //mUnityPlayer.quit();
        finish();
    }

    // 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();
    }

    // This ensures the layout will be correct.
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mUnityPlayer.configurationChanged(newConfig);
    }

    // Notify Unity of the focus change.
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        mUnityPlayer.windowFocusChanged(hasFocus);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mUnityPlayer.injectEvent(event);
    }
}

每次crash都是底层的错误,每次都有下面的错误:
启动unity页面时:
07-21 15:54:55.810 331-917/? E/AudioALSAPlaybackHandlerBase: openPcmDriver(), pcm_start(0xf20817c0) fail due to cannot start channel: Broken pipe

07-21 15:54:55.857 331-917/? E/AudioALSAPlaybackHandlerBase: -getHardwareBufferInfo pcm_get_htimestamp fail, ret = -1, pcm_get_error = cannot start channel: Broken pipe

crash时:
07-21 15:54:59.407 825-943/? E/InputDispatcher: channel '1dede42c com.cain.unitytest/com.cain.unitytest.activity.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

07-21 15:54:59.407 825-943/? E/InputDispatcher: channel '10dcff56 com.cain.unitytest/com.cain.unitytest.activity.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!


查看完整描述

2 回答

?
qq_allin_10

求指导  邮箱hjs1210918@163.com

查看完整回答
反对 回复 2018-08-01
?
leiving

你好,我也遇到这个问题,请问你解决了么?能把解决方案贴一下么?

查看完整回答
反对 回复 2017-01-13

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信