Sync Property

For synchronizing not frequently updated GameObject States

Create Sync Property

Sync Properties are created in the Unity Editor Inspector by clicking the Add Sync Property button.

You can have up to 250 Sync Properties in one Sync Property Agent.

Maximum message size of a syncProperty is 4KB.

Events

Modify and Read Sync Property

Sync Properties can be modified by calling SWNetwork API in C# scripts

SyncPropertyAgent Class

Modify Sync Property

public void Modify(string name, bool value);
public void Modify(string name, int value);
public void Modify(string name, float value);
public void Modify(string name, string value);
public void Modify(string name, Vector2 value);
public void Modify(string name, Vector3 value);
public void Modify(string name, Vector4 value);
public void Modify(string name, Quaternion value);
public void Modify<T>(string name, T value);
public void ModifyTinyJson(string name, object value);

Get Sync Property

public SWSyncedProperty GetPropertyWithName(string name);

SWSyncedProperty Class

Read Sync property values

public int GetIntValue();
public bool GetBoolValue();
public float GetFloatValue();
public string GetStringValue();
public Vector2 GetVector2Value();
public Vector3 GetVector3Value();
public Vector4 GetVector4Value();
public Quaternion GetQuaternionValue();
public T GetValue<T>();
public T GetTinyJsonValue<T>();

Example

In this example, an Int Synced Property of name "hp" is modified.

public void GotHit(int damage)
{
    SyncPropertyAgent syncPropertyAgent = gameObject.GetComponent<SyncPropertyAgent>();
    
    // change hp to 100
    syncPropertyAgent.Modify("hp", 100);
}

To get updates when a SyncProperty is modified, you can add a handler to the OnValueChange event of the SyncProperty.

    public void OnHpChanged()
    {
        // read hp value
        int currentHP = syncPropertyAgent.GetPropertyWithName("hp").GetIntValue();
    }

Last updated