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

Manifests:<?xml  version="1.0" encoding="utf-8"?>

package="com.cain.unitytest">

android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:xlargeScreens="true"

android:anyDensity="true" />

android:name=".tool.MyApplication"

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

android:name=".activity.MainActivity"

android:screenOrientation="portrait"

android:launchMode="singleTask">

android:screenOrientation="portrait"

android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">

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!

更多推荐