public static void DontDestroyOnLoad (Object target);

パラメーター

targetThe object which is not destroyed on scene change.

説明

新しいシーンを読み込んでもオブジェクトが自動で破壊されないように設定します

ExampleScript2:

// Connected to the Cube and includes a DontDestroyOnLoad()
// LoadScene() is called by the first  script and switches to the second.

using UnityEngine; using UnityEngine.SceneManagement;

public class ExampleScript1 : MonoBehaviour { private static bool created = false;

void Awake() { if (!created) { DontDestroyOnLoad(this.gameObject); created = true; Debug.Log("Awake: " + this.gameObject); } }

public void LoadScene() { if (SceneManager.GetActiveScene().name == "scene1") { SceneManager.LoadScene("scene2", LoadSceneMode.Single); } } }
// Connected to the Cube and includes a DontDestroyOnLoad()
// LoadScene() is called by the second script and loads the first.

using UnityEngine; using UnityEngine.SceneManagement;

public class ExampleScript2 : MonoBehaviour { public void LoadScene() { if (SceneManager.GetActiveScene().name == "scene2") { SceneManager.LoadScene("scene1", LoadSceneMode.Single); } } }