#if UNITY_2020_3_OR_NEWER using FishNet.CodeAnalysis.Annotations; #endif using FishNet.Component.ColliderRollback; using FishNet.Connection; using FishNet.Managing; using FishNet.Managing.Client; using FishNet.Managing.Logging; using FishNet.Managing.Observing; using FishNet.Managing.Scened; using FishNet.Managing.Server; using FishNet.Managing.Timing; using FishNet.Managing.Transporting; using System; using UnityEngine; namespace FishNet.Object { public abstract partial class NetworkBehaviour : MonoBehaviour { /// /// True if the NetworkObject for this NetworkBehaviour is deinitializing. /// public bool IsDeinitializing => _networkObjectCache.IsDeinitializing; [Obsolete("Use IsDeinitializing instead.")] public bool Deinitializing => IsDeinitializing; //Remove on 2023/01/01. /// /// NetworkManager for this object. /// public NetworkManager NetworkManager => _networkObjectCache.NetworkManager; /// /// ServerManager for this object. /// public ServerManager ServerManager => _networkObjectCache.ServerManager; /// /// ClientManager for this object. /// public ClientManager ClientManager => _networkObjectCache.ClientManager; /// /// ObserverManager for this object. /// public ObserverManager ObserverManager => _networkObjectCache.ObserverManager; /// /// TransportManager for this object. /// public TransportManager TransportManager => _networkObjectCache.TransportManager; /// /// TimeManager for this object. /// public TimeManager TimeManager => _networkObjectCache.TimeManager; /// /// SceneManager for this object. /// public SceneManager SceneManager => _networkObjectCache.SceneManager; /// /// RollbackManager for this object. /// public RollbackManager RollbackManager => _networkObjectCache.RollbackManager; /// /// True if the client is active and authenticated. /// public bool IsClient => _networkObjectCache.IsClient; /// /// True if only the client is active and authenticated. /// public bool IsClientOnly => _networkObjectCache.IsClientOnly; /// /// True if server is active. /// public bool IsServer => _networkObjectCache.IsServer; /// /// True if only the server is active. /// public bool IsServerOnly => _networkObjectCache.IsServerOnly; /// /// True if client and server are active. /// public bool IsHost => _networkObjectCache.IsHost; /// /// True if client nor server are active. /// public bool IsOffline => _networkObjectCache.IsOffline; /// /// True if the local client is the owner of this object. /// #if UNITY_2020_3_OR_NEWER [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartServer")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Awake")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Start")] #endif public bool IsOwner => _networkObjectCache.IsOwner; /// /// Owner of this object. /// public NetworkConnection Owner { get { //Ensures a null Owner is never returned. if (_networkObjectCache == null) return FishNet.Managing.NetworkManager.EmptyConnection; return _networkObjectCache.Owner; } } /// /// ClientId for this NetworkObject owner. /// public int OwnerId => _networkObjectCache.OwnerId; /// /// Unique Id for this _networkObjectCache. This does not represent the object owner. /// public int ObjectId => _networkObjectCache.ObjectId; /// /// The local connection of the client calling this method. /// public NetworkConnection LocalConnection => _networkObjectCache.LocalConnection; /// /// Returns if a connection is the owner of this object. /// Internal use. /// /// /// public bool CompareOwner(NetworkConnection connection) { return (_networkObjectCache.Owner == connection); } /// /// Despawns this _networkObjectCache. Can only be called on the server. /// /// Overrides the default DisableOnDespawn value for this single despawn. Scene objects will never be destroyed. public void Despawn(bool? disableOnDespawnOverride = null) { if (!IsNetworkObjectNull(true)) _networkObjectCache.Despawn(disableOnDespawnOverride); } /// /// Spawns an object over the network. Can only be called on the server. /// /// GameObject instance to spawn. /// Connection to give ownership to. public void Spawn(GameObject go, NetworkConnection ownerConnection = null) { if (IsNetworkObjectNull(true)) return; _networkObjectCache.Spawn(go, ownerConnection); } /// /// Returns if NetworkObject is null. /// /// True to throw a warning if null. /// private bool IsNetworkObjectNull(bool warn) { bool isNull = (_networkObjectCache == null); if (isNull && warn) { if (NetworkManager.CanLog(LoggingType.Warning)) Debug.LogWarning($"NetworkObject is null. This can occur if this object is not spawned, or initialized yet."); } return isNull; } /// /// Removes ownership from all clients. /// public void RemoveOwnership() { _networkObjectCache.GiveOwnership(null, true); } /// /// Gives ownership to newOwner. /// /// public void GiveOwnership(NetworkConnection newOwner) { _networkObjectCache.GiveOwnership(newOwner, true); } } }