Partial classes instead of regions

Instead of marking your code with regions, such code can also be extracted to a partial class.
This will reduce the number of lines in one file :wink:
Instead of this:

// MyNetworkPlayer.cs
#region Server

[Server]
public void SetDisplayName(string newDisplayName)
{
    displayName = newDisplayName;
}

[Server]
public void SetDisplayColour(Color newDisplayColour)
{
    displayColour = newDisplayColour;
}

[Command]
private void CmdSetDisplayName(string newDisplayName)
{
    RpcLogNewName(newDisplayName);

    SetDisplayName(newDisplayName);
}

#endregion

following can be done:

// MyNetworkPlayer.Server.cs
public partial class MyNetworkPlayer {
    [Server]
    public void SetDisplayName(string newDisplayName)
    {
        displayName = newDisplayName;
    }

    [Server]
    public void SetDisplayColour(Color newDisplayColour)
    {
        displayColour = newDisplayColour;
    }

    [Command]
    private void CmdSetDisplayName(string newDisplayName)
    {
        RpcLogNewName(newDisplayName);

        SetDisplayName(newDisplayName);
    }
}

Note that original class from where the code was extracted, needs to be marked partial too.

1 Like

Privacy & Terms