본문 바로가기
유니티

[유니티] 싱글톤만들기

by SUGI_ 2023. 8. 26.
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

'유니티' 카테고리의 다른 글

[유니티] Scripting Define Symbol 스크립트로 제어하기  (0) 2023.08.29
Firebase  (1) 2023.08.29
HTTP 통신  (0) 2023.08.24
JSON (JavaScript Object Notation)  (1) 2023.08.23
[유니티] 파일저장  (0) 2023.08.23