//============ TinyNetIdentity Functions ============//

        /// <summary>
        /// Applies the initial state of an object (it's <see cref="TinyNetSyncVar"/>).
        /// </summary>
        /// <param name="tinyNetId">The <see cref="TinyNetIdentity"/>r.</param>
        /// <param name="position">The position.</param>
        /// <param name="initialState">The initial state.</param>
        /// <param name="networkID">The network identifier.</param>
        /// <param name="newGameObject">The new <see cref="GameObject"/>.</param>
        void ApplyInitialState(TinyNetIdentity tinyNetId, Vector3 position, byte[] initialState, int networkID, GameObject newGameObject)
        {
            if (!tinyNetId.gameObject.activeSelf)
            {
                tinyNetId.gameObject.SetActive(true);
            }

            tinyNetId.transform.position = position;

            tinyNetId.OnNetworkCreate();

            if (initialState != null && initialState.Length > 0)
            {
                var initialStateReader = new NetDataReader(initialState);
                tinyNetId.DeserializeAllTinyNetObjects(initialStateReader, true);
            }

            if (newGameObject == null)
            {
                return;
            }

            newGameObject.SetActive(true);
            tinyNetId.ReceiveNetworkID(networkID);
            AddTinyNetIdentityToList(tinyNetId);

            // If the object was spawned as part of the initial replication (s_TineNetObjectSpawnFinishedMessage.state == 0) it will have it's OnStartClient called by OnObjectSpawnFinished.
            if (_isSpawnFinished)
            {
                tinyNetId.OnStartClient();
            }
        }
Beispiel #2
0
        //============ TinyNetIdentity Functions ============//

        /// <summary>
        /// Applies the initial state of an object (it's <see cref="TinyNetSyncVar"/>).
        /// </summary>
        /// <param name="tinyNetId">The <see cref="TinyNetIdentity"/>r.</param>
        /// <param name="position">The position.</param>
        /// <param name="initialState">The initial state.</param>
        /// <param name="networkID">The network identifier.</param>
        /// <param name="newGameObject">The new <see cref="GameObject"/>.</param>
        void ApplyInitialState(TinyNetIdentity tinyNetId, Vector3 position, byte[] initialState, int networkID, GameObject newGameObject, ushort frameTick)
        {
            if (!tinyNetId.gameObject.activeSelf)
            {
                tinyNetId.gameObject.SetActive(true);
            }

            tinyNetId.transform.position = position;

            tinyNetId.OnNetworkCreate();

            if (newGameObject != null)
            {
                tinyNetId.ReceiveNetworkID(new TinyNetworkID(networkID));
            }

            if (initialState != null && initialState.Length > 0)
            {
                _stateUpdateReader.Clear();
                _stateUpdateReader.SetSource(initialState);
                _stateUpdateReader.SetFrameTick(frameTick);
                tinyNetId.TinyDeserialize(_stateUpdateReader, true);
            }

            // If this is null then this object already existed (already have a networkID and was registered in TinyNetScene), just apply the update to existing object
            if (newGameObject == null)
            {
                return;
            }

            newGameObject.SetActive(true);
            tinyNetId.ReceiveNetworkID(new TinyNetworkID(networkID));
            AddTinyNetIdentityToList(tinyNetId);

            // If the object was spawned as part of the initial replication (s_TineNetObjectSpawnFinishedMessage.state == 0) it will have it's OnStartClient called by OnObjectSpawnFinished.
            if (_isSpawnFinished)
            {
                tinyNetId.OnStartClient();
            }
        }
        /// <summary>
        /// Destroys the object.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/> of the object.</param>
        /// <param name="destroyServerObject">if set to <c>true</c> destroy the object on server too.</param>
        public void DestroyObject(TinyNetIdentity tni, bool destroyServerObject)
        {
            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("DestroyObject instance:" + tni.TinyInstanceID);
            }

            /*if (_localIdentityObjects.ContainsKey(tni.TinyInstanceID.NetworkID)) {
             *      _localIdentityObjects.Remove(tni.TinyInstanceID.NetworkID);
             * }*/
            RemoveTinyNetIdentityFromList(tni);

            if (tni.ConnectionToOwnerClient != null)
            {
                tni.ConnectionToOwnerClient.RemoveOwnedObject(tni);
            }

            TinyNetObjectDestroyMessage msg = new TinyNetObjectDestroyMessage();

            msg.networkID = tni.TinyInstanceID.NetworkID;
            SendMessageByChannelToAllObserversOf(tni, msg, DeliveryMethod.ReliableOrdered);

            for (int i = 0; i < tinyNetConns.Count; i++)
            {
                tinyNetConns[i].HideObjectToConnection(tni, true);
            }

            /*if (TinyNetGameManager.instance.isListenServer) {
             *      tni.OnNetworkDestroy();
             * }*/

            tni.OnNetworkDestroy();
            // when unspawning, dont destroy the server's object
            if (destroyServerObject)
            {
                Object.Destroy(tni.gameObject);
            }

            tni.ReceiveNetworkID(new TinyNetworkID(-1));
        }