Other
Solution was found during moderation process, though it was an alternative to the requested solution. But, if it works it works i suppose.
Android, Vuforia and mUnityPlayer.exit()
After porting a basic Vuforia application over to Android, and allowing me to change to it as a new activity i started working on embedding it of sorts into an activity. Here i found the problem that turning the phone would call onDestroy, and also the unityplayers mUnityPlayer.quit().
In an attempt to work around it, i tried to put it in its own fragment, allowing the fragment to be closed and recreated, but to no avail. Seems like mUnityPlayer.quit() closes the application completely.
Is there a good way to prevent the unityplayer from closing the application whenever i change orientation without using android:config or locking the orientation of the application?
Edit: By dropping fragments and putting it all into the mainActivity it gave the same effect as i'd hope. Solution was found here: http://stackoverflow.com/questions/23467994/errors-managing-the-unityplayer-lifecycle-in-a-native-android-application?noredirect=1&lq=1
Still, shame you can't make it work with a fragment, meaning it has to be the main part of the activity.
MainActivity:
private Button startUnityActivity;
private Button startUnityEmbedded;
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
if (savedInstanceState == null)
{
Fragment newFragment = new UnityFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.UnityFragment, newFragment).commit();
}
}
UnityFragment:
public class UnityFragment extends Fragment
{
protected UnityPlayer mUnityPlayer;
private FrameLayout unityFrame;
public UnityFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.unityfragment, container, false);
mUnityPlayer = new UnityPlayer(getActivity());
this.unityFrame = (FrameLayout) view.findViewById(R.id.UnityFragment);
this.unityFrame.addView(mUnityPlayer.getView(), FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
mUnityPlayer.requestFocus();
// mUnityPlayer.windowFocusChanged(true); // Fix for blackscreen unity player.
return view;
}
// Quit Unity
@Override
public void onDestroy() {
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
@Override
public void onPause() {
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
@Override
public void onResume() {
super.onResume();
mUnityPlayer.resume();
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:id="@+id/MainLayout">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/UnityEmbedded"
android:layout_above="@+id/FirstButton"
android:background="@android:color/darker_gray"
android:name="com.testing.VuforiaImageTracking.UnityFragment"
tools:layout="@layout/unityfragment">
</fragment>
<Button
android:text="Start Unity Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/FirstButton"
android:layout_above="@+id/SecondButton"/>
<Button
android:text="Start Unity Embedded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/SecondButton"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>