본문 바로가기
소프트웨어 개발/유니티(Unity)

[유니티] DontDestroyOnLoad 싱글톤 패턴 사용법

by Sakriun 2021. 6. 22.
728x90
using UnityEngine;

public class StartManager : MonoBehaviour
{
    public static StartManager instance = null;
    
    void Awake()
    {
    	// SoundManager 인스턴스가 이미 있는지 확인, 이 상태로 설정
        if (instance == null) 
            instance = this; 
         
        // 인스턴스가 이미 있는 경우 오브젝트 제거
        else if (instance != this) 
            Destroy(gameObject);
            
        // 이렇게 하면 다음 scene으로 넘어가도 오브젝트가 사라지지 않습니다.
        DontDestroyOnLoad(gameObject); 
    }
}

 

728x90
반응형

댓글