Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Asset SyncVars

Discussion in '5.2 Beta' started by seanr, Jul 13, 2015.

  1. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Is this cool or is it crazy?

    SyncVars could be any Unity asset type, such as Texture, Material, Sprite, Shader, Light..

    As long as they are registered at runtime on the client, they can be sent from the server to clients. This applies to parameters in ClientRpc calls as well.

    Code (CSharp):
    1. public class Player : NetworkBehaviour
    2. {
    3.     [SyncVar(hook="OnMat")]
    4.     Material mat;
    5.  
    6.     void OnMat(Material m)
    7.     {
    8.         GetComponent<Renderer>().material = m;
    9.         mat = m;
    10.     }
    11.  
    12.     public Material otherMaterial;
    13.  
    14.     void Awake()
    15.     {
    16.         ClientScene.RegisterAsset(otherMaterial);
    17.     }
    18.  
    19.     public override void OnStartClient()
    20.     {
    21.         if (mat != null)
    22.             GetComponent<Renderer>().material = mat;
    23.     }
    24.  
    25.         [ClientRpc]
    26.         void RpcSetMaterial(Material mat)
    27.         {
    28.           GetComponent<Renderer>().material = mat;
    29.         }
    30. }
    31.  
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think its incredibly cool.
     
  3. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Nice. That's only in beta 5.2 or..? I see its in the beta forums. :p
     
  4. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    I think that would be really cool.
     
  5. Garazbolg

    Garazbolg

    Joined:
    Oct 19, 2014
    Posts:
    2
    That would be awesome ! We could even send entire transform without having to get position, rotation and scale just to merge them right after !
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    ah.. but that misconception is one reason not to do this. Passing a component such as a Transform would pass by reference, not by value. So the other end would get a reference to its local instance of the transform with the local values, not the values from the source object.
     
  7. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    So, will textures and stuff synced by content or by asset id?
     
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    everything would by synced by reference, not by content
     
  9. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Okay, good to know.
    Either way great news.
     
  10. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Looks very UE4-ish but I don't understand the example. You have SyncVar on mat, but it's not being modified anywhere else other than the hook? The asset that is registered is otherMaterial but that's not being used anywhere. The RPC is setup but what's the typical call to the RPC look like?