Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice
  2. We've opened up a space to discuss, share feedback, and showcase everything related to the Unity Shader Graph! Come show us what you've made.
    Dismiss Notice

PHOTON/ Having problem syncing player name and sprite to other players!

Discussion in 'Connected Games' started by Playjoa, Feb 8, 2019.

  1. Playjoa

    Playjoa

    Joined:
    Aug 21, 2017
    Posts:
    4
    Currently I have a script attached to my player object that spawns along with other players, I've successfully achieved movement and camera follow on the players, however I need to update the sprites so players can choose their unique skin, and on top of that show their names!

    // This is the script which I use for offline and for the multiplayer! I've set my skins in an array, and have an ID (skinAtual) stored in a playerprefs.int that is the position of the selected sprites. I've looked online and tryed this RPC method which causes for the player to see everyone on the game with his name and skin on all the players, and now I'm stuck and don't know how to proceed!

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         if (isOnline)
    4.         {
    5.             PV = GetComponent<PhotonView>();
    6.             //idSkin = PlayerPrefs.GetInt("skinAtual");
    7.             //nome = PlayerPrefs.GetString("name");
    8.         }
    9.  
    10.         spr = GetComponent<SpriteRenderer>();
    11.  
    12.         CarregarSkin();
    13.     }
    14.  
    15.     void CarregarSkin()
    16.     {
    17.         if (isOnline)
    18.         {
    19.             if (PV.IsMine)
    20.                 PV.RPC("loadSprite", RpcTarget.AllBuffered, null);
    21.         }
    22.         else
    23.         {
    24.             spr.sprite = skins[PlayerPrefs.GetInt("skinAtual")];
    25.         }
    26.     }
    27.  
    28.     [PunRPC]
    29.     void loadSprite()
    30.     {
    31.         GetComponent<SpriteRenderer>().sprite = skins[PlayerPrefs.GetInt("skinAtual")];
    32.         txtNome.text = PlayerPrefs.GetString("name");
    33.     }
    This is what I get inGame:
    upload_2019-2-8_20-3-9.png
     
    Last edited: Feb 8, 2019
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,147
    Player skin is a good example of a value you can sync via Custom Properties. It's per player and won't change all that often.
    Here's the short intro:
    https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state

    Alternatively, you can use the last (optional) parameter of PhotonNetwork.Instantiate() to pass values along with the instantiation of the character. Some script on the prefab has to check the "instantiation data" to pick and show the correct skin.

    Hope that gives you some ideas.
     
    Playjoa likes this.
  3. Playjoa

    Playjoa

    Joined:
    Aug 21, 2017
    Posts:
    4

    YEEES Thank You! I've managed to get it working with custom properties, anyone having this issue this is how I got it working!

    Code (CSharp):
    1. PhotonView PV;
    2.     Player player;
    3.     ExitGames.Client.Photon.Hashtable PropriedadesPlayer = new ExitGames.Client.Photon.Hashtable();
    4.     private SpriteRenderer spr;
    5.    
    6.     private void Start()
    7.     {
    8.         if (isOnline)
    9.         {
    10.             PV = GetComponent<PhotonView>();
    11.         }
    12.  
    13.         spr = GetComponent<SpriteRenderer>();
    14.  
    15.         CarregarSkin();
    16.     }
    17.  
    18.     void CarregarSkin()
    19.     {
    20.         if (isOnline)
    21.         {
    22.             if (PV.IsMine)
    23.             {  
    24.                 PropriedadesPlayer.Add("idSkin", PlayerPrefs.GetInt("skinAtual"));
    25.                 PropriedadesPlayer.Add("nomePlayer", PlayerPrefs.GetString("name"));
    26.  
    27.                 PhotonNetwork.LocalPlayer.SetCustomProperties(PropriedadesPlayer);
    28.  
    29.                 spr.sprite = skins[PlayerPrefs.GetInt("skinAtual")];
    30.                 txtNome.text = PhotonNetwork.LocalPlayer.NickName;
    31.             }
    32.             else
    33.             {
    34.                 player = PV.Owner;
    35.  
    36.                 spr.sprite = skins[(int)player.CustomProperties["idSkin"]];
    37.                 txtNome.text = (string)player.CustomProperties["nomePlayer"];
    38.             }
    39.  
    40.         }
    41.         else
    42.         {
    43.             spr.sprite = skins[PlayerPrefs.GetInt("skinAtual")];
    44.         }
    45.     }

    And this is how it looks like in game now, :D so happy thanks for the light tobiass!
     

    Attached Files:

    tobiass likes this.