我新建了一個簡單的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="https://bbs.csdn.net/topics/true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="https://bbs.csdn.net/topics/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 pipe07-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!
uj5u.com熱心網友回復:
這個問題解決了嗎?我也碰到了,頭疼啊uj5u.com熱心網友回復:
你這個BaseActivity是哪里來的,我記得以前的unity集成的是UnityPlayerNativeActivity或UnityPlayerActivityuj5u.com熱心網友回復:
哥們 我這邊也遇到了unity內嵌安卓的退出崩潰問題 這里給一下我的解決程序吧 其實就是playersetting里面的旋轉需要設定成自動旋轉自動旋轉會有一些選項給你 如果不設定成自動旋轉 內嵌unity運行程式退出的時候就會閃退或卡死 我打包使用的是IL2CPP 不過使用MONO還是IL2CPP關系應該不大
uj5u.com熱心網友回復:
你看一下你onDestory,是不是位置不對轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/105468.html
標籤:Android
上一篇:安卓開發按鍵不見了
下一篇:Android Studio
