PUN Classic (v1), PUN 2, Bolt는 휴업 모드입니다. Unity2022에 대해서는 PUN 2에서 서포트하지만, 신기능의 추가는 없습니다. 현재 이용중인 고객님의 PUN 및 Bolt 프로젝트는 중단되지 않고, 퍼포먼스나 성능이 떨어지는 일도 없습니다. 앞으로의 새로운 프로젝트에는 Photon Fusion 또는 Quantum을 사용해 주십시오.

간단한 서버에서 제어되는 채팅

게임내 채팅을 서버에서 제어되기를 원할 때도 있습니다. 예를들면, 불량한 단어의 사용을 제어하고, 스팸을 방지하거나 둘 모두를 원할때 입니다.

제가 참여하고 있는 게임에서 아주 간단한 서버에서 제어되는 채팅을 만들었습니다. 원하는 것이 아닐수도 있으나, 여기에서 무엇인가를 얻었으면 합니다.

PlayerChatController

플레이어에 플레이어로부터의 입력을 다루는 PlayerChatController 를 붙이고 서버에 전송합니다. GUI에 대해서 DF-GUI*를 사용하고 있어 dfTextbox를 참조하지만 어떤 GUI-시스템을 사용하던지에 관계없이 원리는 동일합니다.

* DF-GUI는 더이상 유지관리가 되지 않으니, GUI 솔루션을 사용하는 것을 권고합니다.

C#

public class PlayerChatController : Bolt.EntityEventListener<IPlayerState> {

    dfTextbox chatInputTextbox;

    void Start () {
        chatInputTextbox = PlayerUI.instance.chatInputTextbox; // PlayerUI is a local, non-networked, singleton
    }

    void Update () {
        // The chat input box is triggered when the player presses "T"
        if ( Input.GetKey(KeyCode.T) && !chatInputTextbox.IsVisible ) {
            chatInputTextbox.MaxLength = 140; // Bolt allows strings of max 140 chars length
            chatInputTextbox.Text = "";
            chatInputTextbox.IsVisible = true;
            chatInputTextbox.Focus();
        }

        // Things that can only happen if the chat input textbox is visible
        if ( chatInputTextbox.IsVisible ) {
            // Send message when player presses Enter/Return
            if ( Input.GetKeyDown(KeyCode.Return) ) {
                chatInputTextbox.IsVisible = false;
                chatInputTextbox.Trim(); // Removes spaces at the beginning and end; we don't want strings filled with spaces to bother us

                // Send the chat message to the server only for "quality check"
                if ( chatInputTextbox.Text.Length > 0 ) {
                    using ( var evnt = SendChatMsgToServer.Create(Bolt.GlobalTargets.OnlyServer) ) {
                        evnt.Sender = entity;
                        evnt.Message = chatInputTextbox.Text;
                    }
                }
            }

            // Abort (i.e. if pressing the ESC key)
            if ( Input.GetKeyDown(KeyCode.Escape) ) {
                chatInputTextbox.IsVisible = false;
            }
        }
    }

}

UI_ChatOutputController

이 컨트롤러는 플레이어의 사용자 인터페이스에 붙여지는 것으로 채팅 메시지를 다루며, 메시지를 스크롤하고 오래된 것은 삭제등을 해 줍니다. 채팅 이벤트도 다루고 있습니다.

C#

public class UI_ChatOutputController : Bolt.GlobalEventListener {

    public override void OnEvent ( SendChatMsgToServer evnt  ) {
         bool okToSend = true;

        // This is the place where you do all the checks to see if it's OK to forward the message to all the clients.
        // Then, if it's OK...

        if ( okToSend ) {
            // Send the message to everyone, including the server
            using ( var clientEvnt = SendChatMsgToClients(Bolt.GlobalTargets.Everyone) ) {
                clientEvnt.Message = evnt.Message;
            }
        }
    }

    public override void OnEvent ( SendChatMsgToClients evnt ) {
        // This is where you receive the chat message from the server, adds it to your chat messages list/array and refreshes your GUI.
    }

}

동일한 작업에 두개의 이벤트?

채팅 메시지를 보내는 동일한 두개의 이벤트가 있다는 것을 알아채셨을 것 입니다. 클라이언트가 서버에게만 이벤트를 전송(위에 있는 코드와 같이)하고 나서 모든 클라이언트들에게(또는 위에있는 코드와 같이 서버를 포함한 모두에게) 동일한 이벤트를 전송하는 하나의 이벤트만 사용할 수 있습니다.

편하신 것을 골라서 사용하시면 됩니다.

Back to top