Hi,
I am still new to photon PUN2 and I want to understand if I do this correctly,
I want to make when the player is idle for x time, the player’s text will first greyed out and when time pass y time, player will be disconnect from the room.
Can someone please help pointing me to the right direction.
public class PlayerIdle : MonoBehaviour
{
/// <summary>
/// 900 is 15min
/// </summary>
public int timeIdle = 0;
public int curTime;
public int lastInputTime;
// Date Time
DateTime curDateTime;
[Space(10)]
public bool isIdle = false;
public bool sent = false;
public TextMeshProUGUI displayName;
public PlayerMain pMain;
ExitGames.Client.Photon.Hashtable _customProperties = new ExitGames.Client.Photon.Hashtable();
public Player player { get;set;}
// Start is called before the first frame update
void Start()
{
//curDateTime = DateTime.Now;
//curTime = curDateTime.Minute;
lastInputTime = curDateTime.Minute;
}
void Update()
{
if (!Input.anyKey)
{
curDateTime = DateTime.Now;
//======================//
curTime = curDateTime.Second;
timeIdle = curTime - lastInputTime;
if (timeIdle >= 15 && PhotonNetwork.InRoom)
{
// Grey out the name
isIdle = true;
setIdle();
}
else if (!PhotonNetwork.InRoom)
{
isIdle = false;
timeIdle = 0;
sent = false;
}
if(timeIdle >= 30)
{
// Kick player
sent = false;
if (PhotonNetwork.InRoom)
{
PhotonNetwork.Disconnect();
}
}
}
else
{
timeIdle = 0;
lastInputTime = curDateTime.Minute;
isIdle = false;
sent = false;
//setIdle();
}
}
void setIdle()
{
if(isIdle && !sent)
{
_customProperties["charIdle"] = isIdle;
pMain.Player.SetCustomProperties(_customProperties);
PhotonNetwork.LocalPlayer.CustomProperties = _customProperties;
displayName.color = Color.grey;
SetPlayerText(PhotonNetwork.CurrentRoom.GetPlayer(pMain.Player.ActorNumber));
sent = true;
}
if(!isIdle && !sent)
{
_customProperties["charIdle"] = isIdle;
pMain.Player.SetCustomProperties(_customProperties);
PhotonNetwork.LocalPlayer.CustomProperties = _customProperties;
SetPlayerText(PhotonNetwork.CurrentRoom.GetPlayer(pMain.Player.ActorNumber));
displayName.color = Color.white;
sent = true;
}
}
void SetPlayerText(Player player)
{
bool result;
if (player.CustomProperties.ContainsKey("charIdle"))
{
result = (bool)player.CustomProperties["charIdle"];
if(result)
displayName.color = Color.grey;
else
displayName.color = Color.white;
}
}
}