松江品划网站建设维护,源码交易平台,大岭山镇仿做网站,杭州的服装网站建设【Unity工具,简单应用】Photon PUN 2,做一个简单多人聊天室前置知识,安装,及简单UI大厅聊天室简单同步较复杂同步自定义同步最终效果前置知识,安装,及简单UI
【Unity工具,简单学习】PUN 2&…
【Unity工具,简单应用】Photon + PUN 2,做一个简单多人聊天室
前置知识,安装,及简单UI
大厅
聊天室
简单同步
较复杂同步
自定义同步
最终效果
前置知识,安装,及简单UI
【Unity工具,简单学习】PUN 2,多人在线游戏开发,初步使用
需要有一定 UNITY 使用经验的开发者可以顺利阅读。
大厅
简单搭建一下大厅UI。 给 Laucher 节点一个 Launcher 脚本
Launcher 脚本如下,具体功能看注释
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 levelPhotonNetwork.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;}}}}