This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/RpcTarget.cs
Unity Technologies 143a6cbd34 com.unity.netcode.gameobjects@2.0.0-exp.2
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [2.0.0-exp.2] - 2024-04-02

### Added
- Added updates to all internal messages to account for a distributed authority network session connection.  (#2863)
- Added `NetworkRigidbodyBase` that provides users with a more customizable network rigidbody, handles both `Rigidbody` and `Rigidbody2D`, and provides an option to make `NetworkTransform` use the rigid body for motion.  (#2863)
  - For a customized `NetworkRigidbodyBase` class:
    - `NetworkRigidbodyBase.AutoUpdateKinematicState` provides control on whether the kinematic setting will be automatically set or not when ownership changes.
    - `NetworkRigidbodyBase.AutoSetKinematicOnDespawn` provides control on whether isKinematic will automatically be set to true when the associated `NetworkObject` is despawned.
    - `NetworkRigidbodyBase.Initialize` is a protected method that, when invoked, will initialize the instance. This includes options to:
      - Set whether using a `RigidbodyTypes.Rigidbody` or `RigidbodyTypes.Rigidbody2D`.
      - Includes additional optional parameters to set the `NetworkTransform`, `Rigidbody`, and `Rigidbody2d` to use.
  - Provides additional public methods:
    - `NetworkRigidbodyBase.GetPosition` to return the position of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.GetRotation` to return the rotation of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.MovePosition` to move to the position of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.MoveRotation` to move to the rotation of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.Move` to move to the position and rotation of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.Move` to move to the position and rotation of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.SetPosition` to set the position of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.SetRotation` to set the rotation of the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting).
    - `NetworkRigidbodyBase.ApplyCurrentTransform` to set the position and rotation of the `Rigidbody` or `Rigidbody2d` based on the associated `GameObject` transform (depending upon its initialized setting).
    - `NetworkRigidbodyBase.WakeIfSleeping` to wake up the rigid body if sleeping.
    - `NetworkRigidbodyBase.SleepRigidbody` to put the rigid body to sleep.
    - `NetworkRigidbodyBase.IsKinematic` to determine if the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting) is currently kinematic.
    - `NetworkRigidbodyBase.SetIsKinematic` to set the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting) current kinematic state.
    - `NetworkRigidbodyBase.ResetInterpolation` to reset the `Rigidbody` or `Rigidbody2d` (depending upon its initialized setting) back to its original interpolation value when initialized.
  - Now includes a `MonoBehaviour.FixedUpdate` implementation that will update the assigned `NetworkTransform` when `NetworkRigidbodyBase.UseRigidBodyForMotion` is true. (#2863)
- Added `RigidbodyContactEventManager` that provides a more optimized way to process collision enter and collision stay events as opposed to the `Monobehaviour` approach. (#2863)
  - Can be used in client-server and distributed authority modes, but is particularly useful in distributed authority.
- Added rigid body motion updates to `NetworkTransform` which allows users to set interolation on rigid bodies. (#2863)
  - Extrapolation is only allowed on authoritative instances, but custom class derived from `NetworkRigidbodyBase` or `NetworkRigidbody` or `NetworkRigidbody2D` automatically switches non-authoritative instances to interpolation if set to extrapolation.
- Added distributed authority mode support to `NetworkAnimator`. (#2863)
- Added session mode selection to `NetworkManager` inspector view. (#2863)
- Added distributed authority permissions feature. (#2863)
- Added distributed authority mode specific `NetworkObject` permissions flags (Distributable, Transferable, and RequestRequired). (#2863)
- Added distributed authority mode specific `NetworkObject.SetOwnershipStatus` method that applies one or more `NetworkObject` instance's ownership flags. If updated when spawned, the ownership permission changes are synchronized with the other connected clients. (#2863)
- Added distributed authority mode specific `NetworkObject.RemoveOwnershipStatus` method that removes one or more `NetworkObject` instance's ownership flags. If updated when spawned, the ownership permission changes are synchronized with the other connected clients. (#2863)
- Added distributed authority mode specific `NetworkObject.HasOwnershipStatus` method that will return (true or false) whether one or more ownership flags is set. (#2863)
- Added distributed authority mode specific `NetworkObject.SetOwnershipLock` method that locks ownership of a `NetworkObject` to prevent ownership from changing until the current owner releases the lock. (#2863)
- Added distributed authority mode specific `NetworkObject.RequestOwnership` method that sends an ownership request to the current owner of a spawned `NetworkObject` instance. (#2863)
- Added distributed authority mode specific `NetworkObject.OnOwnershipRequested` callback handler that is invoked on the owner/authoritative side when a non-owner requests ownership. Depending upon the boolean returned value depends upon whether the request is approved or denied. (#2863)
- Added distributed authority mode specific `NetworkObject.OnOwnershipRequestResponse` callback handler that is invoked when a non-owner's request has been processed. This callback includes a `NetworkObjet.OwnershipRequestResponseStatus` response parameter that describes whether the request was approved or the reason why it was not approved. (#2863)
- Added distributed authority mode specific `NetworkObject.DeferDespawn` method that defers the despawning of `NetworkObject` instances on non-authoritative clients based on the tick offset parameter. (#2863)
- Added distributed authority mode specific `NetworkObject.OnDeferredDespawnComplete` callback handler that can be used to further control when deferring the despawning of a `NetworkObject` on non-authoritative instances. (#2863)
- Added `NetworkClient.SessionModeType` as one way to determine the current session mode of the network session a client is connected to. (#2863)
- Added distributed authority mode specific `NetworkClient.IsSessionOwner` property to determine if the current local client is the current session owner of a distributed authority session. (#2863)
- Added distributed authority mode specific client side spawning capabilities. When running in distributed authority mode, clients can instantiate and spawn `NetworkObject` instances (the local client is authomatically the owner of the spawned object). (#2863)
  - This is useful to better visually synchronize owner authoritative motion models and newly spawned `NetworkObject` instances (i.e. projectiles for example).
- Added distributed authority mode specific client side player spawning capabilities. Clients will automatically spawn their associated player object locally. (#2863)
- Added distributed authority mode specific `NetworkConfig.AutoSpawnPlayerPrefabClientSide` property (default is true) to provide control over the automatic spawning of player prefabs on the local client side. (#2863)
- Added distributed authority mode specific `NetworkManager.OnFetchLocalPlayerPrefabToSpawn` callback that, when assigned, will allow the local client to provide the player prefab to be spawned for the local client. (#2863)
  - This is only invoked if the `NetworkConfig.AutoSpawnPlayerPrefabClientSide` property is set to true.
- Added distributed authority mode specific `NetworkBehaviour.HasAuthority` property that determines if the local client has authority over the associated `NetworkObject` instance (typical use case is within a `NetworkBehaviour` script much like that of `IsServer` or `IsClient`). (#2863)
- Added distributed authority mode specific `NetworkBehaviour.IsSessionOwner` property that determines if the local client is the session owner (typical use case would be to determine if the local client can has scene management authority within a `NetworkBehaviour` script). (#2863)
- Added support for distributed authority mode scene management where the currently assigned session owner can start scene events (i.e. scene loading and scene unloading). (#2863)

### Fixed

- Fixed issue where the host was not invoking `OnClientDisconnectCallback` for its own local client when internally shutting down. (#2822)
- Fixed issue where NetworkTransform could potentially attempt to "unregister" a named message prior to it being registered. (#2807)
- Fixed issue where in-scene placed `NetworkObject`s with complex nested children `NetworkObject`s (more than one child in depth) would not synchronize properly if WorldPositionStays was set to true. (#2796)

### Changed
- Changed client side awareness of other clients is now the same as a server or host. (#2863)
- Changed `NetworkManager.ConnectedClients` can now be accessed by both server and clients. (#2863)
- Changed `NetworkManager.ConnectedClientsList` can now be accessed by both server and clients. (#2863)
- Changed `NetworkTransform` defaults to owner authoritative when connected to a distributed authority session. (#2863)
- Changed `NetworkVariable` defaults to owner write and everyone read permissions when connected to a distributed authority session (even if declared with server read or write permissions).  (#2863)
- Changed `NetworkObject` no longer implements the `MonoBehaviour.Update` method in order to determine whether a `NetworkObject` instance has been migrated to a different scene. Instead, only `NetworkObjects` with the `SceneMigrationSynchronization` property set will be updated internally during the `NetworkUpdateStage.PostLateUpdate` by `NetworkManager`. (#2863)
- Changed `NetworkManager` inspector view layout where properties are now organized by category. (#2863)
- Changed `NetworkTransform` to now use `NetworkTransformMessage` as opposed to named messages for NetworkTransformState updates. (#2810)
- Changed `CustomMessageManager` so it no longer attempts to register or "unregister" a null or empty string and will log an error if this condition occurs. (#2807)
2024-04-02 00:00:00 +00:00

592 lines
26 KiB
C#

using System.Collections.Generic;
using Unity.Collections;
namespace Unity.Netcode
{
/// <summary>
/// Configuration for the default method by which an RPC is communicated across the network
/// </summary>
public enum SendTo
{
/// <summary>
/// Send to the NetworkObject's current owner.
/// Will execute locally if the local process is the owner.
/// </summary>
Owner,
/// <summary>
/// Send to everyone but the current owner, filtered to the current observer list.
/// Will execute locally if the local process is not the owner.
/// </summary>
NotOwner,
/// <summary>
/// Send to the server, regardless of ownership.
/// Will execute locally if invoked on the server.
/// </summary>
Server,
/// <summary>
/// Send to everyone but the server, filtered to the current observer list.
/// Will NOT send to a server running in host mode - it is still treated as a server.
/// If you want to send to servers when they are host, but not when they are dedicated server, use
/// <see cref="ClientsAndHost"/>.
/// <br />
/// <br />
/// Will execute locally if invoked on a client.
/// Will NOT execute locally if invoked on a server running in host mode.
/// </summary>
NotServer,
/// <summary>
/// Execute this RPC locally.
/// <br />
/// <br />
/// Normally this is no different from a standard function call.
/// <br />
/// <br />
/// Using the DeferLocal parameter of the attribute or the LocalDeferMode override in RpcSendParams,
/// this can allow an RPC to be processed on localhost with a one-frame delay as if it were sent over
/// the network.
/// </summary>
Me,
/// <summary>
/// Send this RPC to everyone but the local machine, filtered to the current observer list.
/// </summary>
NotMe,
/// <summary>
/// Send this RPC to everone, filtered to the current observer list.
/// Will execute locally.
/// </summary>
Everyone,
/// <summary>
/// Send this RPC to all clients, including the host, if a host exists.
/// If the server is running in host mode, this is the same as <see cref="Everyone" />.
/// If the server is running in dedicated server mode, this is the same as <see cref="NotServer" />.
/// </summary>
ClientsAndHost,
/// <summary>
/// Send this RPC to the authority.
/// In distributed authority mode, this will be the owner of the NetworkObject.
/// In normal client-server mode, this is basically the exact same thing as a server rpc.
/// </summary>
Authority,
/// <summary>
/// Send this RPC to all non-authority instances.
/// In distributed authority mode, this will be the non-owners of the NetworkObject.
/// In normal client-server mode, this is basically the exact same thing as a client rpc.
/// </summary>
NotAuthority,
/// <summary>
/// This RPC cannot be sent without passing in a target in RpcSendParams.
/// </summary>
SpecifiedInParams
}
public enum RpcTargetUse
{
Temp,
Persistent
}
/// <summary>
/// Implementations of the various <see cref="SendTo"/> options, as well as additional runtime-only options
/// <see cref="Single"/>,
/// <see cref="Group(NativeArray{ulong})"/>,
/// <see cref="Group(NativeList{ulong})"/>,
/// <see cref="Group(ulong[])"/>,
/// <see cref="Group{T}(T)"/>, <see cref="Not(ulong)"/>,
/// <see cref="Not(NativeArray{ulong})"/>,
/// <see cref="Not(NativeList{ulong})"/>,
/// <see cref="Not(ulong[])"/>, and
/// <see cref="Not{T}(T)"/>
/// </summary>
public class RpcTarget
{
private NetworkManager m_NetworkManager;
internal RpcTarget(NetworkManager manager)
{
m_NetworkManager = manager;
Everyone = new EveryoneRpcTarget(manager);
Owner = new OwnerRpcTarget(manager);
NotOwner = new NotOwnerRpcTarget(manager);
Server = new ServerRpcTarget(manager);
NotServer = new NotServerRpcTarget(manager);
NotMe = new NotMeRpcTarget(manager);
Me = new LocalSendRpcTarget(manager);
ClientsAndHost = new ClientsAndHostRpcTarget(manager);
Authority = new AuthorityRpcTarget(manager);
NotAuthority = new NotAuthorityRpcTarget(manager);
m_CachedProxyRpcTargetGroup = new ProxyRpcTargetGroup(manager);
m_CachedTargetGroup = new RpcTargetGroup(manager);
m_CachedDirectSendTarget = new DirectSendRpcTarget(manager);
m_CachedProxyRpcTarget = new ProxyRpcTarget(0, manager);
m_CachedProxyRpcTargetGroup.Lock();
m_CachedTargetGroup.Lock();
m_CachedDirectSendTarget.Lock();
m_CachedProxyRpcTarget.Lock();
}
public void Dispose()
{
Everyone.Dispose();
Owner.Dispose();
NotOwner.Dispose();
Server.Dispose();
NotServer.Dispose();
NotMe.Dispose();
Me.Dispose();
ClientsAndHost.Dispose();
Authority.Dispose();
NotAuthority.Dispose();
m_CachedProxyRpcTargetGroup.Unlock();
m_CachedTargetGroup.Unlock();
m_CachedDirectSendTarget.Unlock();
m_CachedProxyRpcTarget.Unlock();
m_CachedProxyRpcTargetGroup.Dispose();
m_CachedTargetGroup.Dispose();
m_CachedDirectSendTarget.Dispose();
m_CachedProxyRpcTarget.Dispose();
}
/// <summary>
/// Send to the NetworkObject's current owner.
/// Will execute locally if the local process is the owner.
/// </summary>
public BaseRpcTarget Owner;
/// <summary>
/// Send to everyone but the current owner, filtered to the current observer list.
/// Will execute locally if the local process is not the owner.
/// </summary>
public BaseRpcTarget NotOwner;
/// <summary>
/// Send to the server, regardless of ownership.
/// Will execute locally if invoked on the server.
/// </summary>
public BaseRpcTarget Server;
/// <summary>
/// Send to everyone but the server, filtered to the current observer list.
/// Will NOT send to a server running in host mode - it is still treated as a server.
/// If you want to send to servers when they are host, but not when they are dedicated server, use
/// <see cref="SendTo.ClientsAndHost"/>.
/// <br />
/// <br />
/// Will execute locally if invoked on a client.
/// Will NOT execute locally if invoked on a server running in host mode.
/// </summary>
public BaseRpcTarget NotServer;
/// <summary>
/// Execute this RPC locally.
/// <br />
/// <br />
/// Normally this is no different from a standard function call.
/// <br />
/// <br />
/// Using the DeferLocal parameter of the attribute or the LocalDeferMode override in RpcSendParams,
/// this can allow an RPC to be processed on localhost with a one-frame delay as if it were sent over
/// the network.
/// </summary>
public BaseRpcTarget Me;
/// <summary>
/// Send this RPC to everyone but the local machine, filtered to the current observer list.
/// </summary>
public BaseRpcTarget NotMe;
/// <summary>
/// Send this RPC to everone, filtered to the current observer list.
/// Will execute locally.
/// </summary>
public BaseRpcTarget Everyone;
/// <summary>
/// Send this RPC to all clients, including the host, if a host exists.
/// If the server is running in host mode, this is the same as <see cref="Everyone" />.
/// If the server is running in dedicated server mode, this is the same as <see cref="NotServer" />.
/// </summary>
public BaseRpcTarget ClientsAndHost;
/// <summary>
/// Send this RPC to the authority.
/// In distributed authority mode, this will be the owner of the NetworkObject.
/// In normal client-server mode, this is basically the exact same thing as a server rpc.
/// </summary>
public BaseRpcTarget Authority;
/// <summary>
/// Send this RPC to all non-authority instances.
/// In distributed authority mode, this will be the non-owners of the NetworkObject.
/// In normal client-server mode, this is basically the exact same thing as a client rpc.
/// </summary>
public BaseRpcTarget NotAuthority;
/// <summary>
/// Send to a specific single client ID.
/// </summary>
/// <param name="clientId"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Single(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Single(ulong clientId, RpcTargetUse use)
{
if (clientId == m_NetworkManager.LocalClientId)
{
return Me;
}
if (m_NetworkManager.IsServer || clientId == NetworkManager.ServerClientId)
{
if (use == RpcTargetUse.Persistent)
{
return new DirectSendRpcTarget(clientId, m_NetworkManager);
}
m_CachedDirectSendTarget.SetClientId(clientId);
return m_CachedDirectSendTarget;
}
if (use == RpcTargetUse.Persistent)
{
return new ProxyRpcTarget(clientId, m_NetworkManager);
}
m_CachedProxyRpcTarget.SetClientId(clientId);
return m_CachedProxyRpcTarget;
}
/// <summary>
/// Send to everyone EXCEPT a specific single client ID.
/// </summary>
/// <param name="excludedClientId"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Not(ulong excludedClientId, RpcTargetUse use)
{
IGroupRpcTarget target;
if (m_NetworkManager.IsServer)
{
if (use == RpcTargetUse.Persistent)
{
target = new RpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedTargetGroup;
}
}
else
{
if (use == RpcTargetUse.Persistent)
{
target = new ProxyRpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedProxyRpcTargetGroup;
}
}
target.Clear();
foreach (var clientId in m_NetworkManager.ConnectedClientsIds)
{
if (clientId != excludedClientId)
{
target.Add(clientId);
}
}
// If ServerIsHost, ConnectedClientIds already contains ServerClientId and this would duplicate it.
if (!m_NetworkManager.ServerIsHost && excludedClientId != NetworkManager.ServerClientId)
{
target.Add(NetworkManager.ServerClientId);
}
return target.Target;
}
/// <summary>
/// Sends to a group of client IDs.
/// NativeArrays can be trivially constructed using Allocator.Temp, making this an efficient
/// Group method if the group list is dynamically constructed.
/// </summary>
/// <param name="clientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Group(NativeArray<ulong> clientIds, RpcTargetUse use)
{
IGroupRpcTarget target;
if (m_NetworkManager.IsServer)
{
if (use == RpcTargetUse.Persistent)
{
target = new RpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedTargetGroup;
}
}
else
{
if (use == RpcTargetUse.Persistent)
{
target = new ProxyRpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedProxyRpcTargetGroup;
}
}
target.Clear();
foreach (var clientId in clientIds)
{
target.Add(clientId);
}
return target.Target;
}
/// <summary>
/// Sends to a group of client IDs.
/// NativeList can be trivially constructed using Allocator.Temp, making this an efficient
/// Group method if the group list is dynamically constructed.
/// </summary>
/// <param name="clientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Group(NativeList<ulong> clientIds, RpcTargetUse use)
{
var asArray = clientIds.AsArray();
return Group(asArray, use);
}
/// <summary>
/// Sends to a group of client IDs.
/// Constructing arrays requires garbage collected allocations. This override is only recommended
/// if you either have no strict performance requirements, or have the group of client IDs cached so
/// it is not created each time.
/// </summary>
/// <param name="clientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Group(ulong[] clientIds, RpcTargetUse use)
{
return Group(new NativeArray<ulong>(clientIds, Allocator.Temp), use);
}
/// <summary>
/// Sends to a group of client IDs.
/// This accepts any IEnumerable type, such as List&lt;ulong&gt;, but cannot be called without
/// a garbage collected allocation (even if the type itself is a struct type, due to boxing).
/// This override is only recommended if you either have no strict performance requirements,
/// or have the group of client IDs cached so it is not created each time.
/// </summary>
/// <param name="clientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Group<T>(T clientIds, RpcTargetUse use) where T : IEnumerable<ulong>
{
IGroupRpcTarget target;
if (m_NetworkManager.IsServer)
{
if (use == RpcTargetUse.Persistent)
{
target = new RpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedTargetGroup;
}
}
else
{
if (use == RpcTargetUse.Persistent)
{
target = new ProxyRpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedProxyRpcTargetGroup;
}
}
target.Clear();
foreach (var clientId in clientIds)
{
target.Add(clientId);
}
return target.Target;
}
/// <summary>
/// Sends to everyone EXCEPT a group of client IDs.
/// NativeArrays can be trivially constructed using Allocator.Temp, making this an efficient
/// Group method if the group list is dynamically constructed.
/// </summary>
/// <param name="excludedClientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Not(NativeArray<ulong> excludedClientIds, RpcTargetUse use)
{
IGroupRpcTarget target;
if (m_NetworkManager.IsServer)
{
if (use == RpcTargetUse.Persistent)
{
target = new RpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedTargetGroup;
}
}
else
{
if (use == RpcTargetUse.Persistent)
{
target = new ProxyRpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedProxyRpcTargetGroup;
}
}
target.Clear();
using var asASet = new NativeHashSet<ulong>(excludedClientIds.Length, Allocator.Temp);
foreach (var clientId in excludedClientIds)
{
asASet.Add(clientId);
}
foreach (var clientId in m_NetworkManager.ConnectedClientsIds)
{
if (!asASet.Contains(clientId))
{
target.Add(clientId);
}
}
// If ServerIsHost, ConnectedClientIds already contains ServerClientId and this would duplicate it.
if (!m_NetworkManager.ServerIsHost && !asASet.Contains(NetworkManager.ServerClientId))
{
target.Add(NetworkManager.ServerClientId);
}
return target.Target;
}
/// <summary>
/// Sends to everyone EXCEPT a group of client IDs.
/// NativeList can be trivially constructed using Allocator.Temp, making this an efficient
/// Group method if the group list is dynamically constructed.
/// </summary>
/// <param name="excludedClientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Not(NativeList<ulong> excludedClientIds, RpcTargetUse use)
{
var asArray = excludedClientIds.AsArray();
return Not(asArray, use);
}
/// <summary>
/// Sends to everyone EXCEPT a group of client IDs.
/// Constructing arrays requires garbage collected allocations. This override is only recommended
/// if you either have no strict performance requirements, or have the group of client IDs cached so
/// it is not created each time.
/// </summary>
/// <param name="excludedClientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Not(ulong[] excludedClientIds, RpcTargetUse use)
{
return Not(new NativeArray<ulong>(excludedClientIds, Allocator.Temp), use);
}
/// <summary>
/// Sends to everyone EXCEPT a group of client IDs.
/// This accepts any IEnumerable type, such as List&lt;ulong&gt;, but cannot be called without
/// a garbage collected allocation (even if the type itself is a struct type, due to boxing).
/// This override is only recommended if you either have no strict performance requirements,
/// or have the group of client IDs cached so it is not created each time.
/// </summary>
/// <param name="excludedClientIds"></param>
/// <param name="use"><see cref="RpcTargetUse.Temp"/> will return a cached target, which should not be stored as it will
/// be overwritten in future calls to Not() or Group(). Do not call Dispose() on Temp targets.<br /><br /><see cref="RpcTargetUse.Persistent"/> will
/// return a new target, which can be stored, but should not be done frequently because it results in a GC allocation. You must call Dispose() on Persistent targets when you are done with them.</param>
/// <returns></returns>
public BaseRpcTarget Not<T>(T excludedClientIds, RpcTargetUse use) where T : IEnumerable<ulong>
{
IGroupRpcTarget target;
if (m_NetworkManager.IsServer)
{
if (use == RpcTargetUse.Persistent)
{
target = new RpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedTargetGroup;
}
}
else
{
if (use == RpcTargetUse.Persistent)
{
target = new ProxyRpcTargetGroup(m_NetworkManager);
}
else
{
target = m_CachedProxyRpcTargetGroup;
}
}
target.Clear();
using var asASet = new NativeHashSet<ulong>(m_NetworkManager.ConnectedClientsIds.Count, Allocator.Temp);
foreach (var clientId in excludedClientIds)
{
asASet.Add(clientId);
}
foreach (var clientId in m_NetworkManager.ConnectedClientsIds)
{
if (!asASet.Contains(clientId))
{
target.Add(clientId);
}
}
// If ServerIsHost, ConnectedClientIds already contains ServerClientId and this would duplicate it.
if (!m_NetworkManager.ServerIsHost && !asASet.Contains(NetworkManager.ServerClientId))
{
target.Add(NetworkManager.ServerClientId);
}
return target.Target;
}
private ProxyRpcTargetGroup m_CachedProxyRpcTargetGroup;
private RpcTargetGroup m_CachedTargetGroup;
private DirectSendRpcTarget m_CachedDirectSendTarget;
private ProxyRpcTarget m_CachedProxyRpcTarget;
}
}