๐ŸŽฎ Unity Study/Unity

Project Manager ์‹ฑ๊ธ€ํ†ค ์ œ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

ibelieveinme 2024. 7. 2. 17:28
728x90

 

public class DataManager : MonoBehaviour
{
    private static DataManager _instance;
    public static DataManager Instance
    {
        get
        {
            if (_instance == null)
            {
                Debug.Log(_instance == null);
                _instance = FindObjectOfType<DataManager>();
                DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }
    }

    private void Awake()
    {
        if (_instance != this)
        {
            Destroy(gameObject);
        }
    }
}
public class DataManager
{
    private static DataManager _instance;
    public DataManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new DataManager();
                DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }
    }
    private void Awake()
    {
        if (_instance != this)
        {
            Destroy(gameObject);
        }
    }
}

 

์œ„, ์•„๋ž˜ ์ฝ”๋“œ์˜ ์ฐจ์ด๋Š” 1) MonoBehaviour ์˜ ์œ ๋ฌด. 2) _instance ์ƒ์„ฑ ์ž…๋‹ˆ๋‹ค.

 

 

1) MonoBehaviour ์˜ ์œ ๋ฌด

 

์šฐ์„ , MonoBehaviour ํด๋ž˜์Šค๋Š” Editor ์—์„œ 1) GameObject ์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ๊ณตํ•˜๊ณ  Start, Update ๋“ฑ์˜ Unity 2) GameObject ์ƒ๋ช…์ฃผ๊ธฐ Event ํ•จ์ˆ˜๋“ค๊ณผ 3) Coroutine ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, MonoBehaviour ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์œ„ ์ฝ”๋“œ๋Š” ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

https://docs.unity3d.com/kr/2023.2/ScriptReference/MonoBehaviour.html

 

MonoBehaviour - Unity ์Šคํฌ๋ฆฝํŒ… API

MonoBehaviour is a base class that many Unity scripts derive from.

docs.unity3d.com

 

์ฐธ๊ณ ๋กœ Unity์—์„œ MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ GameObject์— ๋ถ™์˜€์„ ๋•Œ, ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ์ดˆ๊ธฐํ™” ๊ณผ์ • ๋ฐ ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. ์ƒ์„ฑ์ž (Constructor)
  2. Awake()
  3. OnEnable()
  4. Start()

 

  1. ์ƒ์„ฑ์ž:
    • ์Šคํฌ๋ฆฝํŠธ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • Unity์—์„œ๋Š” ์ƒ์„ฑ์ž ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  Awake() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
  2. Awake():
    • ์Šคํฌ๋ฆฝํŠธ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๋น„ํ™œ์„ฑํ™” ์ƒํƒœ์—ฌ๋„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ์˜ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ๊ฒŒ์ž„ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ๋ชจ๋“  Awake() ํ˜ธ์ถœ์ด ์™„๋ฃŒ๋œ ํ›„ Start() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  3. OnEnable():
    • ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ™œ์„ฑํ™”๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ํ™œ์„ฑํ™”๋  ๋•Œ๋„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ์ฝ”๋ฃจํ‹ด์„ ์‹œ์ž‘ํ•˜๊ธฐ์— ์ ํ•ฉํ•œ ์œ„์น˜์ž…๋‹ˆ๋‹ค.
  4. Start():
    • ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ™œ์„ฑํ™”๋œ ํ›„ ์ฒซ ํ”„๋ ˆ์ž„ ์—…๋ฐ์ดํŠธ ์ง์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ๋ชจ๋“  Awake() ํ˜ธ์ถœ์ด ์™„๋ฃŒ๋œ ํ›„์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ์ดˆ๊ธฐํ™”๊ฐ€ ์™„๋ฃŒ๋œ ํ›„ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ดˆ๊ธฐํ™”์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ด ์ˆœ์„œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ ์ ˆํžˆ ํ™œ์šฉํ•˜๋ฉด ์Šคํฌ๋ฆฝํŠธ ๊ฐ„์˜ ์˜์กด์„ฑ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ํšจ๊ณผ์ ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

2) _instance ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ

 

์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ(MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค)์ฒ˜๋Ÿผ MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋ฐ˜๋“œ์‹œ Unity์˜ ๋ฐฉ์‹(์˜ˆ: AddComponent, Instantiate ๋“ฑ)์œผ๋กœ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Singleton ํŒจํ„ด์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ์ฃผ๋กœ FindObjectOfType์ด๋‚˜ ์”ฌ์— ๋ฏธ๋ฆฌ ๋ฐฐ์น˜๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.

 

๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ์ฒ˜๋Ÿผ MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ์ผ๋ฐ˜ C# ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ์—๋Š” new ํ‚ค์›Œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ Unity์˜ ์ปดํฌ๋„ŒํŠธ ์‹œ์Šคํ…œ๊ณผ ์—ฐ๋™๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ, GameObject๋‚˜ ์”ฌ๊ณผ ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, ์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ์—์„œ _instance = FindObjectOfType<DataManager>(); ๊ฐ€ ์•„๋‹Œ _instance = new DataManager();๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  1. Unity ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฌด์‹œ: MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋Š” Unity์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์— ๋”ฐ๋ผ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. new ํ‚ค์›Œ๋“œ๋กœ ์ง์ ‘ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด Unity ์—”์ง„์ด ๊ด€๋ฆฌํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด Awake(), Start() ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  2. ์ปดํฌ๋„ŒํŠธ๋กœ์„œ์˜ ๊ธฐ๋Šฅ ์ƒ์‹ค: new๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ์”ฌ์˜ GameObject์— ๋ถ€์ฐฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Transform ๋“ฑ GameObject์™€ ๊ด€๋ จ๋œ ์ปดํฌ๋„ŒํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. DontDestroyOnLoad ๋ฌธ์ œ: DontDestroyOnLoad()๋Š” GameObject์— ๋Œ€ํ•ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. new๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” GameObject๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  4. FindObjectOfType ๋ถˆ์ผ์น˜: FindObjectOfType<DataManager>()๋Š” ์”ฌ์— ์กด์žฌํ•˜๋Š” DataManager ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐพ์ง€๋งŒ, new๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ์”ฌ์— ์กด์žฌํ•˜์ง€ ์•Š์•„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  5. ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ์ด์Šˆ: Unity์˜ ๊ฐ์ฒด ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ๋ฒ—์–ด๋‚˜๋ฏ€๋กœ, ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  6. Destroy ๋ฉ”์„œ๋“œ ์ž‘๋™ ๋ถˆ๊ฐ€: Destroy(gameObject)๋Š” Unity ์—”์ง„์ด ๊ด€๋ฆฌํ•˜๋Š” GameObject์— ๋Œ€ํ•ด์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. new๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์—๋Š” ์ ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

728x90