실행 이미지
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
public class Launcher : MonoBehaviourPunCallbacks
{
#region Private Serializable Fields
#endregion
#region Private Fields
/// <summary>
/// This client's version number. Users are separated from each other by gameVersion (which allows you to make breaking changes).
/// </summary>
string gameVersion = "1";
[Tooltip("The maximum number of players per room. When a room is full, it can't be joined by new players, and so new room will be created")]
[SerializeField]
private byte maxPlayersPerRoom = 4;
#endregion
#region MonoBehaviour CallBacks
/// <summary>
/// MonoBehaviour method called on GameObject by Unity during early initialization phase.
/// </summary>
///
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
[SerializeField]
private GameObject controlPanel;
[Tooltip("The UI Label to inform the user that the connection is in progress")]
[SerializeField]
private GameObject progressLabel;
bool isConnecting;
#region MonoBehaviourPunCallbacks Callbacks
public override void OnConnectedToMaster()
{
if (isConnecting)
{
Debug.Log("Connected to Master, joining lobby...");
PhotonNetwork.JoinLobby();
}
Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
}
public override void OnJoinedLobby()
{
if (isConnecting)
{
Debug.Log("Joined Lobby, joining random room...");
PhotonNetwork.JoinRandomRoom();
}
}
public override void OnDisconnected(DisconnectCause cause)
{
progressLabel.SetActive(false);
controlPanel.SetActive(true);
Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
}
#endregion
void Awake()
{
// #Critical
// this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
PhotonNetwork.AutomaticallySyncScene = true;
}
/// <summary>
/// MonoBehaviour method called on GameObject by Unity during initialization phase.
/// </summary>
void Start()
{
progressLabel.SetActive(false);
controlPanel.SetActive(true);
}
#endregion
#region Public Methods
/// <summary>
/// Start the connection process.
/// - If already connected, we attempt joining a random room
/// - if not yet connected, Connect this application instance to Photon Cloud Network
/// </summary>
public void Connect()
{
isConnecting = true;
progressLabel.SetActive(true);
controlPanel.SetActive(false);
Debug.Log($" 연결상태: {PhotonNetwork.IsConnected}");
// we check if we are connected or not, we join if we are , else we initiate the connection to the server.
if (PhotonNetwork.IsConnected)
{
// #Critical we need at this point to attempt joining a Random Room. If it fails, we'll get notified in OnJoinRandomFailed() and we'll create one.
PhotonNetwork.JoinRandomRoom();
Debug.Log($" 연결상태: {PhotonNetwork.IsConnected}");
}
else
{
// #Critical, we must first and foremost connect to Photon Online Server.
PhotonNetwork.GameVersion = gameVersion;
PhotonNetwork.ConnectUsingSettings();
Debug.Log($" 연결상태: {PhotonNetwork.IsConnected}");
}
Debug.Log($" 연결상태: {PhotonNetwork.IsConnected}");
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("PUN Basics Tutorial/Launcher: OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
// #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayersPerRoom });
}
public override void OnJoinedRoom()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
Debug.Log("We load the 'Room for 1' ");
// #Critical
// Load the Room Level.
PhotonNetwork.LoadLevel("Roomfor1");
}
#endregion
}
Luancher.cs
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Photon.Realtime;
namespace Com.MyCompany.MyGame
{
public class GameManager : MonoBehaviourPunCallbacks
{
#region Photon Callbacks
public override void OnPlayerEnteredRoom(Player other)
{
Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName); // not seen if you're the player connecting
if (PhotonNetwork.IsMasterClient)
{
Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
LoadArena();
}
}
public override void OnPlayerLeftRoom(Player other)
{
Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName); // seen when other disconnects
if (PhotonNetwork.IsMasterClient)
{
Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
LoadArena();
}
}
#endregion
#region Private Methods
void LoadArena()
{
if (!PhotonNetwork.IsMasterClient)
{
Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
return;
}
Debug.LogFormat("PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount);
PhotonNetwork.LoadLevel("Roomfor" + PhotonNetwork.CurrentRoom.PlayerCount);
}
#endregion
#region Photon Callbacks
/// <summary>
/// Called when the local player left the room. We need to load the launcher scene.
/// </summary>
public override void OnLeftRoom()
{
SceneManager.LoadScene(0);
}
#endregion
#region Public Methods
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
#endregion
}
}
GameManager.cs
'KDT > 네트워크 콘텐츠 제작' 카테고리의 다른 글
[PUN - Photon Unity Networking] Photon을 이용한 네트워크 연결 및 동기화 해보기 - Treasure Hunter (0) | 2024.06.13 |
---|