NetworkOwnerChecker.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace Mirror
  5. {
  6. /// <summary>
  7. /// Component that limits visibility of networked objects to the authority client.
  8. /// <para>Any object with this component on it will only be visible to the client that has been assigned authority for it.</para>
  9. /// <para>This would be used for spawning a non-player networked object for single client to interact with, e.g. in-game puzzles.</para>
  10. /// </summary>
  11. // Deprecated 2021-02-17
  12. [Obsolete(NetworkVisibilityObsoleteMessage.Message)]
  13. [DisallowMultipleComponent]
  14. [AddComponentMenu("Network/NetworkOwnerChecker")]
  15. [RequireComponent(typeof(NetworkIdentity))]
  16. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-owner-checker")]
  17. public class NetworkOwnerChecker : NetworkVisibility
  18. {
  19. /// <summary>
  20. /// Callback used by the visibility system to determine if an observer (player) can see this object.
  21. /// <para>If this function returns true, the network connection will be added as an observer.</para>
  22. /// </summary>
  23. /// <param name="conn">Network connection of a player.</param>
  24. /// <returns>True if the client is the owner of this object.</returns>
  25. public override bool OnCheckObserver(NetworkConnection conn)
  26. {
  27. // Debug.Log($"OnCheckObserver {netIdentity.connectionToClient} {conn}");
  28. return (netIdentity.connectionToClient == conn);
  29. }
  30. /// <summary>
  31. /// Callback used by the visibility system to (re)construct the set of observers that can see this object.
  32. /// </summary>
  33. /// <param name="observers">The new set of observers for this object.</param>
  34. /// <param name="initialize">True if the set of observers is being built for the first time.</param>
  35. public override void OnRebuildObservers(HashSet<NetworkConnection> observers, bool initialize)
  36. {
  37. // Do nothing here because the authority client is always added as an observer internally.
  38. }
  39. }
  40. }