Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Sync player text color

Discussion in 'Multiplayer' started by scarffy, Nov 2, 2020.

  1. scarffy

    scarffy

    Joined:
    Jan 15, 2013
    Posts:
    23
    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.

    Code (CSharp):
    1.  public class PlayerIdle : MonoBehaviour
    2.     {
    3.         /// <summary>
    4.         /// 900 is 15min
    5.         /// </summary>
    6.         public int timeIdle = 0;
    7.         public int curTime;
    8.         public int lastInputTime;
    9.  
    10.         // Date Time
    11.         DateTime curDateTime;
    12.  
    13.         [Space(10)]
    14.         public bool isIdle = false;
    15.         public bool sent = false;
    16.  
    17.         public TextMeshProUGUI displayName;
    18.  
    19.         public PlayerMain pMain;
    20.  
    21.         ExitGames.Client.Photon.Hashtable _customProperties = new ExitGames.Client.Photon.Hashtable();
    22.  
    23.         public Player player { get;set;}
    24.  
    25.         // Start is called before the first frame update
    26.         void Start()
    27.         {
    28.             //curDateTime = DateTime.Now;
    29.             //curTime = curDateTime.Minute;
    30.             lastInputTime = curDateTime.Minute;
    31.         }
    32.  
    33.         void Update()
    34.         {
    35.             if (!Input.anyKey)
    36.             {
    37.                 curDateTime = DateTime.Now;
    38.                 //======================//
    39.  
    40.                 curTime = curDateTime.Second;
    41.  
    42.                 timeIdle = curTime - lastInputTime;
    43.  
    44.                 if (timeIdle >= 15 && PhotonNetwork.InRoom)
    45.                 {
    46.                     // Grey out the name
    47.  
    48.                     isIdle = true;
    49.                     setIdle();
    50.                 }
    51.                 else if (!PhotonNetwork.InRoom)
    52.                 {
    53.                     isIdle = false;
    54.                     timeIdle = 0;
    55.                     sent = false;
    56.                 }
    57.  
    58.                 if(timeIdle >= 30)
    59.                 {
    60.                     // Kick player
    61.                     sent = false;
    62.  
    63.                     if (PhotonNetwork.InRoom)
    64.                     {
    65.                         PhotonNetwork.Disconnect();
    66.                        
    67.                     }
    68.                 }
    69.             }
    70.             else
    71.             {
    72.                 timeIdle = 0;
    73.                 lastInputTime = curDateTime.Minute;
    74.                 isIdle = false;
    75.                 sent = false;
    76.                 //setIdle();
    77.             }
    78.  
    79.  
    80.         }
    81.  
    82.         void setIdle()
    83.         {
    84.             if(isIdle && !sent)
    85.             {
    86.                 _customProperties["charIdle"] = isIdle;
    87.                 pMain.Player.SetCustomProperties(_customProperties);
    88.                 PhotonNetwork.LocalPlayer.CustomProperties = _customProperties;
    89.                 displayName.color = Color.grey;
    90.                 SetPlayerText(PhotonNetwork.CurrentRoom.GetPlayer(pMain.Player.ActorNumber));
    91.                 sent = true;
    92.             }
    93.             if(!isIdle && !sent)
    94.             {
    95.                 _customProperties["charIdle"] = isIdle;
    96.                 pMain.Player.SetCustomProperties(_customProperties);
    97.                 PhotonNetwork.LocalPlayer.CustomProperties = _customProperties;
    98.                 SetPlayerText(PhotonNetwork.CurrentRoom.GetPlayer(pMain.Player.ActorNumber));
    99.                 displayName.color = Color.white;
    100.                 sent = true;
    101.             }
    102.         }
    103.  
    104.         void SetPlayerText(Player player)
    105.         {
    106.             bool result;
    107.             if (player.CustomProperties.ContainsKey("charIdle"))
    108.             {
    109.                 result = (bool)player.CustomProperties["charIdle"];
    110.                 if(result)
    111.                     displayName.color = Color.grey;
    112.                 else
    113.                     displayName.color = Color.white;
    114.             }
    115.         }
    116.  
    117.        
    118.     }
     
  2. scarffy

    scarffy

    Joined:
    Jan 15, 2013
    Posts:
    23
    Ok. I got it working by using [PunRPC]
     
unityunity