Hi guys,
I'm quite new to Unity and Photon, my problem is whenever I joined a room, I get kicked out from the room after one or two minutes. I saw some threads saying this behaviour is due to too many messages are transferred in a short period of time. I tried removing all the photonViews and networking related components, the result is still the same, I still get kicked out from the room by server. I have no idea where is the error come from, even I open the Windows task manager, it shows no network transmission is occurring.
Here is my code:
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using ExitGames.Client.Photon;
public class NetworkManager : MonoBehaviourPunCallbacks
{
public static NetworkManager Instance { get; private set; }
void Awake()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
void Start()
{
PhotonNetwork.ConnectUsingSettings();
}
public void JoinLobby()
{
//===== check if the client is connected to master server before joining lobby
if(PhotonNetwork.NetworkClientState == ClientState.ConnectedToMasterServer)
{
PhotonNetwork.JoinLobby();
}
}
public void JoinOrCreateRoom(string roomName, string roomType)
{
PhotonNetwork.SetPlayerCustomProperties(UserInfo.localUserInfo.GetCustomProperties());
Hashtable customPropertise = new Hashtable { { "RT", roomType } };
RoomOptions roomOptions = new RoomOptions()
{
IsVisible = true,
IsOpen = true,
CustomRoomProperties = customPropertise,
CustomRoomPropertiesForLobby = new string[] { "RT" },
MaxPlayers = 0
};
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
}
public void JoinRoom(string roomName)
{
PhotonNetwork.SetPlayerCustomProperties(UserInfo.localUserInfo.GetCustomProperties());
PhotonNetwork.JoinRoom(roomName);
}
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
#region Pun Callbacks
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("connected to master");
this.JoinLobby();
}
public override void OnJoinedLobby()
{
base.OnJoinedLobby();
Debug.Log("joined lobby");
}
public override void OnLeftLobby()
{
base.OnLeftLobby();
Debug.Log("left lobby");
}
public override void OnRoomListUpdate(List updatedRoomList)
{
base.OnRoomListUpdate(updatedRoomList);
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
base.OnCreateRoomFailed(returnCode, message);
Debug.Log("fail to create room");
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
Debug.Log("Joined room >>>>> " + PhotonNetwork.CurrentRoom.Name);
//load in room scene
PhotonNetwork.LoadLevel(PhotonNetwork.CurrentRoom.CustomProperties["RT"].ToString() + " Room"); //RT => Room Type
}
public override void OnLeftRoom()
{
base.OnLeftRoom();
Debug.Log("OnLeftRoom");
//load main screen
PhotonNetwork.LoadLevel("MainScene");
}
public override void OnPlayerPropertiesUpdate(Player target, Hashtable changedProps)
{
base.OnPlayerPropertiesUpdate(target, changedProps);
if (target.IsLocal)
{
UserInfo.localUserInfo.UpdateUserInfo(changedProps);
}
else
{
for(int i = 0; i < UsersInRoom.Count; i++)
{
if (target.NickName == UsersInRoom[i].Username)
{
UsersInRoom[i].UpdateUserInfo(changedProps);
}
}
}
}
#endregion
}
Can someone help? I don't mind to share my project if needed.
Btw I'm using PUN2 version 2.12, Unity 2018.3.8f1