using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using ADBannerView = UnityEngine.iOS.ADBannerView;
namespace Com.MyCompany.MyGame
{
///<summary>
///Player name input field.Let the user input his name,will appear above the player in the game.
/// </summary>>
[RequireComponent(typeof(InputField))]
public class PlayerNameInputField : MonoBehaviour
{
#region Private Constants
//Store the PlayerPref Key to avoid types
private const string playerNamePrefKey = "PlayerName";
#endregion
#region MonoBehaviour CallBacke
void Start()
{
string defaultName = string.Empty;
InputField _inputField = this.GetComponent<InputField>();
if (_inputField != null)
{
if (PlayerPrefs.HasKey(playerNamePrefKey))
{
defaultName = PlayerPrefs.GetString(playerNamePrefKey);
_inputField.text = defaultName;
}
}
PhotonNetwork.NickName = defaultName;
}
#endregion
#region Public Methods
public void SetPlayerName(string value)
{
if (string.IsNullOrEmpty(value))
{
Debug.LogError("Player Name is null orempty");
return;
}
PhotonNetwork.NickName = value;
PlayerPrefs.SetString(playerNamePrefKey,value);
}
#endregion
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Photon.Realtime;
namespace Com.MyCompany.MyGame
{
public class GameManager : MonoBehaviourPunCallbacks
{
#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
}
}
public override void OnLeftRoom()
{
SceneManager.LoadScene(0);
}
using System;
using Photon.Pun;
using UnityEngine;
namespace Com.MyCompany.MyGame
{
public class Launcher : MonoBehaviour
{
#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 changers).
/// </summary>
string gameVersion = "1";
#endregion
#region Monobihaviour CallBackes
private 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
/// </summary>
void Start()
{
Connect();
}
#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
/// </summary>
void Connect()
{
//we check if we are connected or not, we hoin if we are , else we initialize
if (PhotonNetwork.IsConnected)
{
//#Critical we need at this point ot attempt hoining a Random Room.
PhotonNetwork.JoinRandomRoom();
}
else
{
//#Critical, we must first and foremost connect to Photon Online Serer
PhotonNetwork.GemeVersion = gameVersion;
PhotonNetwork.ConnectUsingSettings();
}
}
#endregion
}
}
void Connect()
{
//we check if we are connected or not, we hoin if we are , else we initialize
if (PhotonNetwork.IsConnected)
{
//#Critical we need at this point ot attempt Joining a Random Room.
PhotonNetwork.JoinRandomRoom();
}
else
{
//#Critical, we must first and foremost connect to Photon Online Serer
PhotonNetwork.GameVersion = gameVersion;
PhotonNetwork.ConnectUsingSettings();
}
}
public class Launcher : MonoBehaviourPunCallbacks
{
[Photon.Realtime]を追加します。
using System;
using Photon.Pun;
using UnityEngine;
using Photon.Realtime;
namespace Com.MyCompany.MyGame
{
...
次にコールバックの処理をクラスの最後に加えます。
#region MonoBehaviourPunCallbacks Callbacks
public override void OnConnectedToMaster()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was Called by PUN with reason {0}",cause);
}
#endregion
public override void OnConnectedToMaster()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
//#Critical: The first we try to do is to hoin a potential existing room.If there is, good, else, we'll be called back with OnJoinRandomFailed()
PhotonNetwork.JoinRandomRoom();
}
ルームの参加に失敗した場合その旨を通知する必要があります。
次の文をクラスに追加します。
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());
}
public override void OnJoinedRoom()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
}