using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
public class Launcher : MonoBehaviourPunCallbacks
{
public string RoomName ="Room";// 设置房间名,确保每次连接到同一个房间
private string gameVersion ="1";[SerializeField]
private const byte maxPlayersPerRoom =4;// 设置单房间最多玩家数[SerializeField]
private GameObject controlPanel;[SerializeField]
private GameObject progressLabel;voidAwake(){
// Then let master server can use PhotonNetwork.LoadLevel()// Everyone will see the same level
PhotonNetwork.AutomaticallySyncScene = true;// 确保该变量为 true,否则无法同步
progressLabel.SetActive(false);
controlPanel.SetActive(true);}
public voidConnect(){
progressLabel.SetActive(true);
controlPanel.SetActive(false);if(PhotonNetwork.IsConnected)// 若已连接,则直接加入到房间{
PhotonNetwork.JoinOrCreateRoom(RoomName, new RoomOptions(){
MaxPlayers = maxPlayersPerRoom },default);}else{
PhotonNetwork.ConnectUsingSettings();// 用 PhotonServerSettings 来连接
PhotonNetwork.GameVersion = gameVersion;}}
public override voidOnJoinRandomFailed(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(RoomName, new RoomOptions(){
MaxPlayers = maxPlayersPerRoom });}
public override voidOnJoinedRoom()// 若加入了某房间,则加载聊天室场景,不要使用 UNITY的加载场景方法{
Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
PhotonNetwork.LoadLevel("ChatingRoom");}
public override voidOnConnectedToMaster()// 运行ConnectUsingSettings()后会先连接到 Master节点,再创建或加载房间{
Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
PhotonNetwork.JoinOrCreateRoom(RoomName, new RoomOptions(){
MaxPlayers = maxPlayersPerRoom },default);}
public override voidOnDisconnected(DisconnectCause cause)// 若失去连接后{
progressLabel.SetActive(false);
controlPanel.SetActive(true);
Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
public class GameManagerPUN : MonoBehaviourPunCallbacks
{
public string DialogueText ="DialogueText";[SerializeField]
private InputField _inputField;
public GameObject Content;voidStart(){
GameObject go = PhotonNetwork.Instantiate("Sphere", Vector3.zero, Quaternion.identity);
go.GetComponent<ChangeRandomPosition>().change();// 用 PhotonNetwork.Instantiate 创建一个物体,让它的位置随机变一下,代表显示该房间内的玩家个数}
public override voidOnLeftRoom(){
SceneManager.LoadScene("LobbyScene");}
public voidLeaveRoom()// 退出按钮的监听器直接调用该代码即可调用 OnLeftRoom() 方法,退出大厅{
PhotonNetwork.LeaveRoom();}
public override voidOnPlayerEnteredRoom(Player other)// 自动监听是否有玩家进入{
Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName);// not seen if you're the player connectingif(PhotonNetwork.IsMasterClient){
Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);// called before OnPlayerLeftRoom}}
public override voidOnPlayerLeftRoom(Player other)// 自动监听是否有玩家退出{
Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName);// seen when other disconnectsif(PhotonNetwork.IsMasterClient){
Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);// called before OnPlayerLeftRoom}}
public voidSendMessage()// 发送消息,记录发送消息者名称,与它发送的消息,然后创建一个UI物体,加载到滚动content中{
string res = PhotonNetwork.NickName +" : "+ _inputField.text;
GameObject obj = PhotonNetwork.Instantiate(DialogueText, Vector3.zero, Quaternion.identity);
obj.GetComponent<Text>().text = res;
obj.GetComponent<SentenceAsync>().str = res;
obj.transform.SetParent(Content.transform);
_inputField.text ="";// 发送玩后清空输入框}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class SentenceAsync : MonoBehaviourPunCallbacks, IPunObservable
{
public string str;
public GameManagerPUN gm;
public GameObject pa;void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.IsWriting){
stream.SendNext(str);}else{
str =(string)stream.ReceiveNext();if(pa == null)// 用于在不同客户端的同步处理{
pa = GameObject.Find("Content");
this.gameObject.transform.SetParent(pa.transform);
this.GetComponent<Text>().text = str;}}}}