SerializeView() public method

public SerializeView ( PhotonStream, stream, PhotonMessageInfo, info ) : void
stream PhotonStream,
info PhotonMessageInfo,
return void
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private Hashtable OnSerializeWrite(PhotonView view)
    {
        PhotonStream pStream = new PhotonStream( true, null );
        PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );

        // each view creates a list of values that should be sent
        view.SerializeView( pStream, info );

        if( pStream.Count == 0 )
        {
            return null;
        }

        object[] dataArray = pStream.data.ToArray();

        if (view.synchronization == ViewSynchronization.UnreliableOnChange)
        {
            if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
            {
                if (view.mixedModeIsReliable)
                {
                    return null;
                }

                view.mixedModeIsReliable = true;
                view.lastOnSerializeDataSent = dataArray;
            }
            else
            {
                view.mixedModeIsReliable = false;
                view.lastOnSerializeDataSent = dataArray;
            }
        }

        // EVDATA:
        // 0=View ID (an int, never compressed cause it's not in the data)
        // 1=data of observed type (different per type of observed object)
        // 2=compressed data (in this case, key 1 is empty)
        // 3=list of values that are actually null (if something was changed but actually IS null)
        Hashtable evData = new Hashtable();
        evData[(byte)0] = (int)view.viewID;
        evData[(byte)1] = dataArray;    // this is the actual data (script or observed object)


        if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
        {
            // compress content of data set (by comparing to view.lastOnSerializeDataSent)
            // the "original" dataArray is NOT modified by DeltaCompressionWrite
            // if something was compressed, the evData key 2 and 3 are used (see above)
            bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);

            // buffer the full data set (for next compression)
            view.lastOnSerializeDataSent = dataArray;

            if (!somethingLeftToSend)
            {
                return null;
            }
        }

        return evData;
    }
Exemplo n.º 2
0
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private object[] OnSerializeWrite(PhotonView view)
    {
        if (view.synchronization == ViewSynchronization.Off)
        {
            return null;
        }

        // each view creates a list of values that should be sent
        PhotonMessageInfo info = new PhotonMessageInfo(this.LocalPlayer, PhotonNetwork.ServerTimestamp, view);
        this.pStream.ResetWriteStream();
        this.pStream.SendNext((int)view.viewID);
        this.pStream.SendNext(false);
        this.pStream.SendNext(null);
        view.SerializeView(this.pStream, info);

        // check if there are actual values to be sent (after the "header" of viewId, (bool)compressed and (int[])nullValues)
        if (this.pStream.Count <= SyncFirstValue)
        {
            return null;
        }
        if (view.synchronization == ViewSynchronization.Unreliable)
        {
            return this.pStream.ToArray();
        }

        // ViewSynchronization: Off, Unreliable, UnreliableOnChange, ReliableDeltaCompressed

        object[] currentValues = this.pStream.ToArray();
        if (view.synchronization == ViewSynchronization.UnreliableOnChange)
        {
            if (AlmostEquals(currentValues, view.lastOnSerializeDataSent))
            {
                if (view.mixedModeIsReliable)
                {
                    return null;
                }

                view.mixedModeIsReliable = true;
                view.lastOnSerializeDataSent = currentValues;
            }
            else
            {
                view.mixedModeIsReliable = false;
                view.lastOnSerializeDataSent = currentValues;
            }

            return currentValues;
        }

        if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
        {
            // compress content of data set (by comparing to view.lastOnSerializeDataSent)
            // the "original" dataArray is NOT modified by DeltaCompressionWrite
            object[] dataToSend = this.DeltaCompressionWrite(view.lastOnSerializeDataSent, currentValues);

            // cache the values that were written this time (not the compressed values)
            view.lastOnSerializeDataSent = currentValues;

            return dataToSend;
        }

        return null;
    }