using Cysharp.Threading.Tasks; using Mirror; using System.Threading; using UnityEngine; namespace QSB.Utility.LinkedWorldObject; public static class ILinkedWorldObject_Extensions { /// /// link a world object and a network behaviour /// public static void LinkTo(this ILinkedWorldObject @this, ILinkedNetworkBehaviour networkBehaviour) { @this.SetNetworkBehaviour((NetworkBehaviour)networkBehaviour); networkBehaviour.SetWorldObject(@this); } /// /// link a world object and network object, then spawn it. /// (host only) /// public static void SpawnLinked(this ILinkedWorldObject @this, GameObject prefab, bool spawnWithServerOwnership) { var go = Object.Instantiate(prefab); var networkBehaviour = go.GetComponent(); @this.LinkTo(networkBehaviour); if (spawnWithServerOwnership) { go.SpawnWithServerOwnership(); } else { NetworkServer.Spawn(go); } } /// /// wait for a world object to be linked. /// (non host only) /// public static async UniTask WaitForLink(this ILinkedWorldObject @this, CancellationToken ct) => await UniTask.WaitUntil(() => @this.NetworkBehaviour, cancellationToken: ct); }