반응형
유니티 C# 스크립트 싱글톤
public class Singleton
{
private static Singleton instance;
private Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
instance = new Singleton();
return instance;
}
}
}
MonoBehaviour를 상속받지 않아서 오브젝트 컴포넌트에 추가하지 않고 사용합니다
using UnityEngine;
public class Singleton
{
private static Singleton instance;
private Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
{
Debug.Log("싱글톤 생성");
instance = new Singleton();
}
return instance;
}
}
public void Load()
{
Debug.Log("Load");
}
}
제대로 작동하는지 확인하기 위해 메서드를 작성해줍니다
Singleton.Instance.Load();
3번 실행한 결과
반응형
'Unity > Tip' 카테고리의 다른 글
[Unity 2020.3 LTS] TextMeshPro 한글 네모로 출력 해결 (0) | 2022.01.20 |
---|---|
[Unity 2020] UGUI Button 클릭 이벤트 스크립트 (4) | 2021.11.01 |
[Unity 2021] 기본 프로젝트 HDRP 설정하기 (1) | 2021.02.09 |
[Unity] 픽셀 깨짐 없는 orthographic size 구하기 (0) | 2016.02.16 |
[Unity] SetActive를 사용해 오브젝트(GameObject) 활성화, 비활성화 시켜주기 (0) | 2015.10.19 |