简体   繁体   English

在 Android 中将 Unity 作为子视图加载时调用场景

[英]Invoking scene when loading Unity as subview in Android

I have integrated Unity in an Android activity (similar to this ).我已将 Unity 集成到 Android 活动中(与类似)。 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:我尝试复制Unity 如何加载场景,这是我知道 Android Activity 能够与 UnityPlayer 通信的唯一方式,但显然不起作用:

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).我知道在 Unity 中还有其他加载场景的方法,但我不知道如何从 Android 本身调用这些方法(还要考虑UnitySendMessage只允许一个参数)。

If you plan to load Unity activity from Android, you should create an empty scene.如果您计划从 Android 加载 Unity 活动,您应该创建一个场景。 Call this this Main Menu , then make it the default scene that loads through the Build Settings .将此称为主菜单,然后使其成为通过构建设置加载的默认场景。 Make it index 0.使其索引为 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:在这个主菜单场景中,创建一个名为SceneLoader的游戏对象,然后将下面的脚本附加到它:

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.您还应该在每个其他场景中创建一个名为SceneLoader的游戏对象,并将上面的脚本附加到所有场景中。

Now, load Unity Activity which automatically loads the default/empty scene.现在,加载 Unity Activity,它会自动加载默认/空场景。 Since there is only one GameObject/ SceneLoader in it, it will load very fast.由于其中只有一个GameObject / SceneLoader ,因此加载速度非常快。

You can now load other scenes from Java with:您现在可以从 Java 加载其他场景:

UnityPlayer.UnitySendMessage("SceneLoader", "loadScene", "9");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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