> 예제
using UnityEngine; using UnityEngine.SceneManagement; public class ExampleCode : MonoBehaviour { void Start() { DonDestroyOnLoad(); // Start는 최초 씬에서만 호출된다. } void OnEnable() { // 씬 매니저의 sceneLoaded에 체인을 건다. SceneManager.sceneLoaded += OnSceneLoaded; } // 체인을 걸어서 이 함수는 매 씬마다 호출된다. void OnSceneLoaded(Scene scene, LoadSceneMode mode) { Debug.Log("OnSceneLoaded: " + scene.name); Debug.Log(mode); } void OnDisable() { SceneManager.sceneLoaded -= OnSceneLoaded; } }
> 로비 씬에서 인게임 씬에 데이터 넘겨주기
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System; public class Lobby : MonoBehaviour { public Button btn1; public Button btn2; public int id; // Start is called before the first frame update void Start() { this.btn1.onClick.AddListener(() => { log("1"); SceneManager.sceneLoaded += SceneLoad; // 씬 로드 될 때 실행 SceneManager.LoadScene("Ingame"); }); this.btn2.onClick.AddListener(() => { log("2"); SceneManager.sceneLoaded += SceneLoad; SceneManager.LoadScene("Ingame"); }); } private void SceneLoad(Scene scene, LoadSceneMode mode) { log("[Lobby] 델리게이트"); this.id = 100; GameObject inGameGo = GameObject.Find("Ingame"); var ingame = inGameGo.GetComponent<Ingame>(); ingame.Init(this.id); } // Update is called once per frame void Update() { } public static void log(object data) { Debug.Log(data); } }
'c# 강의 > 유니티' 카테고리의 다른 글
▶ 유니티/싱글톤 - DataManager (0) | 2020.05.12 |
---|---|
▶ 마우스 이벤트 (0) | 2020.05.11 |
20200511 - 현재까지 패키지 구성 (0) | 2020.05.11 |
20200511 - Scene (0) | 2020.05.11 |
20200511 - FindWithTag (자주 쓰이는 예제 필요) (0) | 2020.05.11 |