/// <summary>
        /// Spawns the <paramref name="identity"/> and assigns <paramref name="owner"/> to be it's owner
        /// </summary>
        public void Spawn(NetworkIdentity identity, INetworkPlayer owner)
        {
            if (!Server || !Server.Active)
            {
                throw new InvalidOperationException("NetworkServer is not active. Cannot spawn objects without an active server.");
            }

            ThrowIfPrefab(identity.gameObject);

            identity.Owner = owner;

            identity.SetServerValues(Server, this);

            // special case to make sure hasAuthority is set
            // on start server in host mode
            if (owner == Server.LocalPlayer)
            {
                identity.HasAuthority = true;
            }

            if (!identity.IsSpawned)
            {
                // the object has not been spawned yet
                identity.NetId = GetNextNetworkId();
                identity.StartServer();
                Server.World.AddIdentity(identity.NetId, identity);
            }

            if (logger.LogEnabled())
            {
                logger.Log($"SpawnObject instance ID {identity.NetId} asset ID {identity.PrefabHash:X}");
            }

            identity.RebuildObservers(true);
        }
        /// <summary>
        /// <para>When an <see cref="AddCharacterMessage"/> message handler has received a request from a player, the server calls this to associate the player object with the connection.</para>
        /// <para>When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail.</para>
        /// </summary>
        /// <param name="player">Connection which is adding the player.</param>
        /// <param name="identity">Player object spawned for the player.</param>
        /// <exception cref="ArgumentException">NetworkIdentity must not be null.</exception>
        public void AddCharacter(INetworkPlayer player, NetworkIdentity identity)
        {
            // cannot have an existing player object while trying to Add another.
            if (player.HasCharacter)
            {
                throw new ArgumentException("AddCharacter can only be called if the player does not already have a character");
            }

            // make sure we have a controller before we call SetClientReady
            // because the observers will be rebuilt only if we have a controller
            player.Identity = identity;

            identity.SetServerValues(Server, this);

            // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
            identity.SetClientOwner(player);

            // special case, we are in host mode, set hasAuthority to true so that all overrides see it
            if (player == Server.LocalPlayer)
            {
                identity.HasAuthority = true;
                Server.LocalClient.Player.Identity = identity;
            }

            // spawn any new visible scene objects
            SpawnVisibleObjects(player);

            if (logger.LogEnabled())
            {
                logger.Log($"Adding new playerGameObject object netId: {identity.NetId} asset ID {identity.PrefabHash:X}");
            }

            Respawn(identity);
        }
        /// <summary>
        /// <para>When an <see cref="AddCharacterMessage"/> message handler has received a request from a player, the server calls this to associate the player object with the connection.</para>
        /// <para>When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail.</para>
        /// </summary>
        /// <param name="player">Connection which is adding the player.</param>
        /// <param name="identity">Player object spawned for the player.</param>
        /// <exception cref="ArgumentException">NetworkIdentity must not be null.</exception>
        public void AddCharacter(INetworkPlayer player, NetworkIdentity identity)
        {
            // cannot have an existing player object while trying to Add another.
            if (player.Identity != null)
            {
                throw new ArgumentException("AddPlayer: player object already exists");
            }

            // make sure we have a controller before we call SetClientReady
            // because the observers will be rebuilt only if we have a controller
            player.Identity = identity;

            identity.SetServerValues(Server, this);

            // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
            identity.SetClientOwner(player);

            // special case,  we are in host mode,  set hasAuthority to true so that all overrides see it
            if (player == Server.LocalPlayer)
            {
                identity.HasAuthority = true;
                Server.LocalClient.Player.Identity = identity;
            }

            // set ready if not set yet
            SetClientReady(player);

            if (logger.LogEnabled())
            {
                logger.Log("Adding new playerGameObject object netId: " + identity.NetId + " asset ID " + identity.AssetId);
            }

            Respawn(identity);
        }
Exemple #4
0
        internal void SpawnObject(GameObject obj, INetworkPlayer ownerPlayer)
        {
            if (!Server || !Server.Active)
            {
                throw new InvalidOperationException("NetworkServer is not active. Cannot spawn objects without an active server.");
            }

            NetworkIdentity identity = obj.GetComponent <NetworkIdentity>();

            if (identity is null)
            {
                throw new InvalidOperationException("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj);
            }

            identity.ConnectionToClient = ownerPlayer;

            identity.SetServerValues(Server, this);

            // special case to make sure hasAuthority is set
            // on start server in host mode
            if (ownerPlayer == Server.LocalPlayer)
            {
                identity.HasAuthority = true;
            }

            if (identity.NetId == 0)
            {
                // the object has not been spawned yet
                identity.NetId = GetNextNetworkId();
                identity.StartServer();
                Server.World.AddIdentity(identity.NetId, identity);
            }

            if (logger.LogEnabled())
            {
                logger.Log("SpawnObject instance ID " + identity.NetId + " asset ID " + identity.AssetId);
            }

            identity.RebuildObservers(true);
        }