2022-03-14 01:22:50 -07:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using Mirror;
|
2022-03-14 01:53:12 -07:00
|
|
|
|
using System.Threading;
|
2022-03-14 00:51:25 -07:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Utility.LinkedWorldObject;
|
|
|
|
|
|
2022-08-15 23:34:12 -07:00
|
|
|
|
public static class ILinkedWorldObject_Extensions
|
2022-03-14 00:51:25 -07:00
|
|
|
|
{
|
2022-03-14 03:34:00 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// link a world object and a network behaviour
|
|
|
|
|
/// </summary>
|
2022-08-16 17:32:33 -07:00
|
|
|
|
public static void LinkTo(this ILinkedWorldObject<NetworkBehaviour> @this, ILinkedNetworkBehaviour networkBehaviour)
|
2022-03-14 03:34:00 -07:00
|
|
|
|
{
|
2022-08-16 17:32:33 -07:00
|
|
|
|
@this.SetNetworkBehaviour((NetworkBehaviour)networkBehaviour);
|
|
|
|
|
networkBehaviour.SetWorldObject(@this);
|
2022-03-14 03:34:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 01:22:50 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// link a world object and network object, then spawn it.
|
|
|
|
|
/// (host only)
|
|
|
|
|
/// </summary>
|
2022-08-16 17:32:33 -07:00
|
|
|
|
public static void SpawnLinked(this ILinkedWorldObject<NetworkBehaviour> @this, GameObject prefab, bool spawnWithServerAuthority)
|
2022-03-14 00:51:25 -07:00
|
|
|
|
{
|
2022-03-14 01:53:12 -07:00
|
|
|
|
var go = Object.Instantiate(prefab);
|
2022-03-14 03:34:00 -07:00
|
|
|
|
var networkBehaviour = go.GetComponent<ILinkedNetworkBehaviour>();
|
2022-03-14 01:22:50 -07:00
|
|
|
|
|
2022-08-16 17:32:33 -07:00
|
|
|
|
@this.LinkTo(networkBehaviour);
|
2022-03-14 01:22:50 -07:00
|
|
|
|
|
2022-03-14 03:44:52 -07:00
|
|
|
|
if (spawnWithServerAuthority)
|
|
|
|
|
{
|
|
|
|
|
go.SpawnWithServerAuthority();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NetworkServer.Spawn(go);
|
|
|
|
|
}
|
2022-03-14 00:51:25 -07:00
|
|
|
|
}
|
2022-03-14 01:22:50 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// wait for a world object to be linked.
|
|
|
|
|
/// (non host only)
|
|
|
|
|
/// </summary>
|
2022-08-16 17:32:33 -07:00
|
|
|
|
public static async UniTask WaitForLink(this ILinkedWorldObject<NetworkBehaviour> @this, CancellationToken ct) =>
|
|
|
|
|
await UniTask.WaitUntil(() => @this.NetworkBehaviour, cancellationToken: ct);
|
2022-03-14 00:51:25 -07:00
|
|
|
|
}
|