简体   繁体   中英

Unity 5 networking color sync

I'm trying to sync my cube's color from my server script. I have Network Transform and Network Identity component on it and transform synchronizing works good, now I'd need to sync the color. I need something like Network Material component or how else should I do this? Thank you :) (I hope you got what i mean, sorry for english.)

You can sync values using the SyncVar attribute:

[SyncVar]
Color myColor;

This will keep the changes syncronized on clients and the server. But this is just a field in your class of color type, it doesn't actually change the color of your render.

You could change the color of your render to your myColor value in the Update callback, but it would update it even when there is no change in the color.

The best approach would be using a hook:

[SyncVar (hook = "OnColorChanged")]
Color myColor;

This will call the "OnColorChanged" function each time the color is updated.

Something like this should work (if you are working with SpriteRenders):

[SyncVar (hook = "OnColorChanged")]
Color myColor;

void OnColorChanged(Color value)
{
    myColor = value;
    GetComponent<SpriteRenderer>().color = myColor;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM