简体   繁体   中英

Android: Changing Unity Scene from UI components outside the scene

I am trying to build an Android app that uses Unity for UI rendering where the user can perform some actions from outside the scene.

What this means is that say I have an activity where I have a unity scene as a subview. The rest of the screen is composed of native android views (buttons etc). I want to use these views to make changes to the unity scene such as to show a text field, add text, hide text, add text effects etc.

So far I have added the unity scene as a subview by following this answer and it works fine. I am new to Unity and I am not sure how I can manage and change the scene from views outside the scene. Is this even possible? I know if I add buttons to the scene itself when I create the scene in Unity then I can use those buttons to alter the scene.

But is this possible by UI components that are not a part of the scene? If yes how can I do this? I have tried to find info on this but couldn't find much. Thanks for reading.

  • Make communicator between Unity side and Android. It's possible to do that technically, if you make following function in unity. I've just wrote here how unity and android communicate so before use this code you need to see brief of integration between Unity and Android to use native plugin from Unity so see this link to do that.

  • https://docs.unity3d.com/Manual/PluginsForAndroid.html

After you understand that how to make plugin for android, then see codes. It will help you.

  • GameObject Name: LoadGameSceneObject, loadScene.cs
    public class loadScene : MonoBehaviour
    {
        public static void LoadScene(string sceneName)
        {
            SceneManager.LoadScene(sceneName);
        }
    }


  • SceneLoadActivie.java
    public static void SendMessageToUnity(String gameObjectName, String methodName, String message) throws Exception
    {
        Log.d("unity", gameObjectName + " " + methodName + " " + message);
        final Class<?> player = Class.forName("com.unity3d.player.UnityPlayer");
        player.getMethod("UnitySendMessage", String.class, String.class,
                         String.class).invoke(null, gameObjectName, methodName, message);
    }

    ...
    // call this function.
    public void LoadScene()
    {
        SceneLoadActivie.SendMessageToUnity("LoadGameSceneObject", "LoadScene", "SomeScene");
    }

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