Advertisement
Guest User

Untitled

a guest
May 31st, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1.     // Instead of having an expensive update function, we'll just hook playerName and run code ONLY when it changes.
  2.     [SyncVar(hook = "UpdateName")]
  3.     public float playerName;
  4.  
  5.     private string localName = "SomeDefaultName";
  6.     private TextMesh textMesh;
  7.  
  8.     void Start()
  9.     {
  10.         textMesh = GetComponentInChildren<TextMesh>();
  11.     }
  12.  
  13.     // This hook is run on all players when the value of playerName changes on the server.
  14.     void UpdateName(float val)
  15.     {
  16.         playerName = val;
  17.  
  18.         // Update the name text for all players.
  19.         textMesh.text = playerName;
  20.     }
  21.  
  22.     void OnGUI()
  23.     {
  24.         // Only allow the owner of this player object to change this player object's name.
  25.         // Kind of unnecessary though due to the player authority system.
  26.         if (isLocalPlayer)
  27.         {
  28.             localName = GUI.TextField (new Rect (25, Screen.height - 40, 100, 30), localName);
  29.  
  30.             if (GUI.Button (new Rect (130, Screen.height - 40, 80, 30), "Change"))
  31.             {
  32.                 CmdChangeName(localName);        
  33.             }
  34.         }
  35.     }
  36.  
  37.     [Command]
  38.     void CmdChangeName (string newName)
  39.     {
  40.         print ("Change Player Name");
  41.         playerName = newName;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement