Customizing Behavior
There are settings and attributes unique to SyncTypes which allow various ways of customizing your SyncType.
SyncTypeSettings
SyncTypeSettings can be initialized with any SyncType to define the default settings of your SyncType.
// Custom settings are optional.
// This is an example of declaring a SyncVar without custom settings.
private readonly SyncVar<int> _myInt = new();
// Each SyncType has a different constructor to take settings.
// Here is an example for SyncVars. This will demonstrate how to use
// the unreliable channel for SyncVars, and send the value upon any change.
// There are many different ways to create SyncTypeSettings; you can even
// make a const settings and initialize with that!
private readonly SyncVar<int> _myInt = new(new SyncTypeSettings(0f, Channel.Unreliable));Settings can also be changed at runtime. This can be very useful to change behavior based on your game mechanics and needs, or to even slow down send rate as your player count grows.
// This example shows in Awake but this code
// can be used literally anywhere.
private void Awake()
{
// You can change all settings at once.
_myInt.UpdateSettings(new SyncTypeSettings(....);
// Or update only specific things, such as SendRate.
// Change send rate to once per second.
_myInt.UpdateSendRate(1f);
}Showing in the inspector
SyncTypes can also be shown in the inspector.
You must first make sure your type is marked as serializable if a container; this is a Unity requirement.
Next the SyncType must not use the 'readonly' indicator. We require the readonly indicator by default to emphasis you should not initialize your SyncType at runtime.
Below is an example of what NOT to do.
The code above will actually prevent compilation in the editor as our code generators will detect you did not include the readonly indicator. To remove the readonly indicator you must also add the AllowMutableSyncType above your SyncType.
Last updated