// serialize all components (or only dirty ones if not initial state) // -> returns TRUE if any date other than dirtyMask was written! internal bool OnSerializeAllSafely(NetworkBehaviour[] components, NetworkWriter writer, bool initialState) { if (components.Length > 64) { if (LogFilter.logError) { Debug.LogError("Only 64 NetworkBehaviour components are allowed for NetworkIdentity: " + name + " because of the dirtyComponentMask"); } return(false); } // loop through all components only once and then write dirty+payload into the writer afterwards ulong dirtyComponentsMask = 0L; NetworkWriter payload = new NetworkWriter(); for (int i = 0; i < components.Length; ++i) { // is this component dirty? // -> always serialize if initialState so all components are included in spawn packet // -> note: IsDirty() is false if the component isn't dirty or sendInterval isn't elapsed yet NetworkBehaviour comp = m_NetworkBehaviours[i]; if (initialState || comp.IsDirty()) { // set bit #i to 1 in dirty mask dirtyComponentsMask |= (ulong)(1L << i); // serialize the data if (LogFilter.logDebug) { Debug.Log("OnSerializeAllSafely: " + name + " -> " + comp.GetType() + " initial=" + initialState); } OnSerializeSafely(comp, payload, initialState); // Clear dirty bits only if we are synchronizing data and not sending a spawn message. // This preserves the behavior in HLAPI if (!initialState) { comp.ClearAllDirtyBits(); } } } // did we write anything? then write dirty, payload and return true if (dirtyComponentsMask != 0L) { byte[] payloadBytes = payload.ToArray(); writer.WritePackedUInt64(dirtyComponentsMask); // WritePacked64 so we don't write full 8 bytes if we don't have to writer.Write(payloadBytes, 0, payloadBytes.Length); return(true); } // didn't write anything, return false return(false); }
private ulong GetDirtyMask(NetworkBehaviour[] components, bool initialState) { // loop through all components only once and then write dirty+payload into the writer afterwards ulong dirtyComponentsMask = 0L; for (int i = 0; i < components.Length; ++i) { NetworkBehaviour comp = components[i]; if (initialState || comp.IsDirty()) { dirtyComponentsMask |= (ulong)(1L << i); } } return(dirtyComponentsMask); }