Created
February 22, 2014 17:21
-
-
Save cubed2d/9158415 to your computer and use it in GitHub Desktop.
NetworkBehaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using System; | |
| public abstract class NetworkBehaviour<T> : MonoBehaviour where T : INetworkPacket, new() | |
| { | |
| public T Packet = new T(); | |
| public T LastPacket = new T(); | |
| public NetworkingManager Manager; | |
| public bool IsEchoEnabled = true; | |
| public int SendPeriod = 1; | |
| private int periodCount = 0; | |
| public NetworkView View; | |
| public new Transform transform; | |
| void Awake() | |
| { | |
| // Find network manager... | |
| Manager = GameObject.FindObjectOfType(typeof(NetworkingManager)) as NetworkingManager; | |
| View = base.networkView; | |
| transform = base.transform; | |
| } | |
| public void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) | |
| { | |
| var networkView = info.networkView; // make sure were looking at the correct network view... | |
| // Switch the packets round | |
| if (stream.isWriting) | |
| { | |
| // If this NetworkBehaviour needs to echo to all other clients, Write out the data without updating it. | |
| if (Network.isServer && !networkView.isMine) | |
| { | |
| if (IsEchoEnabled) | |
| { | |
| if (SendPeriod == 1) | |
| { | |
| // were sending every frame | |
| Packet.Serialize(stream, info); | |
| } | |
| else | |
| { | |
| // were sending once every SendPeriod frames | |
| periodCount++; | |
| if (periodCount >= SendPeriod) | |
| { | |
| periodCount = 0; | |
| Packet.Serialize(stream, info); | |
| } | |
| } | |
| } | |
| } | |
| else | |
| { | |
| FlipPacket(info.timestamp); | |
| OnUpdatePacket(info); | |
| Packet.Serialize(stream, info); | |
| } | |
| } | |
| else | |
| { | |
| // if (Network.isServer && !networkView.isMine) in this case, the server has recieved a packet from another player, it needs to be stored and | |
| // forwarded on to other players! Unity will call OnSerializeNetworkView(...) again for this to be written. | |
| FlipPacket(info.timestamp); | |
| Packet.Serialize(stream, info); | |
| OnNewPacket(info); | |
| } | |
| } | |
| private void FlipPacket(double timestamp) | |
| { | |
| var next = LastPacket; | |
| LastPacket = Packet; | |
| Packet = next; | |
| Packet.NetworkTime = timestamp; | |
| } | |
| protected virtual void OnNewPacket(NetworkMessageInfo info) { } | |
| protected abstract void OnUpdatePacket(NetworkMessageInfo info); | |
| } | |
| public interface INetworkPacket | |
| { | |
| double NetworkTime { get; set; } | |
| void Serialize(BitStream stream, NetworkMessageInfo info); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| /// <summary> | |
| /// This manages synchronising position/rotation on objects that do not have a rigid body and only need simple | |
| /// smothing to look correct. | |
| /// </summary> | |
| public class SimplePositionSyncronization : NetworkBehaviour<PositionRotationPacket> | |
| { | |
| public bool CalculateLerpTime = true; | |
| public float PositionLerpTime = 0.1f; | |
| // Use this for initialization | |
| void Start () | |
| { | |
| if (CalculateLerpTime) | |
| { | |
| PositionLerpTime = 1f / Network.sendRate; // help me i cant maths | |
| } | |
| SendPeriod = 60; | |
| } | |
| // Update is called once per frame | |
| protected void Update () | |
| { | |
| if (Network.isClient) | |
| { | |
| transform.position = Vector3.Lerp(transform.position, Packet.Position, PositionLerpTime); | |
| transform.rotation = Quaternion.Slerp(transform.rotation, Packet.Rotation, PositionLerpTime); | |
| } | |
| } | |
| protected override void OnUpdatePacket(NetworkMessageInfo info) | |
| { | |
| Packet.Position = transform.position; | |
| Packet.Rotation = transform.rotation; | |
| } | |
| } | |
| public class PositionRotationPacket : INetworkPacket | |
| { | |
| public double NetworkTime { get; set; } | |
| public Vector3 Position; | |
| public Quaternion Rotation; | |
| public void Serialize(BitStream stream, NetworkMessageInfo info) | |
| { | |
| stream.Serialize(ref Position); | |
| stream.Serialize(ref Rotation); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment