일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- node.js
- 벡터와 리스트의 차이
- react native
- react
- 스탠실 버퍼 사용
- C++
- 싸이월드
- Expo
- node
- javascript
- react native typescript navigate
- react native 타입스크립트
- GitHub
- react native typescript navigation
- c++ 정보은닉
- c++ using
- 리액트 네이티브 설치 오류
- react native accessible
- react-native
- 스탠실 버퍼 튜토리얼
- cyworld
- CSS
- stencil buffer
- unity stencil buffer
- react native ios 기기 연결
- react native typescript
- 스탠실 버퍼 시작
- html
- 리액트 네이티브 맥
- react native mac
Archives
- Today
- Total
혀니의 이거저거 뿌시기
싱글톤 패턴 멋대로 해석 - 인프런 유니티 강의 본문
728x90
싱글톤이란?
인스턴스를 하나로 둬서 그것을 다른 클래스에서 참조하도록 해주는 것!
인스턴스와 오브젝트의 차이점?
둘 다 어떤 하나의 객체를 말하긴 하지만
오브젝트: 실체
인스턴스: 원본과의 관계에 집중 (어떤 원본에서 나눠진 개별의 객체)
싱글톤 만드는 이유?

gameObject에서 가장 핵심적인 것들을 다루는 Manager라는 component를 만들고 이를 다른 class들이 쉽게 접근 가능하도록 도와주는 애인 것 같다.
if Managers.cs의 코드가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Managers : MonoBehaviour
{
static Managers s_instance; //고정한다는 의미의 static을 붙여 Managers class의 instance만듦
public static Managers Instance{ get { init(); return s_instance;} } //다른 모든 class들이 공통으로 사용 가능하도록
//public으로 init()함수를 실행하고 인스턴스를 return하는 기능하는 애를 만듦
// Start is called before the first frame update
void Start()
{
init(); //Managers는 시작 시 init()을 실행시킴
}
// Update is called once per frame
void Update()
{
}
static void init()
{
if(s_instance == null) //if @Managers가 scene의 없을때를 방지해
{
GameObject go = GameObject.Find("@Managers");
if(go == null)
{
go = new GameObject { }; //없으면 새로 만들고
go.AddComponent<Managers>(); //Managers.cs라는 component를 집어넣는다.
}
DontDestroyOnLoad(go); //scene이 바뀌어도 사라지지 않게 하는 DontDestroyOnLoad
s_instance = go.GetComponent<Managers>(); //s_instance라는 애는 여기서 Managers라는 component를 호출하는 기능을 함.
}
}
}
일 때, 이 Managers를 상속받는 Player.cs의 코드는
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Managers mg = Managers.Instance; //Managers의 Instance기능을 사용할 수 있게 됨
}
// Update is called once per frame
void Update()
{
}
}
이런 식으로 써져야 할 것이다.
이렇게 하면 다른 외부 코드에서 쉽게 Managers의 Instance를 사용해서 그 script내의 기능들을 자유롭게 사용할 수 있게 되는 장점이 있다.
코드를 효율적으로 관리, 사용하기 좋으니 꼭 기억하자.
앞으로의 강의에서 이 기능을 더욱 적극 활용할 것 같다.
자세한 코드에 대한 설명은 주석을 참조하자!
728x90
'GAME ENGINE > Unity' 카테고리의 다른 글
유니티 경고 windowsvideomedia error unhandled color standard: 0 falling back to default this may result in rendering issues (0) | 2023.10.12 |
---|---|
Managers에 InputManager 집어넣어 event class 관리하기 (0) | 2022.09.04 |
unity stencil buffer 적용 방법 (0) | 2022.05.13 |
text mesh pro 사용법 (0) | 2022.01.19 |