当前位置: 首页 > 工具软件 > Photon > 使用案例 >

photon网络框架

费凯康
2023-12-01

photon

MULTIPLAYER 多人对战				COMMUNICATION					SELF-HOSTED
REALTIME 实时更新					CHAT 聊天						   SERVER 自己搭主机
PUN photon针对unity开发的		    VOICE 语音
BOLT 点对点
QUANTUM 质量监控

前期配置

photon有几个重要的类
PhotonNetwork 核心的类 有很多静态方法  这个类是分开的 (public static partial class PhotonNetwork)	
PhotonView 给角色进行网络同步的 (进入战斗用的同步)
Callback 回调 MonoBehaviouPunCallbacks(大而全 没有遵循接口隔离原则 他里面有四个接口 有三个接口没有在这个里面)
PhotonStream 状态同步
using UnityEngine;
using Photon.Pun;
using System;
using System.Collections;
using Photon.Realtime;


public class PunGUIDemo : MonoBehaviourPunCallbacks {
	
    #region Fields
    public GUISkin skin;
    
    private string state;
    //临时state
    private ClientState clientState;
    
    //房间列表
    private List<RoomInfo> roomList;
    #endregion
    
    #region GUI Fields   
    //错误日志
    private string errorlog;
    
    private string roomName = "Room001";
    private string roomMaxPlayer = "2";
    private string nickName = "Player001";
    #endregion
        
    #region Mono Callbacks
    private void Start(){
        //通过用户配置连接服务器
        PhotonNetwork.ConnectUsingSettings();
        clientState = PhotonNetwork.NetworkClientState;
        state = clientState + "\n";
    }
    
    private void Update(){
        if(PhotonNetwork.NetworkClientState != clientState){
            state += PhotonNetwork.NetworkClientState + "\n";
        }
        clientState = PhotonNetwork.NetworkClientState;
    }
    
    private void OnGUI(){
        //设置皮肤
        GUI.skin = skin;
        
        //显示此时photon状态
        GUILayout.Label("Status:" + state);
        
        switch(PhotonNetwork.NetworkClientState){
            //连接主服务器成功状态
            case ClientState.ConnectedToMasterServer:
                ConnectedToMasterServerGUI();
                break;
            case ClientState.Joined:
                JoinedGUI();
                break;
			case ClientState.JoinedLobby:
                JoinedLobbyGUI();
                break;
        }
        GUILayout.Label(errorlog);
    }
   
    //加入大厅状态
    private void JoinedLobbyGUI(){
        GUILayout.Label("---------------------");
        //遍历房间列表
        for(int i = 0;i < roomList.Count;i++){
            GUILayout.Label("房间名称:" + roomList[i].Name); 
            GUILayout.Label("房间人数:" + roomList[i].PlayerCount + "/" + roomList[i].MaxPlayers);
            if(GUILayout.Button("加入该房间")){
                //加入该房间
                PhotonNetwork.JoinRoom(roomList[i].Name);
            }
            
            if(i != roomList.Count - 1){
                GUILayout.Label("*************");
            }
        }
        GUILayout.Label("---------------------");
        
        if(GUILayout.Button("离开大厅")){
            PhotonNetwork.LeaveLobby();
        }
    }
    
    //加入房间状态
    private void JoinedGUI(){
        
        GUILayout.Label("---------------------");
        //排好序的
        //遍历房间中的玩家列表
        for(int i = 0;i < PhotonNetwork.PlayerList.Length;i++){
            
            GUILayout.Label("玩家编号:" + PhotonNetwork.PlayerList[i].ActorNumber);
            GUILayout.Label("玩家名称:" + PhotonNetwork.PlayerList[i].NickName);
            
            if(i != PhotonNetwork.PlayerList.Length - 1){
                GUILayout.Label("*************");
            }
        }
        GUILayout.Label("---------------------");	
        
        if(GUILayout.Button("离开房间")){
            PhotonNetwork.LeaveRoom();
        }
    }
    
