Unity自用工具.Photon大厅

Unity自用工具.Photon大厅_第1张图片

using System;
using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class Lobby :  MonoBehaviourPunCallbacks
{
    public Text Info;
    public static Text S_info;
    private int _timeOff=1;
    public static int networktype;
    public void Start()
    {
        S_info = Info;
        networktype++;
        
        ConnectToPhoton();
    }

    public void ConnectToPhoton()
    {
        PhotonNetwork.GameVersion = "1.0";
        PhotonNetwork.ConnectUsingSettings();
    }

    public void FixedUpdate()
    {
        switch (networktype)
        {
            case 1:
                Info.text = $"正在链接服务器({_timeOff}%)";
                break;
            case 2:
                Info.text = $"正在加入大厅({_timeOff}%)";
                break;
            case 101:
                try
                {
                    Info.text = $"创建房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是拥有者";
                    networktype = 102;
                }
                catch (NullReferenceException e)
                {
                    Info.text = $"房间正在创建({((_timeOff>98)? _timeOff : 98)}%)";
                    if (_timeOff>150)
                    {
                        networktype = 0;
                        Info.text =
                            "创建房间失败(Connection lost. OnStatusChanged to TimeoutDisconnect. Client state was: ConnectingToGameServer. SCS v0 UDP SocketErrorCode: 0 WinSock)";
                    }
                }
                break;
            case 102:
                Info.text = $"创建房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是拥有者";
                break;
            case 201:
                try
                {
                    Info.text = $"加入房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是客人";
                    networktype = 202;
                }
                catch (NullReferenceException e)
                {
                    Info.text = $"房间正在加入({_timeOff}%)";
                }
                break;
            case 202:
                Info.text = $"加入房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是客人";
                break;
        }
        int gt = Random.Range(0, 20);
        if (gt >16)
        {
            if (_timeOff<98)
            {
                _timeOff += Random.Range(1, 3);
            }
        }
    }

    public override void OnConnectedToMaster()
    {
        _timeOff = 0;
        networktype++;
        Debug.Log("Connected to Photon Server.");
        JoinLobby();
    }
    

    public void JoinLobby()
    {
        PhotonNetwork.JoinLobby();
    }

   public void BTNCRoom()
    {
        NetworkManager.IsMulitPlayer = true;
        CreateRoom("Test",5);
    }

   public void BNTJRoom()
   {
       NetworkManager.IsMulitPlayer = true;
        JoinRoom("Test");
    }

   public void GameStart()
   {
       if (networktype==102)
       {
           PhotonNetwork.LoadLevel("Bag");
       }
   }
    public void CreateRoom(string roomName, int maxPlayers)
    {
        if (networktype==0)
        {
            networktype++;
            ConnectToPhoton();
        }
        PhotonNetwork.AutomaticallySyncScene = true;
        networktype = 101;
        RoomOptions roomOptions = new RoomOptions
        {
            MaxPlayers = (byte)maxPlayers
        };
        try
        {
            PhotonNetwork.CreateRoom(roomName, roomOptions);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
      
    }

    public void JoinRoom(string roomName)
    {
        PhotonNetwork.AutomaticallySyncScene = true;
        networktype = 201;
        PhotonNetwork.JoinRoom(roomName);
    }

    public override void OnJoinedLobby()
    {
        _timeOff = 0;
        networktype++;
        Info.text = "准备完成";
        Debug.Log("Joined Lobby.");
        // You can perform additional actions here, like creating a room or listing available rooms.
    }

    public override void OnCreatedRoom()
    {
        Debug.Log("Room created successfully.");
        // You can perform additional actions when the room is created successfully.
    }

    public override void OnJoinRoomFailed(short returnCode, string message)
    {
        Debug.LogError("Failed to join room: " + message);
    }
}

你可能感兴趣的:(unity,游戏引擎)