📜  unity scenemananger - C# (1)

📅  最后修改于: 2023-12-03 15:05:44.886000             🧑  作者: Mango

Unity Scene Manager - C#

Unity Scene Manager is a powerful feature in Unity that allows developers to manage and manipulate different scenes in their game project. This functionality is useful for games that have multiple levels, menus, or different gameplay modes.

With Unity Scene Manager, you can load and unload scenes, switch between scenes, and even add or remove objects between scenes. The Scene Manager can be accessed via C# scripting, which allows game developers to automate these processes and create more dynamic and interactive gameplay experiences for players.

Loading Scenes

The SceneManager.LoadScene() method is used to load a scene in Unity. This method takes the scene name or index as its argument. Here is an example of how to load a new scene:

using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    public void LoadLevel(string levelName)
    {
        SceneManager.LoadScene(levelName);
    }
}

In the example above, we create a new method called LoadLevel(), which takes a string argument representing the name of the scene we want to load. This method then passes the levelName argument to the SceneManager.LoadScene() method to load the specified scene. We can then call this method from a button or other UI element in our game.

Unloading Scenes

The SceneManager.UnloadScene() method is used to unload a scene from Unity. This method takes the scene name or index as its argument. Here is an example of how to unload a scene:

using UnityEngine.SceneManagement;

public class SceneUnloader : MonoBehaviour
{
    public void UnloadLevel(string levelName)
    {
        SceneManager.UnloadSceneAsync(levelName);
    }
}

In the example above, we create a new method called UnloadLevel(), which takes a string argument representing the name of the scene we want to unload. This method then passes the levelName argument to the SceneManager.UnloadSceneAsync() method to unload the specified scene. We can then call this method from a button or other UI element in our game.

Switching Scenes

The SceneManager.LoadScene() method can also be used to switch between scenes. This method takes the scene name or index as its argument. Here is an example of how to switch between scenes:

using UnityEngine.SceneManagement;

public class SceneSwitcher : MonoBehaviour
{
    public void SwitchToLevel(string levelName)
    {
         SceneManager.LoadScene(levelName);
    }
}

In the example above, we create a new method called SwitchToLevel(), which takes a string argument representing the name of the scene we want to switch to. This method then passes the levelName argument to the SceneManager.LoadScene() method to load the specified scene. We can then call this method from a button or other UI element in our game.

Adding and Removing Objects between Scenes

The DontDestroyOnLoad() method can be used to persist objects between scenes. This method can be called on any object that we want to keep alive between scene changes. Here is an example:

using UnityEngine;

public class ObjectManager : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
}

In the example above, we create a new ObjectManager script and attach it to an object in our scene. The Awake() method is called when the script is first loaded, and we use this method to call DontDestroyOnLoad() on the game object. This will ensure that the object persists between scene changes.

To remove an object between scenes, we can simply destroy it using the Destroy() method. Here is an example:

using UnityEngine;

public class ObjectManager : MonoBehaviour
{
    public GameObject objectToDestroy;

    private void OnDestroy()
    {
        Destroy(objectToDestroy);
    }
}

In the example above, we create a new ObjectManager script and attach it to an object in our scene. We also create a public field called objectToDestroy, which is the object that we want to remove between scenes. We then use the OnDestroy() method to call Destroy() on the object, which removes it from the scene.