void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            int    pid  = 0;
            string path = null;

            if (stream.isWriting)
            {
                NetworkReference nref = NetworkReference.FromTransform(transform.parent);
                pid  = nref.parentHandleId;
                path = nref.pathFromParent;

                // Send update

                stream.Serialize(ref pid);
                stream.Serialize(ref path);
            }
            else
            {
                // Receive updated state information

                stream.Serialize(ref pid);
                stream.Serialize(ref path);

                newState.timestamp = info.timestamp;
                newState.parentId  = pid;
                newState.path      = path;
            }
        }
Exemple #2
0
 public override void Apply()
 {
     var actualNor = NetworkReference.FromTransform(transform.parent);
     if (actualNor != parentNetRef) {
         //Debug.Log("Reparenting from " + actualNor + " to " + parentNetRef);
         GameObject newParent = parentNetRef.FindObject();
         //Debug.Log("New parent " + newParent);
         transform.parent = newParent != null ? newParent.transform : null;
     }
     base.Apply();
 }
        void UpdateParent()
        {
            var actualNref = NetworkReference.FromTransform(transform.parent);
            var newNref    = NetworkReference.FromIdAndPath(newState.parentId, newState.path);

            if (actualNref != newNref)
            {
                //Debug.Log("Reparenting from " + actualNref + " to " + newNref);
                GameObject newParent = newNref.FindObject();
                //Debug.Log("New parent " + newParent);
                transform.parent = newParent != null ? newParent.transform : null;
            }
        }
Exemple #4
0
            public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
            {
                int        pid   = 0;
                string     path  = null;
                Vector3    pos   = Vector3.zero;
                Quaternion rot   = Quaternion.identity;
                Vector3    scale = Vector3.one;
                Vector3    v     = Vector3.zero;
                Vector3    angv  = Vector3.zero;

                if (stream.isWriting)
                {
                    if (component is Transform)
                    {
                        Transform        transform = (Transform)component;
                        NetworkReference nref      = NetworkReference.FromTransform(transform.parent);
                        pid  = nref.parentHandleId;
                        path = nref.pathFromParent;
                        if (nit.useLocalValues)
                        {
                            pos = transform.localPosition;
                            rot = transform.localRotation;
                        }
                        else
                        {
                            pos = transform.position;
                            rot = transform.rotation;
                        }
                        scale = transform.localScale;
                        if (!nit.onChangeOnly || slotsUsed == 0 || hasChanged(pid, path, pos, rot, scale, v, angv))
                        {
                            // Send update
                            if (nit.parent)
                            {
                                stream.Serialize(ref pid);
                                stream.Serialize(ref path);
                            }
                            if (nit.position != UpdateMode.None)
                            {
                                stream.Serialize(ref pos);
                            }
                            if (nit.rotation != UpdateMode.None)
                            {
                                stream.Serialize(ref rot);
                            }
                            if (nit.scale != UpdateMode.None)
                            {
                                stream.Serialize(ref scale);
                            }
                        }
                    }
                    else if (component is Rigidbody)
                    {
                        Rigidbody rbody = (Rigidbody)component;
                        v    = rbody.velocity;
                        angv = rbody.angularVelocity;
                        if (!nit.onChangeOnly || slotsUsed == 0 || hasChanged(pid, path, pos, rot, scale, v, angv))
                        {
                            // Send update
                            if (nit.velocity != UpdateMode.None)
                            {
                                stream.Serialize(ref v);
                            }
                            if (nit.angularVelocity != UpdateMode.None)
                            {
                                stream.Serialize(ref angv);
                            }
                        }
                    }
                    if (nit.onChangeOnly)
                    {
                        // Keep a copy
                        State state;
                        state.timestamp = info.timestamp;
                        state.parentId  = pid;
                        state.path      = path;
                        state.pos       = pos;
                        state.rot       = rot;
                        state.scale     = scale;
                        state.v         = v;
                        state.angv      = angv;
                        states[0]       = state;
                        slotsUsed       = 1;
                    }
                }
                else
                {
                    // Receive updated state information
                    if (component is Transform)
                    {
                        Transform transform = component.transform;
                        if (nit.parent)
                        {
                            stream.Serialize(ref pid);
                            stream.Serialize(ref path);
                        }
                        if (nit.position != UpdateMode.None)
                        {
                            stream.Serialize(ref pos);
                        }
                        if (nit.rotation != UpdateMode.None)
                        {
                            stream.Serialize(ref rot);
                        }
                        if (nit.scale != UpdateMode.None)
                        {
                            stream.Serialize(ref scale);
                        }
                    }
                    else if (component is Rigidbody)
                    {
                        Rigidbody rbody = (Rigidbody)component;
                        if (nit.velocity != UpdateMode.None)
                        {
                            stream.Serialize(ref v);
                        }
                        if (nit.angularVelocity != UpdateMode.None)
                        {
                            stream.Serialize(ref angv);
                        }
                    }
                    // Make sure the update was received in the correct order
                    // If not we simply drop it
                    if (slotsUsed == 0 || states[lastRcvdSlot].timestamp <= info.timestamp)
                    {
                        // Save currect received state in the next free slot
                        State state;
                        state.timestamp      = info.timestamp;
                        state.parentId       = pid;
                        state.path           = path;
                        state.pos            = pos;
                        state.rot            = rot;
                        state.scale          = scale;
                        state.v              = v;
                        state.angv           = angv;
                        states[nextFreeSlot] = state;
                        // Increment nextSlot wrapping around to 0 when getting to the end
                        lastRcvdSlot = nextFreeSlot;
                        nextFreeSlot = (nextFreeSlot + 1) % states.Length;
                        // Increment the number of unhandled updates currently in the buffer
                        slotsUsed = Mathf.Min(slotsUsed + 1, states.Length);
                    }
                }
            }
Exemple #5
0
 public override void Obtain()
 {
     parentNetRef = NetworkReference.FromTransform(transform.parent);
 }