UnityConnections - CaptainToTo/owl-tree-unity GitHub Wiki

UnityConnections

The UnityConnection component wraps OwlTree's core Connection class, and has much of the same interface. You can read more about Connections here.

One key difference between using a UnityConnection and a normal Connection, is how they are started. To start a UnityConnection, call the Connect() method, and optionally provide a UnityConnection.Args object. Normally, this object will be gotten from a ConnectionArgs scriptable object.

using OwlTree.Unity;
using UnityEngine;

// attach to the same gameobject the UnityConnection is attached to
public class ConnectionRunner : MonoBehaviour
{
    [SerializeField] ConnectionArgs args;

    void Awake()
    {
        var connection = GetComponent<UnityConnection>();
        connection.Connect(args.GetArgs());
    }
}

If no args object is provided, then the connection will instead use the ConnectionArgs assigned in the Args serialized field.

Prefab Spawning

Separate from NetworkObject spawning, UnityConnection adds the ability to synchronously spawn prefabs. This requires any prefab you plan to spawn synchronously to be in the Prefabs list in the args passed to the connection. The order of the list matters, and should be the same across all applications. Synchronously spawned gameobjects can then be managed by the session authority.

using OwlTree;
using UnityEngine;

// add listeners to corresponding unity events on the UnityConnection
public class ConnectionCallbacks : Monobehaviour
{
    // player prefab must be in the prefab array of the args passed to the connection
    [SerializeField] GameObject playerPrefab;

    UnityConnection connection;
    void OnAwake()
    {
        connection = GetComponent<UnityConnection>();
    }

    public void OnClientConnected(ClientId id)
    {
        if (connection.IsAuthority)
            var player = connection.Spawn(playerPrefab).GetComponent<Player>();
    }

    public void OnClientDisconnected(ClientId id)
    {
        if (connection.IsAuthority)
        {
            Player player = connection.Maps.Get(id);
            connection.Despawn(player);
        }
    }
}

A gameobject that's been spawned through the connection will always have a NetworkGameObject component attach to it, with a unique GameObjectId that can be used to identify the object across clients. You can read more about NetworkGameObjects here. These gameobjects can be retrieved using the <GameObjectId, NetworkGameObject> map:

NetworkGameObject netObj = connection.Maps.Get(gameObjectId);

IEnumerable<NetworkGameObject> netObjs = connection.GameObjects;

if (connection.Maps.TryGet(gameObjectId, out NetworkGameObject netObj)
    ...

connection.WaitForObject<GameObjectId, NetworkGameObject>(gameObjectId, (netObj) => ...);

UnityConnections have two more events for gameobject spawning and despawning, which are separate from the NetworkObject events.

connection.OnGameObejctSpawn.AddListener(...);
connection.OnGameObjectDespawn.AddListener(...);

Simulation Loop

UnityConnections execute received RPCs in FixedUpdate. This means that if you send an RPC every frame in Update, the callee will be executing all of them, one after another, in the same FixedUpdate tick. If you are sending continuous state updates, such as position, rotation, or timers, call the RPC in FixedUpdate instead.

⚠️ **GitHub.com Fallback** ⚠️