티스토리 뷰

NetworkRoomPlayer를 상속받는 클래스를 만든 후 Start를 사용하려고 하였다.

당연히 Override를 하면 되겠거니 했는데 virtual 단에서 코드가 꽤 작성되어 있었다.

        public virtual void Start()
        {
            if (NetworkManager.singleton is NetworkRoomManager room)
            {
                // NetworkRoomPlayer object must be set to DontDestroyOnLoad along with NetworkRoomManager
                // in server and all clients, otherwise it will be respawned in the game scene which would
                // have undesirable effects.
                if (room.dontDestroyOnLoad)
                    DontDestroyOnLoad(gameObject);

                room.roomSlots.Add(this);

                if (NetworkServer.active)
                    room.RecalculateRoomPlayerIndices();

                if (NetworkClient.active)
                    room.CallOnClientEnterRoom();
            }
            else Debug.LogError("RoomPlayer could not find a NetworkRoomManager. The RoomPlayer requires a NetworkRoomManager object to function. Make sure that there is one in the scene.");
        }

 

 

Base를 사용하는건가? 했는데 주석이 있었다.

/// <summary> /// Do not use Start - Override OnStartHost / OnStartClient instead! /// </summary>

 

Start 쓰지 말라고...

대신 OnStartHost / OnStartClient를 사용하라고 한다.

 

이하의 추상 메서드들은 전부 NetworkBehaviour 에 있다.

        /// <summary>Like Start(), but only called on server and host.</summary>
        public virtual void OnStartServer() {}

        /// <summary>Stop event, only called on server and host.</summary>
        public virtual void OnStopServer() {}

        /// <summary>Like Start(), but only called on client and host.</summary>
        public virtual void OnStartClient() {}

        /// <summary>Stop event, only called on client and host.</summary>
        public virtual void OnStopClient() {}

        /// <summary>Like Start(), but only called on client and host for the local player object.</summary>
        public virtual void OnStartLocalPlayer() {}

        /// <summary>Stop event, but only called on client and host for the local player object.</summary>
        public virtual void OnStopLocalPlayer() {}

        /// <summary>Like Start(), but only called for objects the client has authority over.</summary>
        public virtual void OnStartAuthority() {}

        /// <summary>Stop event, only called for objects the client has authority over.</summary>
        public virtual void OnStopAuthority() {}

 

(영어를 잘 못하는 내가 봐도 해석이 가능할 정도로 쉬운 주석이다)

결국 NetworkRoomPlayer 는 NetworkBehaviour를

상기한 추상메서드를 Override 하여 사용할 수 있다.

 

그럼 NetworkRoomPlayer는 OnStartClient를 어떻게 Start 대신 사용하는걸까?

 

그건 바로 NetworkIdentity에서 실행하는 것이다.

        internal void OnStartClient()
        {
            if (clientStarted) return;

            clientStarted = true;

            // Debug.Log($"OnStartClient {gameObject} netId:{netId}");
            foreach (NetworkBehaviour comp in NetworkBehaviours)
            {
                // an exception in OnStartClient should be caught, so that one
                // component's exception doesn't stop all other components from
                // being initialized
                // => this is what Unity does for Start() etc. too.
                //    one exception doesn't stop all the other Start() calls!
                try
                {
                    // user implemented startup
                    comp.OnStartClient();
                }
                catch (Exception e)
                {
                    Debug.LogException(e, comp);
                }
            }
        }

 

NetworkIdentity 내부에도 동명의 OnStartClient 메서드가 있다.

이 메서드에서 연결된 모든 NetworkBehaviour를 상속받는 객체의 OnStartClient를

 

결국 또 이 NetworkBehaviours는 언제 어떻게 생겨난 건지 의문이 생겼다!

 

... 그런데 참조가 31개나 되어서 그냥 포기하고 빠르게 결론을 내리기로 했다.

 

NetworkRoomPlayer를 사용할 때, 이 객체가 생성되는 순간 어떤 초기화를 하고 싶을 수 있다.

이때 Start를 사용하는 것이 아니라 OnStartHost, OnStartClient를 상황에 맞게 사용하면 된다.

예를 들면, 생성과 동시에 유저 데이터를 받아 객체에 적용하고 싶은 순간 말이다.

 

결국 NetworkManager에서 StartClient를 하면 여러 메서드를 거치고 거쳐서 OnStartClient가 실행되는 모양이었다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
글 보관함