PhotonNetworkGUI.cs 소스입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using System.Text.RegularExpressions;
public class PhotonNetworkGUI : MonoBehaviourPunCallbacks
{
[HideInInspector] public Vector3 matrixPos = new Vector3(30, 30, 0);
[HideInInspector] public float matrixScale = 1.9f;
string nicknameInput, roomnameInput;
string stateLabel, callbackLabel, roomListLabel, playerListLabel, roomInfoLabel, lobbyInfoLabel;
int maxplayerInput = 2;
Vector2 roomScroll, playerScroll;
List<RoomInfo> roomList;
bool autoJoinLobby = true;
void Awake()
{
Screen.SetResolution(1280, 720, false);
matrixScale = Screen.height * 1.9f / 1080f;
}
void Update()
{
stateLabel = PhotonNetwork.NetworkClientState.ToString();
}
void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(matrixPos, Quaternion.identity, Vector3.one * matrixScale);
GUILayout.BeginVertical("Box", GUILayout.Width(320));
#region 입력
GUILayout.Label("상태 : " + stateLabel, GUILayout.Width(200));
GUILayout.Label("콜백 : " + callbackLabel, GUILayout.Width(200));
autoJoinLobby = GUILayout.Toggle(autoJoinLobby, " 자동로비접속");
GUILayout.BeginHorizontal();
GUILayout.Label("닉네임");
nicknameInput = GUILayout.TextField(nicknameInput, GUILayout.Width(150));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("방이름");
roomnameInput = GUILayout.TextField(roomnameInput, GUILayout.Width(150));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("최대인원 : " + maxplayerInput);
maxplayerInput = (int)GUILayout.HorizontalSlider(maxplayerInput, 2, 8, GUILayout.Width(100));
GUILayout.EndHorizontal();
GUILayout.Space(10);
#endregion
#region 버튼
GUILayoutOption width = GUILayout.Width(100);
GUILayout.BeginHorizontal();
if (GUILayout.Button("서버접속", width)) ConnectClick();
if (GUILayout.Button("로비접속", width)) JoinLobbyClick();
if (GUILayout.Button("로비나가기", width)) LeaveLobbyClick();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("방만들기", width)) CreateRoomClick();
if (GUILayout.Button("방참가", width)) JoinRoomClick();
if (GUILayout.Button("방참가/만들기", width)) JoinOrCreateRoomClick();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("방랜덤참가", width)) JoinRandomRoomClick();
if (GUILayout.Button("방나가기", width)) LeaveRoomClick();
if (GUILayout.Button("연결끊기", width)) DisconnectClick();
GUILayout.EndHorizontal();
GUILayout.Space(10);
#endregion
#region 로비에서 보는 방리스트
if (PhotonNetwork.InLobby)
{
GUILayout.Label("* 로비에서 보는 방리스트");
GUILayout.Label("방이름\t현재인원\t최대인원");
roomScroll = GUILayout.BeginScrollView(roomScroll, GUILayout.MaxHeight(80));
GUILayout.Label(roomListLabel);
GUILayout.EndScrollView();
}
GUILayout.Space(10);
#endregion
#region 방에서 보는 플레이어리스트
if (PhotonNetwork.InRoom)
{
GUILayout.Label("* 방에서 보는 플레이어리스트");
GUILayout.Label("순번\t닉네임\t액터넘버\t방장\t자신");
playerScroll = GUILayout.BeginScrollView(playerScroll, GUILayout.MaxHeight(80), GUILayout.Width(320));
GUILayout.Label(playerListLabel);
GUILayout.EndScrollView();
GUILayout.Space(10);
GUILayout.Label("* 방정보");
GUILayout.Label(roomInfoLabel);
}
#endregion
GUILayout.EndVertical();
}
#region 버튼과 콜백
void SetCallbackLabel(string s)
{
callbackLabel = s;
}
bool StringAvailable(string s)
{
if (string.IsNullOrWhiteSpace(s)) return false;
return Regex.IsMatch(s, "^[0-9a-zA-Z가-힣]*$");
}
public void ConnectClick()
{
if (StringAvailable(nicknameInput))
{
PhotonNetwork.NickName = nicknameInput;
PhotonNetwork.ConnectUsingSettings();
}
}
public override void OnConnected()
{
SetCallbackLabel("OnConnected");
}
public override void OnConnectedToMaster()
{
SetCallbackLabel("OnConnectedToMaster");
if (autoJoinLobby) JoinLobbyClick();
}
public void JoinLobbyClick()
{
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
SetCallbackLabel("OnJoinedLobby");
}
public void LeaveLobbyClick()
{
PhotonNetwork.LeaveLobby();
}
public override void OnLeftLobby()
{
SetCallbackLabel("OnLeftLobby");
}
public void CreateRoomClick()
{
PhotonNetwork.CreateRoom(roomnameInput, new RoomOptions { MaxPlayers = (byte)maxplayerInput });
}
public override void OnCreatedRoom()
{
SetCallbackLabel("OnCreatedRoom");
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
SetCallbackLabel("OnCreateRoomFailed");
}
public void JoinRoomClick()
{
PhotonNetwork.JoinRoom(roomnameInput);
}
public override void OnJoinedRoom()
{
SetCallbackLabel("OnJoinedRoom");
InRoomListUpdate();
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
SetCallbackLabel("OnJoinRoomFailed");
OnConnectedToMaster();
}
public void JoinOrCreateRoomClick()
{
PhotonNetwork.JoinOrCreateRoom(roomnameInput, new RoomOptions { MaxPlayers = (byte)maxplayerInput }, null);
}
public void JoinRandomRoomClick()
{
PhotonNetwork.JoinRandomRoom();
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
SetCallbackLabel("OnJoinRandomFailed");
}
public void LeaveRoomClick()
{
PhotonNetwork.LeaveRoom();
}
public override void OnLeftRoom()
{
SetCallbackLabel("OnLeftRoom");
}
public void DisconnectClick()
{
PhotonNetwork.Disconnect();
}
public override void OnDisconnected(DisconnectCause cause)
{
SetCallbackLabel("OnDisconnected");
}
#endregion
#region 로비에서 보는 방리스트
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
int roomCount = roomList.Count;
for (int i = 0; i < roomCount; i++)
{
if (!roomList[i].RemovedFromList)
{
if (!roomList.Contains(roomList[i])) roomList.Add(roomList[i]);
else roomList[roomList.IndexOf(roomList[i])] = roomList[i];
}
else if (roomList.IndexOf(roomList[i]) != -1) roomList.RemoveAt(roomList.IndexOf(roomList[i]));
}
string curLabel = "";
for (int i = 0; i < roomList.Count; i++)
{
curLabel += roomList[i].Name + "\t" + roomList[i].PlayerCount + "\t" + roomList[i].MaxPlayers + "\n";
}
roomListLabel = curLabel;
}
#endregion
#region 방에서 보는 플레이어리스트
void InRoomListUpdate()
{
string curLabel = "";
for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
{
Player player = PhotonNetwork.PlayerList[i];
curLabel += i + "\t" + player.NickName + "\t" + player.ActorNumber + "\t" +
player.IsMasterClient + "\t" + player.IsLocal + "\n";
}
playerListLabel = curLabel;
Room curRoom = PhotonNetwork.CurrentRoom;
roomInfoLabel = "방이름:" + curRoom.Name + " 현재인원:" + curRoom.PlayerCount + " 최대인원:" +
curRoom.MaxPlayers + " 열림:" + curRoom.IsOpen + " 보임:" + curRoom.IsVisible;
}
public override void OnRoomPropertiesUpdate(ExitGames.Client.Photon.Hashtable propertiesThatChanged)
{
InRoomListUpdate();
}
public override void OnMasterClientSwitched(Player newMasterClient)
{
InRoomListUpdate();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
InRoomListUpdate();
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
InRoomListUpdate();
}
#endregion
}
PhotonChatGUI.cs 소스입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using Photon.Chat;
using ExitGames.Client.Photon;
public class PhotonChatGUI : MonoBehaviourPunCallbacks, IChatClientListener
{
[HideInInspector] public Vector3 matrixPos = new Vector3(30, 30, 0);
[HideInInspector] public float matrixScale = 1.9f;
ChatClient chatClient;
string stateChatLabel, callbackChatLabel, chatLabel;
string channelName = "채널001", nicknameInput, chatInput;
bool autoJoinChatChannel = true;
Vector2 chatScroll;
void Awake()
{
Screen.SetResolution(1280, 720, false);
matrixScale = Screen.height * 1.9f / 1080f;
chatClient = new ChatClient(this);
}
void Update()
{
chatClient.Service();
}
void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(matrixPos, Quaternion.identity, Vector3.one * matrixScale);
GUILayout.BeginVertical("Box", GUILayout.Width(320));
GUILayout.Label("채팅상태 : " + stateChatLabel);
GUILayout.Label("채팅콜백 : " + callbackChatLabel);
autoJoinChatChannel = GUILayout.Toggle(autoJoinChatChannel, " 자동로비접속");
GUILayout.BeginHorizontal();
GUILayout.Label("닉네임");
nicknameInput = GUILayout.TextField(nicknameInput, GUILayout.Width(150));
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayoutOption width = GUILayout.Width(160);
GUILayout.BeginHorizontal();
if (GUILayout.Button("채팅서버 연결", width)) ConnectChatClick();
if (GUILayout.Button("채팅서버 나가기", width)) DisconnectChatClick();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("채널입장", width)) SubscribeClick();
if (GUILayout.Button("채널나가기", width)) UnsubscribeClick();
GUILayout.EndHorizontal();
GUILayout.Space(10);
chatScroll = GUILayout.BeginScrollView(chatScroll, "Box", GUILayout.MaxHeight(200));
GUILayout.Label(chatLabel);
GUILayout.EndScrollView();
chatInput = GUILayout.TextArea(chatInput);
if (chatInput != null && chatInput.Contains("\n"))
{
if(StringAvailable(chatInput)) Send(chatInput.Trim());
chatInput = "";
}
GUILayout.Label("* 엔터를 치면 보내집니다. (보낼닉네임>>보낼메세지) 로 귓속말이 됩니다");
GUILayout.EndVertical();
}
bool StringAvailable(string s)
{
if (string.IsNullOrWhiteSpace(s)) return false;
return true;
}
public void OnChatStateChange(ChatState state)
{
stateChatLabel = state.ToString();
}
void SetCallbackChatLabel(string s)
{
callbackChatLabel = s;
}
void ReceiveMessages(string nickName, string msg)
{
chatLabel += (chatLabel != "" ? "\n" : "") + nickName + " : " + msg;
}
void ReceiveMessages(string msg)
{
chatLabel += (chatLabel != "" ? "\n" : "") + msg;
}
#region 버튼과 콜백
void ConnectChatClick()
{
if (StringAvailable(nicknameInput))
{
chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion,
new Photon.Chat.AuthenticationValues(nicknameInput));
}
}
public override void OnConnected()
{
SetCallbackChatLabel("OnConnected");
if (autoJoinChatChannel) SubscribeClick();
}
void DisconnectChatClick()
{
chatClient.Disconnect();
}
public void OnDisconnected()
{
SetCallbackChatLabel("OnDisconnected");
}
void SubscribeClick()
{
chatClient.Subscribe(new string[] { channelName }, 10);
}
public void OnSubscribed(string[] channels, bool[] results)
{
chatLabel = "";
SetCallbackChatLabel("OnSubscribed");
}
void UnsubscribeClick()
{
chatClient.Unsubscribe(new string[] { channelName });
}
public void OnUnsubscribed(string[] channels)
{
SetCallbackChatLabel("OnUnsubscribed");
}
void Send(string s)
{
if (s.Contains(">>"))
{
string[] ss = s.Split(new string[] { ">>" }, System.StringSplitOptions.None);
chatClient.SendPrivateMessage(ss[0], ss[1]);
}
else chatClient.PublishMessage(channelName, s);
}
public void OnGetMessages(string channelName, string[] senders, object[] messages)
{
for (int i = 0; i < messages.Length; i++)
{
ReceiveMessages(senders[i], messages[i].ToString());
}
chatScroll.y = 100000;
}
public void OnPrivateMessage(string sender, object message, string channelName)
{
ReceiveMessages(sender + "의 귓속말", message.ToString());
chatScroll.y = 100000;
}
#endregion
public void OnUserSubscribed(string channel, string user) { }
public void OnUserUnsubscribed(string channel, string user) { }
public void OnStatusUpdate(string user, int status, bool gotMessage, object message) { }
public void DebugReturn(DebugLevel level, string message) { }
}