    //连接主服务器状态下,GUI的显示
    private void ConnectedToMasterServerGUI(){
        
        GUILayout.Label("请输入昵称:");
        //玩家昵称
        nickName = GUILayout.TextField(nickName);
        
        
        GUILayout.Label("请输入房间名称:");
        //房间名称输入框
        roomName = GUILayout.TextField(roomName);
        GUILayout.Label("请输入房间最大人数:");
        //房间最大人数输入框
        roomMaxPlayer = GUILayout.TextField(roomMaxPlayer);
        
        if(GUILayout.Button("创建房间")){
            
            //创建房间选项
            RoomOptions roomOptions = new RoomOptions();
            try{
                roomOptions.MaxPlayers = Byte.Parse(roomMaxPlayer);
            }
            catch(Exception e){
                roomOptions.MaxPlayers = 2;
            }
            
            //创建房间
            PhotonNetwork.CreateRoom(roomName,roomOptions);
            //设置昵称
			PhotonNetwork.NickName = nickName;
        }
        
        if(GUILayout.Button("随机加入房间")){
            PhotonNetwork.JoinRandomRoom();
            //设置昵称
			PhotonNetwork.NickName = nickName;
        }
        
        if(GUILayout.Button("进入游戏大厅")){
            PhotonNetwork.JoinLobby();
            //设置昵称
			PhotonNetwork.NickName = nickName;
        }
    }
    
    #endregion
        
        
    #region Pun Callbacks
    public override void OnConnected(){
        base.OnConnected();
        Debug.Log("连接NameServer");
    }    
    
    public override void OnConnectedToMaster(){
        base.OnConnectedToMaster();
        Debug.Log("连接MasterServer");
    }
    
    //创建房间成功
    public override void OnCreatedRoom(){
        base.OnCreatedRoom();
        Debug.Log("创建房间成功");
    }
    
    //创建房间失败
    public override void OnCreateRoomFailed(short returnCode,string message){
        base.OnCreateRoomFailed(returnCode,message);
        Debug.Log("创建房间失败" + returnCode + message);
        ShowErrorLog("创建房间失败" + message);
    }
    
    //加入房间成功
    public override void OnJoinedRoom(){
        base.OnJoinedRoom();
        Debug.Log("加入房间成功");
    }
    
    //加入房间失败
    public override void OnJoinRoomFailed(short returnCode,string message){
        base.OnJoinRoomFailed(returnCode,message);
        Debug.Log("加入房间失败");
        ShowErrorLog("加入房间失败" + message);
    }
    
    //随机加入房间失败
    public override void OnJoinRandomFailed(short returnCode,string message){
        base.OnJoinRandomFailed(returnCode,message);
        Debug.Log("随机加入房间失败" + message);
        ShowErrorLog("随机加入房间失败" + message);
    }
    
    //加入大厅成功
    public override void OnJoinedLobby(){
        base.OnJoinedLobby();
        Debug.Log("加入大厅成功");
    }
    
    public override void OnRoomListUpdate(List<RoomInfo> roomList){
        base.OnRoomListUpdate(roomList);
        //更新房间列表
        this.roomList = roomList;
    }
    
    public override void OnLeftRoom(){
        base.OnLeftRoom();
        Debug.Log("玩家离开了房间");
    }
    
    public override void OnLeftLobby(){
        base.OnLeftLobby();
        Debug.Log("玩家离开了大厅");	
    }
    
    //别人进入了房间
    public override void OnPlayerEnteredRoom(Player newPlayer){
        base.OnPlayerEnteredRoom(newPlayer);
        Debug.Log(newPlayer.NickName + "进入了房间");	
    }
    
    //别人离开了房间
    public override void OnPlayerLeftRoom(Player otherPlayer){
        base.OnPlayerLeftRoom(otherPlayer);
        Debug.Log(otherPlayer.NickName + "离开了房间");	
    }
    #endregion
        
    #region Common
        
    private void ShowErrorLog(string error){
        StartCoroutine(ErrorOperation(error));
    }
    
    IEnumerator ErrorOperation(string error){
        errorlog = error;
        yield return new WaitForSeconds(3);
        errorlog = "";
    }
    #endregion

    
        
        
}
 类似资料: