简体   繁体   中英

Invoking scene when loading Unity as subview in Android

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).

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM