Photon Server 유니티 멀티게임 만들기 webinar 1탄

NetworkManager.cs 연결 테스트 소스입니다



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;

public class NetworkManager : MonoBehaviourPunCallbacks
{
string networkState;

void Start() => PhotonNetwork.ConnectUsingSettings();

public override void OnConnectedToMaster() => 
PhotonNetwork.JoinLobby();

public override void OnJoinedLobby() => 
PhotonNetwork.JoinOrCreateRoom("room", new RoomOptions { MaxPlayers = 4 }, null);

void Update()
{
string curNetworkState = PhotonNetwork.NetworkClientState.ToString();
if (networkState != curNetworkState) 
{
networkState = curNetworkState;
print(networkState);
}
}
}










MyFirstPlugin.MyFirstPlugin.cs 소스입니다



using Photon.Hive.Plugin;

namespace MyFirstPlugin
{
class MyFirstPlugin : PluginBase
{
public override string Name => "MyFirstPlugin";

public override void OnCreateGame(ICreateGameCallInfo info)
{
PluginHost.LogInfo($"OnCreateGame {info.Request.GameId} by user {info.UserId}");
info.Continue(); // same as base.OnCreateGame(info);
}

}
}








MyFirstPlugin.MyPluginFactory.cs 소스입니다



using System.Collections.Generic;
using Photon.Hive.Plugin;

namespace MyFirstPlugin
{
    public class MyPluginFactory : IPluginFactory
    {
        public IGamePlugin Create(IPluginHost gameHost, string pluginName, 
            Dictionary<string, string> config, out string errorMsg)
        {
            MyFirstPlugin plugin = new MyFirstPlugin();
            if (plugin.SetupInstance(gameHost, config, out errorMsg))
            {
                return plugin;
            }
            return null;
        }
    }
}







MyFirstPlugin.MyFirstPlugin.cs 전체 소스입니다



using Photon.Hive.Plugin;

namespace MyFirstPlugin
{
class MyFirstPlugin : PluginBase
{
public override string Name => "MyFirstPlugin";
public override string Version => base.Version;
public override bool IsPersistent => base.IsPersistent;

void TestProperties() 
{
PluginHost.LogInfo($"### AppVersion {AppVersion}");
PluginHost.LogInfo($"### AppId {AppId}");
PluginHost.LogInfo($"### Region {Region}");
PluginHost.LogInfo($"### Cloud {Cloud}");
PluginHost.LogInfo($"### EnvironmentVerion {EnvironmentVerion}");
PluginHost.LogInfo($"### Name {Name}");
PluginHost.LogInfo($"### Version {Version}");
PluginHost.LogInfo($"### IsPersistent {IsPersistent}");
}

public override void BeforeCloseGame(IBeforeCloseGameCallInfo info)
{
// 방에서 나가기 전
PluginHost.LogInfo($"### BeforeCloseGame by user {info.UserId}");
info.Continue();
}

public override void BeforeJoin(IBeforeJoinGameCallInfo info)
{
// 방에 참가하기 전
PluginHost.LogInfo($"### BeforeJoin {info.Request.GameId} by user {info.UserId}");
info.Continue();
}

public override void BeforeSetProperties(IBeforeSetPropertiesCallInfo info)
{
// 커스텀 프로퍼티를 Set하기 전
PluginHost.LogInfo($"### BeforeSetProperties {info.Request.Properties.Keys} by user {info.UserId}");
info.Continue();
}

public override void OnCloseGame(ICloseGameCallInfo info)
{
// 방에서 나간 후
PluginHost.LogInfo($"### OnCloseGame by user {info.UserId}");
info.Continue();
}

public override void OnCreateGame(ICreateGameCallInfo info)
{
// 방을 만든 후
PluginHost.LogInfo($"### OnCreateGame {info.Request.GameId} by user {info.UserId}");
TestProperties();
info.Continue();
}

public override void OnJoin(IJoinGameCallInfo info)
{
// 방에 참가 후
PluginHost.LogInfo($"### OnJoin {info.Request.GameId} by user {info.UserId}");
info.Continue();
}

public override void OnLeave(ILeaveGameCallInfo info)
{
// 방에서 나간 후
PluginHost.LogInfo($"### OnLeave by user {info.UserId}");
info.Continue();
}

public override void OnRaiseEvent(IRaiseEventCallInfo info)
{
// RaiseEvent 발생 후
PluginHost.LogInfo($"### OnRaiseEvent {info.Request.Data} by user {info.UserId}");
info.Continue();
}

public override void OnSetProperties(ISetPropertiesCallInfo info)
{
// 커스텀 프로퍼티 Set 후
PluginHost.LogInfo($"### OnSetProperties {info.Request.Properties.Keys} by user {info.UserId}");
info.Continue();
}

protected override void OnChangeMasterClientId(int oldId, int newId)
{
// 마스터 클라이언트가 바뀐 후
PluginHost.LogInfo($"### OnChangeMasterClientId oldId {oldId}, newId {newId}");
base.OnChangeMasterClientId(oldId, newId);
}
}
}








NetworkManager.cs 재접속 소스입니다



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;

public class NetworkManager : MonoBehaviourPunCallbacks
{
string networkState;
public string BackupRoomName 
{
get => PlayerPrefs.GetString("BackupRoomName", "");
set => PlayerPrefs.SetString("BackupRoomName", value);
}


void Start() => 
PhotonNetwork.ConnectUsingSettings();

void Update()
{
string curNetworkState = PhotonNetwork.NetworkClientState.ToString();
if (networkState != curNetworkState)
{
networkState = curNetworkState;
print(networkState);
}
}

public override void OnConnectedToMaster() =>
PhotonNetwork.JoinLobby();

public override void OnJoinedLobby() 
{
if (BackupRoomName == "")
{
// 방을 만들거나 참여하는 로직
PhotonNetwork.JoinOrCreateRoom("room", new RoomOptions { MaxPlayers = 4, EmptyRoomTtl = 10000 }, null);
}
else
PhotonNetwork.JoinRoom(BackupRoomName);
}

public override void OnJoinedRoom()
{
BackupRoomName = PhotonNetwork.CurrentRoom.Name;
PhotonNetwork.Instantiate("Cube", Vector2.zero, Quaternion.identity);
}

public override void OnDisconnected(DisconnectCause cause) => 
PhotonNetwork.Reconnect();

public override void OnLeftRoom() =>
BackupRoomName = "";

public void OnApplicationQuit() => 
BackupRoomName = "";
}