在android Studio项目中,当我切换到另一个也包含UnityPlayer的活动时,UnityPlayer会在subActivity上显示一个BlackScreen。
我在Arcore项目中,我使用Android studio有一个良好的界面和易于实现,我使用最新的Unity版本和Android studio,以及每一个android librairy(Androidx)。我需要在另一个活动上打开Unity视图,但似乎不起作用。在谷歌上做的每一项研究都会让我回答谁不能解决我的问题,或者在我的manifest.xml上添加一个android:进程,但不可能让它工作。
与这两个活动的mUnityPlayer Manifest.xml相关的每个代码:
[...]
<activity
android:name=".MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
<activity
android:name=".ViewerPlaneActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label=""
android:screenOrientation="landscape"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
[...]我应用程序的MainActivity:
public class MainActivity extends AppCompatActivity {
[...]
public UnityPlayer mUnityPlayer;
public static UnityPlayerActivity currentActivity;
private boolean switchToAnAnotherUnityAct=false;
[...]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mUnityPlayer = new UnityPlayer(this);
setContentView(R.layout.viewer_view_simple_viewer);
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.getView().requestFocus();
currentActivity = this;
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
@Override
protected void onDestroy() {
if(!switchToAnAnotherUnityAct)mUnityPlayer.destroy();
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
if(!switchToAnAnotherUnityAct)mUnityPlayer.pause();
}
@Override
protected void onResume() {
super.onResume();
if(!switchToAnAnotherUnityAct){
mUnityPlayer.resume();
replaceUnityPlayerOnFrameLayout();
}
}
@Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
switchToAnAnotherUnityAct=false;
}
@Override
protected void onStop() {
super.onStop();
if(!switchToAnAnotherUnityAct)mUnityPlayer.stop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mUnityPlayer.lowMemory();
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
public void onClickSizeSettings(View view) {
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
switchToAnAnotherUnityAct=true;
Intent intent = new Intent(this, ViewerPlaneActivity.class);
this.startActivity(intent);
}
[...]子活动:
public class ViewerPlaneActivity extends AppCompatActivity {
public UnityPlayer mUnityPlayer;
public static ViewerPlaneActivity currentActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
mUnityPlayer= MainActivity.currentActivity.mUnityPlayer;
setContentView(R.layout.viewer_plane_unit);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
mUnityPlayer.getView().requestFocus();
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
public void onClickAnchorPlane(View view){
[...]
finish();
}
@Override
public void finish() {
super.finish();
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
}
@Override
protected void onPause() {
super.onPause();
mUnityPlayer.pause();
}
@Override
protected void onResume() {
super.onResume();
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.resume();
}
@Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
}结果比我预期的要显示UnityPlayer,但我得到的只是一个黑屏。零错误。
我精确到我没有在我的MainActivity上返回的问题,在那里UnityPlayer工作并正确显示查看器。只有SecondViewer,在那里我得到了一个黑屏,我希望看到的只是显示我的飞机和相机的统一。
谢谢你的帮助。
发布于 2019-04-16 15:44:22
对于finish,我没有找到解决问题的方法,所以我在我的主布局上使用了两个ViewStub。
https://stackoverflow.com/questions/55650442
复制相似问题