在Android中将Unity加载为子视图时调用场景 [英] Invoking scene when loading Unity as subview in Android

查看:175
本文介绍了在Android中将Unity加载为子视图时调用场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Unity集成到Android活动中(类似于). 现在,我需要根据用户输入动态加载场景.

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

我尝试了在 Unity如何加载场景中复制我知道Android Activity能够与UnityPlayer通讯的唯一方式,但显然不起作用:

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

我知道有其他方式加载场景 Unity,但我不知道如何从Android本身调用它们(还要考虑UnitySendMessage仅允许一个参数).

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

推荐答案

如果您打算从Android加载Unity活动,则应创建一个 empty 场景.将此称为主菜单,然后将其设置为通过 Build Settings 加载的默认场景.使其索引为0.

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.

在此主菜单场景中,创建一个名为 SceneLoader 的GameObject,然后将以下脚本附加到该对象:

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

您还应该在每个其他场景中创建一个名为 SceneLoader 的GameObject,并将上面的脚本附加到所有这些场景上.

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

现在,加载Unity Activity,它会自动加载默认/空场景.由于其中只有一个GameObject/ SceneLoader ,因此加载速度非常快.

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.

您现在可以使用以下方法从Java加载其他场景:

You can now load other scenes from Java with:

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

这篇关于在Android中将Unity加载为子视图时调用场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