如何查看我正在使用的场景?

时间:2018-03-15 00:50:17

标签: c# unity3d

我在一个画布上有所有菜单。如何查看我现在使用的场景?我尝试这段代码,但它总是显示场景的索引是0。

 Scene currentScene = SceneManager.GetActiveScene ();

 string sceneName = currentScene.name;

 int buildIndex = currentScene.buildIndex;

2 个答案:

答案 0 :(得分:-1)

要检查当前打开的场景,您可以使用Unity提供的SceneManagement命名空间。使用SceneManager类,您可以检索游戏中当前活动的场景并将其存储到临时变量中,然后您可以使用该变量检查条件中的名称或构建索引。 如果你的活动场景是" 0"它的回归" 0"当然。例如:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;

 public class ExampleClass : MonoBehaviour
 {
     void Start ()
     {
         // Create a temporary reference to the current scene.
         Scene currentScene = SceneManager.GetActiveScene ();

         // Retrieve the name of this scene.
         string sceneName = currentScene.name;

         if (sceneName == "Example 1") 
         {
             // Do something...
         }
         else if (sceneName == "Example 2")
         {
             // Do something...
         }

         // Retrieve the index of the scene in the project's build settings.
         int buildIndex = currentScene.buildIndex;

         // Check the scene name as a conditional.
         switch (buildIndex)
         {
         case 0:
             // Do something...
             break;
         case 1:
             // Do something...
             break;
         }
     }
 }

以下是文档:https://docs.unity3d.com/540/Documentation/ScriptReference/SceneManagement.SceneManager.GetActiveScene.html

答案 1 :(得分:-1)

我可以查看您当前使用的活动场景。基于Unity Docs

using UnityEngine;
using UnityEngine.SceneManagement;

public class GetActiveSceneExample : MonoBehaviour
{
    void Start()
    {
        Scene scene = SceneManager.GetActiveScene();
        Debug.Log("Active scene is '" + scene.name + "'.");
    }
}

这是确定您使用哪个场景的最简单方法。

相关问题