【问题标题】:Multiple Unity Scene Script for iOS Build用于 iOS 构建的多个 Unity 场景脚本
【发布时间】:2019-01-08 06:03:53
【问题描述】:

Unity 中的两个场景。必须在本机 iOS 应用程序中编写用于控制统一场景(2)的脚本,假设当我单击第一个按钮时本机应用程序中的 2 个按钮必须显示场景 1 和场景 2 的第二个按钮,就像这样。我找到了一个适用于 android 的脚本,但需要为 iOS 编写它。 Android的附加脚本,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.VR;
using System;

public class AnimatorScript : MonoBehaviour 
{
    public Animator animation;
    AndroidJavaObject intent , currentActivity , extras;
    bool hasExtra;
    string arguments;

    // Use this for initialization
    void Start ()
    {
        try
        {

        AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
        AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        intent = currentActivity.Call<AndroidJavaObject>("getIntent");
        hasExtra = intent.Call<bool> ("hasExtra", "test_val");
         Debug.Log(hasExtra);
        Debug.Log("start");
        }
        catch(Exception e)
        {
            Debug.Log(e);
        }

    }

    public void loadScene(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }

    // Update is called once per frame
    void Update () 
    {

        if (hasExtra) 
        {
            Debug.Log("has extra");
            extras = intent.Call<AndroidJavaObject>("getExtras");
            arguments = extras.Call<string> ("getString", "test_val");
            if(string.Equals(arguments,"scene1"))
             {
                 animation.Play("dancing_warrior_sun-001");
             }
             if(string.Equals(arguments,"scene2"))
             {
                //  animation.Play("seating_poses-001");
                 SceneManager.LoadScene(1);
             }
             else Debug.Log("No Animation Found");

            Debug.Log(arguments);
        } 
        else 
        {

            Debug.Log("no extra");
        }
    }
}

【问题讨论】:

    标签: javascript c# ios unity3d


    【解决方案1】:

    场景切换/切换有两种方式,

    1) 在Unity中通过UI放置按钮,编写场景切换脚本如下,

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MenuActions : MonoBehaviour {
      public void MENU_ACTION_NextButton(string sceneName)
        {
            Application.LoadLevel(sceneName);
        }
    }
    

    将此脚本拖到主摄像机并单击按钮,在检查器底部单击以添加此主摄像机并命名必须更改的场景名称。使用相同的脚本对场景 2 执行此操作并命名场景。

    2) 如果您的原生应用中有按钮,请编写如下脚本并尝试编写用于 void update 的脚本,

       using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        using System.Runtime.InteropServices;
        using UnityEngine.SceneManagement;
        public class SceneChanger : MonoBehaviour {
    
            // Use this for initialization
            void Start () {
    
            }
    
            // Update is called once per frame
            void Update () {
                //SceneManager.LoadScene(scene);
    // try your script................
            }
    
    
        public void ChangeScene( string scene ) {
           // Application.LoadLevel(scene);
            SceneManager.LoadScene(scene);
        }
        }
    

    希望这会给你这个概念..

    【讨论】:

      【解决方案2】:

      Building an application with a native plugin for iOS

      您可以按照官方文档,使用iOS中的UnitySendMessage调用Unity中的场景控制脚本。

      【讨论】:

      • 我是 Unity 的初学者,不熟悉 c#。我希望获得 2 个场景的完整脚本支持。
      • 我发现一篇文章实现了你想要的。 the-nerd.be/2014/08/07/…
      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多