3

I have integrated Unity in an Android activity (similar to this). I now need to dynamically load a scene depending on user input.

I've tried replicating how Unity loads a scene, in the only way that I know an Android Activity is able to communicate with the UnityPlayer, but obviously doesn't work:

UnityPlayer.UnitySendMessage("Application", "LoadLevel", "9");

I know there are other ways of loading a scene in Unity, but I don't know how to call those from Android itself (also consider that UnitySendMessage only allows one parameter).

Community
  • 1
  • 1
RominaV
  • 3,335
  • 1
  • 29
  • 59

1 Answers1

2

If you plan to load Unity activity from Android, you should create an empty scene. Call this this Main Menu, then make it the default scene that loads through the Build Settings. Make it index 0.

The goal of this empty scene is to load specific scene when script attached to it is called.

In this Main Menu scene, create a GameObject called SceneLoader then attach the script below to it:

public class SceneLoader : MonoBehaviour
{
    public void loadScene(string sceneIndex)
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene(Convert.ToInt16(sceneIndex));
    }
}

You should also create a GameObject called SceneLoader in every other scene and attach the script above to all of them.

Now, load Unity Activity which automatically loads the default/empty scene. Since there is only one GameObject/SceneLoader in it, it will load very fast.

You can now load other scenes from Java with:

UnityPlayer.UnitySendMessage("SceneLoader", "loadScene", "9");
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks! I'm doing something similar to this, but in a thread about Unity performance in Android, they said something about avoiding creating scenes as much as possible because it may make the loading time even worse. Do you know if having a simple, empty scene like this would be detrimental for performance in Android or not really? – RominaV Dec 05 '16 at 21:25
  • There is absolutely nothing wrong with one empty scene. That shouldn't affect loading performance the way you described it since there is only one simple script attached to it and that script does not reference other components/prefabs or such. – Programmer Dec 05 '16 at 22:55