mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
NetworkAnimator moved to components. Replace messages with Cmds/Rpcs (#294)
This commit is contained in:
parent
853083a132
commit
aedf976d4f
@ -96,16 +96,10 @@ void FixedUpdate()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var animMsg = new AnimationMessage();
|
|
||||||
animMsg.netId = netId;
|
|
||||||
animMsg.stateHash = stateHash;
|
|
||||||
animMsg.normalizedTime = normalizedTime;
|
|
||||||
|
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
WriteParameters(writer, false);
|
WriteParameters(writer, false);
|
||||||
animMsg.parameters = writer.ToArray();
|
|
||||||
|
|
||||||
SendMessage((short)MsgType.Animation, animMsg);
|
SendAnimationMessage(stateHash, normalizedTime, writer.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
|
bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
|
||||||
@ -149,29 +143,34 @@ void CheckSendRate()
|
|||||||
{
|
{
|
||||||
m_SendTimer = Time.time + syncInterval;
|
m_SendTimer = Time.time + syncInterval;
|
||||||
|
|
||||||
var animMsg = new AnimationParametersMessage();
|
|
||||||
animMsg.netId = netId;
|
|
||||||
|
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
WriteParameters(writer, true);
|
WriteParameters(writer, true);
|
||||||
animMsg.parameters = writer.ToArray();
|
|
||||||
|
|
||||||
SendMessage((short)MsgType.AnimationParameters, animMsg);
|
SendAnimationParametersMessage(writer.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendMessage(short type, MessageBase msg)
|
void SendAnimationMessage(int stateHash, float normalizedTime, byte[] parameters)
|
||||||
{
|
{
|
||||||
if (isServer)
|
if (isServer)
|
||||||
{
|
{
|
||||||
NetworkServer.SendToReady(gameObject, type, msg);
|
RpcOnAnimationClientMessage(stateHash, normalizedTime, parameters);
|
||||||
}
|
}
|
||||||
else
|
else if (ClientScene.readyConnection != null)
|
||||||
{
|
{
|
||||||
if (ClientScene.readyConnection != null)
|
CmdOnAnimationServerMessage(stateHash, normalizedTime, parameters);
|
||||||
{
|
}
|
||||||
ClientScene.readyConnection.Send(type, msg);
|
}
|
||||||
}
|
|
||||||
|
void SendAnimationParametersMessage(byte[] parameters)
|
||||||
|
{
|
||||||
|
if (isServer)
|
||||||
|
{
|
||||||
|
RpcOnAnimationParametersClientMessage(parameters);
|
||||||
|
}
|
||||||
|
else if (ClientScene.readyConnection != null)
|
||||||
|
{
|
||||||
|
CmdOnAnimationParametersServerMessage(parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +196,7 @@ void SetRecvTrackingParam(string p, int i)
|
|||||||
if (i == 5) param5 = p;
|
if (i == 5) param5 = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void HandleAnimMsg(AnimationMessage msg, NetworkReader reader)
|
internal void HandleAnimMsg(int stateHash, float normalizedTime, NetworkReader reader)
|
||||||
{
|
{
|
||||||
if (hasAuthority)
|
if (hasAuthority)
|
||||||
return;
|
return;
|
||||||
@ -205,15 +204,15 @@ internal void HandleAnimMsg(AnimationMessage msg, NetworkReader reader)
|
|||||||
// usually transitions will be triggered by parameters, if not, play anims directly.
|
// usually transitions will be triggered by parameters, if not, play anims directly.
|
||||||
// NOTE: this plays "animations", not transitions, so any transitions will be skipped.
|
// NOTE: this plays "animations", not transitions, so any transitions will be skipped.
|
||||||
// NOTE: there is no API to play a transition(?)
|
// NOTE: there is no API to play a transition(?)
|
||||||
if (msg.stateHash != 0)
|
if (stateHash != 0)
|
||||||
{
|
{
|
||||||
m_Animator.Play(msg.stateHash, 0, msg.normalizedTime);
|
m_Animator.Play(stateHash, 0, normalizedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadParameters(reader, false);
|
ReadParameters(reader, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void HandleAnimParamsMsg(AnimationParametersMessage msg, NetworkReader reader)
|
internal void HandleAnimParamsMsg(NetworkReader reader)
|
||||||
{
|
{
|
||||||
if (hasAuthority)
|
if (hasAuthority)
|
||||||
return;
|
return;
|
||||||
@ -332,140 +331,67 @@ public void SetTrigger(string triggerName)
|
|||||||
|
|
||||||
public void SetTrigger(int hash)
|
public void SetTrigger(int hash)
|
||||||
{
|
{
|
||||||
var animMsg = new AnimationTriggerMessage();
|
|
||||||
animMsg.netId = netId;
|
|
||||||
animMsg.hash = hash;
|
|
||||||
|
|
||||||
if (hasAuthority && localPlayerAuthority)
|
if (hasAuthority && localPlayerAuthority)
|
||||||
{
|
{
|
||||||
if (NetworkClient.allClients.Count > 0)
|
if (NetworkClient.allClients.Count > 0 && ClientScene.readyConnection != null)
|
||||||
{
|
{
|
||||||
var client = ClientScene.readyConnection;
|
CmdOnAnimationTriggerServerMessage(hash);
|
||||||
if (client != null)
|
|
||||||
{
|
|
||||||
client.Send((short)MsgType.AnimationTrigger, animMsg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isServer && !localPlayerAuthority)
|
if (isServer && !localPlayerAuthority)
|
||||||
{
|
{
|
||||||
NetworkServer.SendToReady(gameObject, (short)MsgType.AnimationTrigger, animMsg);
|
RpcOnAnimationTriggerClientMessage(hash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------ server message handlers -------------------
|
// ------------------ server message handlers -------------------
|
||||||
|
|
||||||
internal static void OnAnimationServerMessage(NetworkMessage netMsg)
|
[Command]
|
||||||
|
void CmdOnAnimationServerMessage(int stateHash, float normalizedTime, byte[] parameters)
|
||||||
{
|
{
|
||||||
AnimationMessage msg = netMsg.ReadMessage<AnimationMessage>();
|
if (LogFilter.Debug) { Debug.Log("OnAnimationMessage for netId=" + netId); }
|
||||||
if (LogFilter.Debug) { Debug.Log("OnAnimationMessage for netId=" + msg.netId + " conn=" + netMsg.conn); }
|
|
||||||
|
|
||||||
GameObject go = NetworkServer.FindLocalObject(msg.netId);
|
// handle and broadcast
|
||||||
if (go == null)
|
HandleAnimMsg(stateHash, normalizedTime, new NetworkReader(parameters));
|
||||||
{
|
RpcOnAnimationClientMessage(stateHash, normalizedTime, parameters);
|
||||||
return;
|
|
||||||
}
|
|
||||||
NetworkAnimator animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
NetworkReader reader = new NetworkReader(msg.parameters);
|
|
||||||
animSync.HandleAnimMsg(msg, reader);
|
|
||||||
|
|
||||||
NetworkServer.SendToReady(go, (short)MsgType.Animation, msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void OnAnimationParametersServerMessage(NetworkMessage netMsg)
|
[Command]
|
||||||
|
void CmdOnAnimationParametersServerMessage(byte[] parameters)
|
||||||
{
|
{
|
||||||
AnimationParametersMessage msg = netMsg.ReadMessage<AnimationParametersMessage>();
|
// handle and broadcast
|
||||||
|
HandleAnimParamsMsg(new NetworkReader(parameters));
|
||||||
if (LogFilter.Debug) { Debug.Log("OnAnimationParametersMessage for netId=" + msg.netId + " conn=" + netMsg.conn); }
|
RpcOnAnimationParametersClientMessage(parameters);
|
||||||
|
|
||||||
GameObject go = NetworkServer.FindLocalObject(msg.netId);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
NetworkAnimator animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
NetworkReader reader = new NetworkReader(msg.parameters);
|
|
||||||
animSync.HandleAnimParamsMsg(msg, reader);
|
|
||||||
NetworkServer.SendToReady(go, (short)MsgType.AnimationParameters, msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void OnAnimationTriggerServerMessage(NetworkMessage netMsg)
|
[Command]
|
||||||
|
void CmdOnAnimationTriggerServerMessage(int hash)
|
||||||
{
|
{
|
||||||
AnimationTriggerMessage msg = netMsg.ReadMessage<AnimationTriggerMessage>();
|
// handle and broadcast
|
||||||
if (LogFilter.Debug) { Debug.Log("OnAnimationTriggerMessage for netId=" + msg.netId + " conn=" + netMsg.conn); }
|
HandleAnimTriggerMsg(hash);
|
||||||
|
RpcOnAnimationTriggerClientMessage(hash);
|
||||||
GameObject go = NetworkServer.FindLocalObject(msg.netId);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
NetworkAnimator animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
animSync.HandleAnimTriggerMsg(msg.hash);
|
|
||||||
|
|
||||||
NetworkServer.SendToReady(go, (short)MsgType.AnimationTrigger, msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------ client message handlers -------------------
|
// ------------------ client message handlers -------------------
|
||||||
|
[ClientRpc]
|
||||||
internal static void OnAnimationClientMessage(NetworkMessage netMsg)
|
void RpcOnAnimationClientMessage(int stateHash, float normalizedTime, byte[] parameters)
|
||||||
{
|
{
|
||||||
AnimationMessage msg = netMsg.ReadMessage<AnimationMessage>();
|
HandleAnimMsg(stateHash, normalizedTime, new NetworkReader(parameters));
|
||||||
|
|
||||||
GameObject go = ClientScene.FindLocalObject(msg.netId);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
var reader = new NetworkReader(msg.parameters);
|
|
||||||
animSync.HandleAnimMsg(msg, reader);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void OnAnimationParametersClientMessage(NetworkMessage netMsg)
|
[ClientRpc]
|
||||||
|
void RpcOnAnimationParametersClientMessage(byte[] parameters)
|
||||||
{
|
{
|
||||||
AnimationParametersMessage msg = netMsg.ReadMessage<AnimationParametersMessage>();
|
HandleAnimParamsMsg(new NetworkReader(parameters));
|
||||||
|
|
||||||
GameObject go = ClientScene.FindLocalObject(msg.netId);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
var reader = new NetworkReader(msg.parameters);
|
|
||||||
animSync.HandleAnimParamsMsg(msg, reader);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void OnAnimationTriggerClientMessage(NetworkMessage netMsg)
|
// server sends this to one client
|
||||||
|
[ClientRpc]
|
||||||
|
void RpcOnAnimationTriggerClientMessage(int hash)
|
||||||
{
|
{
|
||||||
AnimationTriggerMessage msg = netMsg.ReadMessage<AnimationTriggerMessage>();
|
HandleAnimTriggerMsg(hash);
|
||||||
|
|
||||||
GameObject go = ClientScene.FindLocalObject(msg.netId);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var animSync = go.GetComponent<NetworkAnimator>();
|
|
||||||
if (animSync != null)
|
|
||||||
{
|
|
||||||
animSync.HandleAnimTriggerMsg(msg.hash);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -225,15 +225,12 @@ internal static void RegisterSystemHandlers(NetworkClient client, bool localClie
|
|||||||
client.RegisterHandler(MsgType.ObjectHide, OnObjectDestroy);
|
client.RegisterHandler(MsgType.ObjectHide, OnObjectDestroy);
|
||||||
client.RegisterHandler(MsgType.UpdateVars, OnUpdateVarsMessage);
|
client.RegisterHandler(MsgType.UpdateVars, OnUpdateVarsMessage);
|
||||||
client.RegisterHandler(MsgType.Owner, OnOwnerMessage);
|
client.RegisterHandler(MsgType.Owner, OnOwnerMessage);
|
||||||
client.RegisterHandler(MsgType.Animation, NetworkAnimator.OnAnimationClientMessage);
|
|
||||||
client.RegisterHandler(MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersClientMessage);
|
|
||||||
client.RegisterHandler(MsgType.LocalClientAuthority, OnClientAuthority);
|
client.RegisterHandler(MsgType.LocalClientAuthority, OnClientAuthority);
|
||||||
client.RegisterHandler(MsgType.Pong, NetworkTime.OnClientPong);
|
client.RegisterHandler(MsgType.Pong, NetworkTime.OnClientPong);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.RegisterHandler(MsgType.Rpc, OnRPCMessage);
|
client.RegisterHandler(MsgType.Rpc, OnRPCMessage);
|
||||||
client.RegisterHandler(MsgType.SyncEvent, OnSyncEventMessage);
|
client.RegisterHandler(MsgType.SyncEvent, OnSyncEventMessage);
|
||||||
client.RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerClientMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// spawn handlers and prefabs //////////////////////////////////////////
|
// spawn handlers and prefabs //////////////////////////////////////////
|
||||||
|
@ -376,66 +376,6 @@ public override void Serialize(NetworkWriter writer)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnimationMessage : MessageBase
|
|
||||||
{
|
|
||||||
public uint netId;
|
|
||||||
public int stateHash; // if non-zero, then Play() this animation, skipping transitions
|
|
||||||
public float normalizedTime;
|
|
||||||
public byte[] parameters;
|
|
||||||
|
|
||||||
public override void Deserialize(NetworkReader reader)
|
|
||||||
{
|
|
||||||
netId = reader.ReadPackedUInt32();
|
|
||||||
stateHash = (int)reader.ReadPackedUInt32();
|
|
||||||
normalizedTime = reader.ReadSingle();
|
|
||||||
parameters = reader.ReadBytesAndSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Serialize(NetworkWriter writer)
|
|
||||||
{
|
|
||||||
writer.WritePackedUInt32(netId);
|
|
||||||
writer.WritePackedUInt32((uint)stateHash);
|
|
||||||
writer.Write(normalizedTime);
|
|
||||||
writer.WriteBytesAndSize(parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnimationParametersMessage : MessageBase
|
|
||||||
{
|
|
||||||
public uint netId;
|
|
||||||
public byte[] parameters;
|
|
||||||
|
|
||||||
public override void Deserialize(NetworkReader reader)
|
|
||||||
{
|
|
||||||
netId = reader.ReadPackedUInt32();
|
|
||||||
parameters = reader.ReadBytesAndSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Serialize(NetworkWriter writer)
|
|
||||||
{
|
|
||||||
writer.WritePackedUInt32(netId);
|
|
||||||
writer.WriteBytesAndSize(parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnimationTriggerMessage : MessageBase
|
|
||||||
{
|
|
||||||
public uint netId;
|
|
||||||
public int hash;
|
|
||||||
|
|
||||||
public override void Deserialize(NetworkReader reader)
|
|
||||||
{
|
|
||||||
netId = reader.ReadPackedUInt32();
|
|
||||||
hash = (int)reader.ReadPackedUInt32();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Serialize(NetworkWriter writer)
|
|
||||||
{
|
|
||||||
writer.WritePackedUInt32(netId);
|
|
||||||
writer.WritePackedUInt32((uint)hash);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A client sends this message to the server
|
// A client sends this message to the server
|
||||||
// to calculate RTT and synchronize time
|
// to calculate RTT and synchronize time
|
||||||
class NetworkPingMessage : DoubleMessage
|
class NetworkPingMessage : DoubleMessage
|
||||||
|
@ -79,9 +79,6 @@ internal static void RegisterMessageHandlers()
|
|||||||
RegisterHandler(MsgType.Ready, OnClientReadyMessage);
|
RegisterHandler(MsgType.Ready, OnClientReadyMessage);
|
||||||
RegisterHandler(MsgType.Command, OnCommandMessage);
|
RegisterHandler(MsgType.Command, OnCommandMessage);
|
||||||
RegisterHandler(MsgType.RemovePlayer, OnRemovePlayerMessage);
|
RegisterHandler(MsgType.RemovePlayer, OnRemovePlayerMessage);
|
||||||
RegisterHandler(MsgType.Animation, NetworkAnimator.OnAnimationServerMessage);
|
|
||||||
RegisterHandler(MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersServerMessage);
|
|
||||||
RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerServerMessage);
|
|
||||||
RegisterHandler(MsgType.Ping, NetworkTime.OnServerPing);
|
RegisterHandler(MsgType.Ping, NetworkTime.OnServerPing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +48,6 @@ public enum MsgType : short
|
|||||||
AddPlayer = 37,
|
AddPlayer = 37,
|
||||||
RemovePlayer = 38,
|
RemovePlayer = 38,
|
||||||
Scene = 39,
|
Scene = 39,
|
||||||
Animation = 40,
|
|
||||||
AnimationParameters = 41,
|
|
||||||
AnimationTrigger = 42,
|
|
||||||
|
|
||||||
// time synchronization
|
// time synchronization
|
||||||
Ping = 43,
|
Ping = 43,
|
||||||
|
Loading…
Reference in New Issue
Block a user