![]() |
VOOZH | about |
dotnet add package BetterTogether --version 0.10.0
NuGet\Install-Package BetterTogether -Version 0.10.0
<PackageReference Include="BetterTogether" Version="0.10.0" />
<PackageVersion Include="BetterTogether" Version="0.10.0" />Directory.Packages.props
<PackageReference Include="BetterTogether" />Project file
paket add BetterTogether --version 0.10.0
#r "nuget: BetterTogether, 0.10.0"
#:package BetterTogether@0.10.0
#addin nuget:?package=BetterTogether&version=0.10.0Install as a Cake Addin
#tool nuget:?package=BetterTogether&version=0.10.0Install as a Cake Tool
BetterTogether is a simple multiplayer library targeting netstandard2.1, .NET 6, 7 and 8. It is heavily inspired by Playroom which is pretty good and available for most web frameworks/game engines. While Playroom has a Unity SDK, it only support WebGL builds of Unity (for now at least). This project however should run on any platform that supports Net Standard 2.1, .NET 6, 7 or 8.
Get it from NuGet or build it yourself.
dotnet add package BetterTogether
For Unity, use this link in the package manager https://github.com/ZedDevStuff/BetterTogether.git#unity or NugetForUnity which i recommend solely because you can independently update the packages in case you need to.
using BetterTogether;
// Setup a server using the fluent API
BetterServer server = new BetterServer()
.WithMaxPlayers(2)
.WithAdminPlayers(true) // Allow the server to have admins
.Start(9050);
// Seting up a client without the fluent API
BetterClient client = new BetterClient();
// Fired when the client is connected to the server
client.Connected += (id, playerList) =>
{
Console.WriteLine($"Connected as {id}");
Console.WriteLine("Players:");
foreach (var player in playerList)
{
Console.WriteLine(player);
}
};
// Fired when another player is connected
client.PlayerConnected += (player) =>
{
Console.WriteLine($"{player} connected");
};
client.Connect("127.0.0.1", 9050);
Objects need to be MemoryPackable. See MemoryPack
// SetPlayerState is used to set a player state. Only the player or the server can modify this state
client.SetPlayerState<string>("Username", "Player1");
// SetState is used to set a global state. Every player can modify this state
client.SetState<string>("foo", "bar");
// Returns the latest state of the key available on this client
client.GetState<string>("foo");
foreach(var player in client.Players)
{
Console.WriteLine(player, client.GetPlayerState<string>(player, "Username"));
}
// Register a RPC
client.RegisterRPC("Hello", (args) =>
{
string message = MessagePackSerializer.Deserialize<string>(args);
Console.WriteLine(message);
});
// Call a RPC on every client but this one
client.RpcOthers("Hello", "Hello, World!");
BetterTogether uses a simple Packet struct to transfer data. If you want to do something on the server with them, you can do so like this
public Packet? HandleData(NetPeer peer, Packet packet)
{
// Do something with the packet or return null
// Returning null will discard the packet
return packet;
}
server.DataReceived = HandleData;
Unity behaves differently, especially when it comes to threading. To use BetterTogether in Unity, you need to use some kind of dispatcher like this one inside event handlers. Here is an example using the aforementioned dispatcher
using PimDeWitte.UnityMainThreadDispatcher;
// Usual setup ignored
client.Connected += (id, playerList) =>
{
// This must be done with any kind of event handling. RPCs for example
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
Debug.Log($"Connected as {id}");
Debug.Log("Players:");
foreach (var player in playerList)
{
Debug.Log(player);
}
});
};
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.10.0 | 1,434 | 7/7/2024 |
| 0.9.1 | 1,345 | 6/22/2024 |
| 0.9.0 | 1,348 | 6/22/2024 |
| 0.8.2 | 1,361 | 6/22/2024 |
| 0.8.1 | 1,338 | 6/21/2024 |
| 0.8.0 | 1,356 | 6/21/2024 |
| 0.7.0 | 1,357 | 6/21/2024 |
| 0.6.0 | 1,354 | 6/20/2024 |
| 0.5.2 | 1,361 | 6/20/2024 |
| 0.5.1 | 1,335 | 6/20/2024 |
| 0.5.0 | 1,354 | 6/20/2024 |
| 0.4.1 | 1,374 | 6/20/2024 |
| 0.4.0 | 1,339 | 6/19/2024 |
| 0.3.0 | 1,364 | 6/18/2024 |
| 0.2.0 | 1,339 | 6/18/2024 |
Changelog available at https://github.com/ZedDevStuff/BetterTogether/blob/master/CHANGELOG.md