The application in which I am currently working uses a navigation box, and each tab opens a new fragment in action (replaces the old one).
One of these snippets is the Unity3D script. I basically did this:
- Export your Unity project as an Android app.
- open the action it gave (this is UnityPlayerNativeActivity)
convert this UnityPlayerNativeActivity into a fragment, as shown in the code below. I also changed some minor things in the manifest.
import com.unity3d.player.*; import android.app.Fragment; import android.app.NativeActivity; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; public class UnityPlayerNativeFragment extends Fragment { protected UnityPlayer mUnityPlayer;
The problem here is in the mUnityPlayer.quit () function. As stated here , mUnityPlayer.quit () completely kills the process in which it is running. So basically what happens is that the whole application exits when I switch fragments (i.e., I switch tabs in NavigationDrawer) because it calls onDestroy UnityPlayerNativeFragment.
I do not want it. Ideally, the user will switch to another tab in the NavigationDrawer, and when he returns to the Unity scene tab, this is exactly where he left it. As if they used Android multitasking to change applications and return - nothing changes.
If it was an Activity, you could open it in a different process than the main application by adding the line android:process=":UnityKillsMe" to the manifest file, but this is not possible for the fragment. So my questions are:
- How can I open UnityPlayerNativeFragment in a new process so that the .quit () function does not kill my entire application and that I can restart the fragment when the user leaves the tab and then returns to it?
- Even if I can reach Q1, the state problem remains. Completing and then reloading the Unity scene will recreate it again, starting with the default view and losing any properties that it had (for example, imagine that it was a game, then all data will be lost). So, question 2: how to switch to another tab in the same application, and then return to the Unity tab without changing the scene? (Essentially supporting it in the background without leaving it at all).
Here's how I switch fragments (from MainActivity):
@Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getFragmentManager(); if (position==3) { fragmentManager.beginTransaction() .replace(R.id.container, DirectoryFragment.newInstance(position + 1)) .commit(); } else if (position == 0) { upnf = UnityPlayerNativeFragment.newInstance(position + 1); fragmentManager.beginTransaction() .replace(R.id.container, upnf, "UnityPlayerNativeFragment") .commit(); } else if (position == 2){ fragmentManager.beginTransaction() .replace(R.id.container, WeatherFragment.newInstance(position + 1)) .commit(); } else if (position == 4) { fragmentManager.beginTransaction() .replace(R.id.container, SettingsFragment.newInstance(position + 1)) .commit(); //Log.d("worked pos=4","worked pos=4"); } else{ fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); } }
Jimmy source share