본문 바로가기

카테고리 없음

[유니티 게임 개발] tps게임 공부

Stevie ROF의 3인칭 슈터 튜토리얼을 보고 공부하고 있다.

공부 중 기억할만한 것을 여기에 남겨놓을 예정이다.

 

<프로퍼티>

private static GameManager m_Instance;
    public static GameManager Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = new GameManager();
                m_Instance.gameObject = new GameObject("_gameManager");
                m_Instance.gameObject.AddComponent<InputController>();
            }
            return m_Instance;
        }
    }

다른 클래스에서 따로 개체를 생성하지 않고 불러올 수 있으며,  프로퍼티로 본래 변수는 지키면서 조건을 건다,

여기선 다른 곳에서 게임 매니저를 호출 시, 개체가 생성되어 있지 않으면 개체를 하나 생성한다.

 

//---GameManger.cs
public event System.Action<Player> OnLocalPlayerJoined;
...
private Player m_LocalPlayer;
    public Player LocalPlayer
    { 
        get
        {
            return m_LocalPlayer;
        }
        set
        {
            m_LocalPlayer = value;
            if (OnLocalPlayerJoined != null)
                OnLocalPlayerJoined(m_LocalPlayer);
        }
    }
    ...
    //---ThridPersonCamera.cs
    ...
    public Player localPlayer;
    private void Awake()
    {
        GameManager.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;;
    }

    void HandleOnLocalPlayerJoined (Player player)
    {
        localPlayer = player;
    }

이건 따라하면서 놀랐는데, 일단 대리자를 이용해, 유니티상에 번거러운 조작없이 자동으로 대상을 플레이어로 지정하는 기능인데...그것도 매니저에서 한다는 것이다.

 

event에서 Action은 대리자 형식의 일종으로 매개변수 1개를 받는 대리자라는 뜻이다.

Action<Player>니깐 Player매개변수 하나를 받는 어떤 함수든 집어넣을 수 있다.