mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-21 09:39:56 +00:00
40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Mirror;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
|
|
namespace QSB.Utility.LinkedWorldObject;
|
|
|
|
public static class Extensions
|
|
{
|
|
/// <summary>
|
|
/// link a world object and a network behaviour
|
|
/// </summary>
|
|
public static void LinkTo(this ILinkedWorldObject<NetworkBehaviour> worldObject, ILinkedNetworkBehaviour networkBehaviour)
|
|
{
|
|
worldObject.SetNetworkBehaviour((NetworkBehaviour)networkBehaviour);
|
|
networkBehaviour.SetWorldObject(worldObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// link a world object and network object, then spawn it.
|
|
/// (host only)
|
|
/// </summary>
|
|
public static void SpawnLinked(this ILinkedWorldObject<NetworkBehaviour> worldObject, GameObject prefab)
|
|
{
|
|
var go = Object.Instantiate(prefab);
|
|
var networkBehaviour = go.GetComponent<ILinkedNetworkBehaviour>();
|
|
|
|
worldObject.LinkTo(networkBehaviour);
|
|
|
|
NetworkServer.Spawn(go);
|
|
}
|
|
|
|
/// <summary>
|
|
/// wait for a world object to be linked.
|
|
/// (non host only)
|
|
/// </summary>
|
|
public static async UniTask WaitForLink(this ILinkedWorldObject<NetworkBehaviour> worldObject, CancellationToken ct) =>
|
|
await UniTask.WaitUntil(() => worldObject.NetworkBehaviour, cancellationToken: ct);
|
|
}
|