OnDeserialize() public method

Virtual function to override to receive custom serialization data. The corresponding function to send serialization data is OnSerialize().
public OnDeserialize ( Mirror.NetworkReader reader, bool initialState ) : void
reader Mirror.NetworkReader Reader to read from the stream.
initialState bool True if being sent initial state.
return void
示例#1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal void OnDeserializeSafely(NetworkBehaviour comp, NetworkReader reader, bool initialState)
        {
            // extract data length and data safely, untouched by user code
            // -> returns empty array if length is 0, so .Length is always the proper length
            byte[] bytes = reader.ReadBytesAndSize();
            if (LogFilter.Debug)
            {
                Debug.Log("OnDeserializeSafely extracted: " + comp.name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + " length=" + bytes.Length);
            }

            // call OnDeserialize with a temporary reader, so that the
            // original one can't be messed with. we also wrap it in a
            // try-catch block so there's no way to mess up another
            // component's deserialization
            try
            {
                NetworkReader componentReader = new NetworkReader(bytes);
                comp.OnDeserialize(componentReader, initialState);
                if (componentReader.Position != componentReader.Length)
                {
                    Debug.LogWarning("OnDeserialize didn't read the full " + bytes.Length + " bytes for object:" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + ". Make sure that OnSerialize and OnDeserialize write/read the same amount of data in all cases.");
                }
            }
            catch (Exception e)
            {
                // show a detailed error and let the user know what went wrong
                Debug.LogError("OnDeserialize failed for: object=" + name + " component=" + comp.GetType() + " sceneId=" + m_SceneId + " length=" + bytes.Length + ". Possible Reasons:\n  * Do " + comp.GetType() + "'s OnSerialize and OnDeserialize calls write the same amount of data(" + bytes.Length + " bytes)? \n  * Was there an exception in " + comp.GetType() + "'s OnSerialize/OnDeserialize code?\n  * Are the server and client the exact same project?\n  * Maybe this OnDeserialize call was meant for another GameObject? The sceneIds can easily get out of sync if the Hierarchy was modified only in the client OR the server. Try rebuilding both.\n\n" + e.ToString());
            }
        }