본문 바로가기

게임프로그래밍

[MMO Lab 1기] 매니저(Managers)

** 컨포넌트를 이용해서 기능을 세분화하여 작업함

 

   컨포넌트는 자신만의 기능을 세분화여 들고 있지만,

   범용적으로 항상 사용하는 기능도 컨포넌트로 구현할 것인가?

   그렇다면 그 내용들을 어떻게 공유하고 구현할 것이며 호출할 것인가?

 

   => 그에 대한 해답 중 하나가 매니저이다.

 

 

** 매니저(Managers)

 : 만능형으로, 모든 기능을 종합적으로 관리하는 역할

   이 기능의 핵심은 우리가 작업하는 코드 내에서 언제 언디서든 사용 가능하다는 점

   ㄴ 여러 개의 컨포넌트들이 따로 떨어져 있을 때, 교류하여 작업하는 방식  

 

▼ 매니저

using UnityEngine;

public class Managers : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {    
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void PlaySound()
    {
        // TODO
        Debug.Log("PlaySound");
    }

    public void ShowUI()
    {
        // TODO
        Debug.Log("ShowUI");
    }
}

 

▼ 매니저를 사용하는 컨포넌트

using UnityEngine;

public class Player : MonoBehaviour
{
    // 1)
    //public GameObject _managers;

    // 2)
    //public Managers _managers;

    // 4)
    Managers _managers;

    // Start is called before the first frame update
    void Start()
    {
        // 1)
        //Managers managers = _managers.GetComponent<Managers>();
        //managers.PlaySound();

        // 2)
        //_managers.PlaySound();

        // 3)
        //GameObject go = GameObject.Find("@Managers");
        //Managers _managers = go.GetComponent<Managers>();
        //_managers.PlaySound();

        // 4)
        GameObject go = GameObject.Find("@Managers");
        _managers = go.GetComponent<Managers>();
    }

    // Update is called once per frame
    void Update()
    {
        _managers.PlaySound();
    }
}

 

 

** 참고 블로그

https://velog.io/@ddongg00/UnityGame-Manager%EB%A7%8C%EB%93%A4%EA%B8%B0

 

[Unity Manager]Managers 만들기

게임을 만들때 전역변수로 사용하며 UI, 네트워크, 사운드, Scene관리 등의 기능을 넣어줄 game manager가 필요하다.game manager는 scene에 한개만 존재해야 하므로 singleton pattern을 이용해 구현해줘야 한

velog.io

 

https://velog.io/@scarleter99/Unity-1-1.-%EC%9C%A0%EB%8B%88%ED%8B%B0-%EC%97%94%EC%A7%84-Managers

 

[Unity] 1-1. 유니티 엔진 : Managers

전역으로 게임 전체와 다른 Manager를 관리하는 Manager이다.Singleton 패턴을 적용한다.Init()@GameManger라는 이름을 가진 GameObject에만 GameManager가 존재할 수 있다.DontDestroyOnLoad();Scene 전환 시

velog.io