유니티

[유니티] 싱글톤만들기

SUGI_ 2023. 8. 26. 18:03
public class HttpManager : MonoBehaviour
{
    //public 삭제
    static HttpManager instance;

    //HttpManager return 하는 함수
    //1.
    public static HttpManager Get()
    {
        if(instance == null)
        {
            //게임오브젝트 생성 new
            GameObject go = new GameObject("HttpStudy");
            //만들어진 게임오브젝트에 HttpManager 컴포넌트 붙이자
            go.AddComponent<HttpManager>();
        }

        return instance;
    }

    //2.
    private void Awake()
    {
        if(instance == null)
        {
            instance = this;
            //없으면 붙겠끔 하는 코드도 넣어주기
            DontDestroyOnLoad(gameObject);
        }

        else
        {
            Destroy(gameObject);
        }
    }
728x90