I was a member of SO for a while, but didn’t really ask a question, so here it goes.
My goal
I am trying to make an android application with two actions. The first is a menu screen (using standard Android interface elements) with a button that opens the gameplay. The activity of the game will have some standard Android and FrameLayout interface elements containing a 3D Unity scene.
I am using Unity 5 and Eclipse Luna.
What i work
I made a menu screen with a simple button to start the second action. I followed this tutorial and managed to get my Unity scene built into my second activity. Everything is fine so far ...
Problem
The first time I start the game process, it works fine (I can see the Unity scene embedded in my FrameLayout), but if I close the activity (to return to the menu activity) and then start the game process again, I get this error ...
Activity has leaked IntentReceiver com.unity3d.player.UnityPlayer$17@41866de0 that was originally registered here. Are you missing a call to unregisterReceiver()?
What i tried
I searched the Internet many times, and it looks like I need to call something like this in onPause or onDestroy ;
m_UnityPlayer.currentActivity.unregisterReceiver(receiver);
... but I don’t know what the receiver is, so I cannot refer to it in my unregisterReceiver call . I tried to create a BroadcastReceiver by registering it and then unregistering it to Pause, but that doesn't make any difference.
I found a message on the Unity forum that asked the same question, but unfortunately it remains unanswered (many questions on the Unity forum unanswered why I came here with this question).
I also tried using this method in onDestroy , but it actually leaves my application when I close activity in the gameplay.
m_UnityPlayer.quit();
Code example
Here is the code for my gaming activity (I removed the import for clarity);
public class MyEmbeddedUnityActivity extends Activity { private UnityPlayer m_UnityPlayer; IntentFilter filter = new IntentFilter(""); private BroadcastReceiver bRec = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //String action = intent.getAction(); Log.d("", "onReceive intent: " + intent); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_embedded_unity); if(m_UnityPlayer==null){ m_UnityPlayer = new UnityPlayer(this); int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1); m_UnityPlayer.init(glesMode, false); m_UnityPlayer.currentActivity.registerReceiver(bRec, filter); FrameLayout layout = (FrameLayout) findViewById(R.id.my_frame); LayoutParams lp = new LayoutParams(200, 300); layout.addView(m_UnityPlayer, 0, lp); } } public void onWindowFocusChanged(boolean hasFocus){ super.onWindowFocusChanged(hasFocus); m_UnityPlayer.windowFocusChanged(hasFocus); } @Override public void onDestroy (){ //m_UnityPlayer.quit(); m_UnityPlayer.currentActivity.unregisterReceiver(bRec); super.onDestroy(); } @Override public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig); m_UnityPlayer.configurationChanged(newConfig); } @Override protected void onResume() { super.onResume(); m_UnityPlayer.resume(); } @Override protected void onPause() { super.onPause(); m_UnityPlayer.pause(); } }
And finally ...
The steps I took to implement the Unity scene were written in 2011. The whole process (capturing files from the intermediate area of the Unity project and copying files around, etc.) seems really hacky and undocumented ,
Is there a better way to embed Unity scenes in Android actions with Unity 5? Of course, adding a Unity scene to an existing application is pretty common to try and do this?
Thank you, any help would be greatly appreciated!