Unity Multiplayer With Mirror - SyncVar and SyncVar Hook (Freebie)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sinclair and synchro hooks are going to be a utility of mirror you will be using frequently they are used to keep variables synchronized on objects across the network either to all clients or the owning client to begin i have an object which is acting as the player prefab this is the object that spawns when i connect in demonstrating the sync very logic on this object i have a sprite renderer which i'll change the color of to demonstrate the sync fair i have a network identity so this is a networked object and i have a sync fair demo script and note the sync mode is set to observers so in this example i'd be synchronizing to all clients i'm going to open up the sync fair demo script up top i have a field named underscore color it is of type color 32 and just above that i have a sync there attribute this indicates that this will be a sync there meaning it will be synchronized over the network automatically notice the properties hook equals name of set color set color is the method which will be called on clients after the server updates to sync there's value if you do not need to know when a sync fair updates on clients you can simply leave the hook part out and just use syncfare as demonstrated here it's important to remember that syncvir will update automatically on clients but only if you change the value on the server in addition if a new player joins later in the game they'll receive that updated value as well and the hook will call too going down to set color you can see that it has two parameters the first one is old color the second is new color you can name these whatever you like however the first entry will always be the old value before the change and the second entry will be the new value and again keep in mind that this is called from the hook so this will be called on clients only in this method when i receive the hook so meaning that the color value has changed i will get the sprite renderer on this object and i will set the renderer color to the new color like i said before you'll have to set the value on the server for it to synchronize to clients so onstart server i am starting curating randomize color that's just down here this will run indefinitely it will wait a few seconds and it will change the color field to a random color and since that color field is a sync fair it will update automatically to clients now that you've seen the script let's see this code in action i have a buildup and on the build i'm going to run this as a server and since this is a server only you won't see the sprite render until a player joins since that is a player object so i'm going to go ahead and connect a client as discussed the server is changing the color every two seconds and then that is being updated to clients notice that the sprite render is updating on the client but it's not on the server as mentioned before the hook will only call on clients including client host as you can see in the code i'm not actually updating a sprite renderer color in the curating which is running on the server the server updates the field which will update the sync for on clients and if a hook is present they'll call on the clients as well
Info
Channel: First Gear Games
Views: 30,876
Rating: undefined out of 5
Keywords: Unity3D, Unity, Mirror, Mirror Networking, UNET, Unity Multiplayer, Game Development, Tutorial, C#, Coding, SyncVar, sync var, SyncVarhook, syncvar hook, sync var hook
Id: COLp1PzPKvE
Channel Id: undefined
Length: 2min 55sec (175 seconds)
Published: Fri Jul 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.