Asset SyncVars

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.

public class Player : NetworkBehaviour
{
    [SyncVar(hook="OnMat")]
    Material mat;

    void OnMat(Material m)
    {
        GetComponent<Renderer>().material = m;
        mat = m;
    }

    public Material otherMaterial;

    void Awake()
    {
        ClientScene.RegisterAsset(otherMaterial);
    }

    public override void OnStartClient()
    {
        if (mat != null)
            GetComponent<Renderer>().material = mat;
    }

        [ClientRpc]
        void RpcSetMaterial(Material mat)
        {
          GetComponent<Renderer>().material = mat;
        }
}
4 Likes

I think its incredibly cool.

Nice. That’s only in beta 5.2 or…? I see its in the beta forums. :stuck_out_tongue:

I think that would be really cool.

That would be awesome ! We could even send entire transform without having to get position, rotation and scale just to merge them right after !

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.

So, will textures and stuff synced by content or by asset id?

everything would by synced by reference, not by content

Okay, good to know.
Either way great news.

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?