first commit
This commit is contained in:
@ -0,0 +1,572 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// <copyright file="PhotonAnimatorView.cs" company="Exit Games GmbH">
|
||||
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Component to synchronize Mecanim animations via PUN.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Photon.Pun
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This class helps you to synchronize Mecanim animations
|
||||
/// Simply add the component to your GameObject and make sure that
|
||||
/// the PhotonAnimatorView is added to the list of observed components
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When Using Trigger Parameters, make sure the component that sets the trigger is higher in the stack of Components on the GameObject than 'PhotonAnimatorView'
|
||||
/// Triggers are raised true during one frame only.
|
||||
/// </remarks>
|
||||
[AddComponentMenu("Photon Networking/Photon Animator View")]
|
||||
public class PhotonAnimatorView : MonoBehaviourPun, IPunObservable
|
||||
{
|
||||
#region Enums
|
||||
|
||||
public enum ParameterType
|
||||
{
|
||||
Float = 1,
|
||||
Int = 3,
|
||||
Bool = 4,
|
||||
Trigger = 9,
|
||||
}
|
||||
|
||||
|
||||
public enum SynchronizeType
|
||||
{
|
||||
Disabled = 0,
|
||||
Discrete = 1,
|
||||
Continuous = 2,
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class SynchronizedParameter
|
||||
{
|
||||
public ParameterType Type;
|
||||
public SynchronizeType SynchronizeType;
|
||||
public string Name;
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class SynchronizedLayer
|
||||
{
|
||||
public SynchronizeType SynchronizeType;
|
||||
public int LayerIndex;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
|
||||
#if PHOTON_DEVELOP
|
||||
public PhotonAnimatorView ReceivingSender;
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Members
|
||||
|
||||
private bool TriggerUsageWarningDone;
|
||||
|
||||
private Animator m_Animator;
|
||||
|
||||
private PhotonStreamQueue m_StreamQueue = new PhotonStreamQueue(120);
|
||||
|
||||
//These fields are only used in the CustomEditor for this script and would trigger a
|
||||
//"this variable is never used" warning, which I am suppressing here
|
||||
#pragma warning disable 0414
|
||||
|
||||
[HideInInspector]
|
||||
[SerializeField]
|
||||
private bool ShowLayerWeightsInspector = true;
|
||||
|
||||
[HideInInspector]
|
||||
[SerializeField]
|
||||
private bool ShowParameterInspector = true;
|
||||
|
||||
#pragma warning restore 0414
|
||||
|
||||
[HideInInspector]
|
||||
[SerializeField]
|
||||
private List<SynchronizedParameter> m_SynchronizeParameters = new List<SynchronizedParameter>();
|
||||
|
||||
[HideInInspector]
|
||||
[SerializeField]
|
||||
private List<SynchronizedLayer> m_SynchronizeLayers = new List<SynchronizedLayer>();
|
||||
|
||||
private Vector3 m_ReceiverPosition;
|
||||
private float m_LastDeserializeTime;
|
||||
private bool m_WasSynchronizeTypeChanged = true;
|
||||
|
||||
/// <summary>
|
||||
/// Cached raised triggers that are set to be synchronized in discrete mode. since a Trigger only stay up for less than a frame,
|
||||
/// We need to cache it until the next discrete serialization call.
|
||||
/// </summary>
|
||||
List<string> m_raisedDiscreteTriggersCache = new List<string>();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Unity
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
this.m_Animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (this.m_Animator.applyRootMotion && this.photonView.IsMine == false && PhotonNetwork.IsConnected == true)
|
||||
{
|
||||
this.m_Animator.applyRootMotion = false;
|
||||
}
|
||||
|
||||
if (PhotonNetwork.InRoom == false || PhotonNetwork.CurrentRoom.PlayerCount <= 1)
|
||||
{
|
||||
this.m_StreamQueue.Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.photonView.IsMine == true)
|
||||
{
|
||||
this.SerializeDataContinuously();
|
||||
|
||||
this.CacheDiscreteTriggers();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.DeserializeDataContinuously();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Setup Synchronizing Methods
|
||||
|
||||
/// <summary>
|
||||
/// Caches the discrete triggers values for keeping track of raised triggers, and will be reseted after the sync routine got performed
|
||||
/// </summary>
|
||||
public void CacheDiscreteTriggers()
|
||||
{
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
SynchronizedParameter parameter = this.m_SynchronizeParameters[i];
|
||||
|
||||
if (parameter.SynchronizeType == SynchronizeType.Discrete && parameter.Type == ParameterType.Trigger && this.m_Animator.GetBool(parameter.Name))
|
||||
{
|
||||
if (parameter.Type == ParameterType.Trigger)
|
||||
{
|
||||
this.m_raisedDiscreteTriggersCache.Add(parameter.Name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a specific layer is configured to be synchronize
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">Index of the layer.</param>
|
||||
/// <returns>True if the layer is synchronized</returns>
|
||||
public bool DoesLayerSynchronizeTypeExist(int layerIndex)
|
||||
{
|
||||
return this.m_SynchronizeLayers.FindIndex(item => item.LayerIndex == layerIndex) != -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the specified parameter is configured to be synchronized
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the parameter.</param>
|
||||
/// <returns>True if the parameter is synchronized</returns>
|
||||
public bool DoesParameterSynchronizeTypeExist(string name)
|
||||
{
|
||||
return this.m_SynchronizeParameters.FindIndex(item => item.Name == name) != -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all synchronized layers
|
||||
/// </summary>
|
||||
/// <returns>List of SynchronizedLayer objects</returns>
|
||||
public List<SynchronizedLayer> GetSynchronizedLayers()
|
||||
{
|
||||
return this.m_SynchronizeLayers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all synchronized parameters
|
||||
/// </summary>
|
||||
/// <returns>List of SynchronizedParameter objects</returns>
|
||||
public List<SynchronizedParameter> GetSynchronizedParameters()
|
||||
{
|
||||
return this.m_SynchronizeParameters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type how the layer is synchronized
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">Index of the layer.</param>
|
||||
/// <returns>Disabled/Discrete/Continuous</returns>
|
||||
public SynchronizeType GetLayerSynchronizeType(int layerIndex)
|
||||
{
|
||||
int index = this.m_SynchronizeLayers.FindIndex(item => item.LayerIndex == layerIndex);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
return SynchronizeType.Disabled;
|
||||
}
|
||||
|
||||
return this.m_SynchronizeLayers[index].SynchronizeType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type how the parameter is synchronized
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the parameter.</param>
|
||||
/// <returns>Disabled/Discrete/Continuous</returns>
|
||||
public SynchronizeType GetParameterSynchronizeType(string name)
|
||||
{
|
||||
int index = this.m_SynchronizeParameters.FindIndex(item => item.Name == name);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
return SynchronizeType.Disabled;
|
||||
}
|
||||
|
||||
return this.m_SynchronizeParameters[index].SynchronizeType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the how a layer should be synchronized
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">Index of the layer.</param>
|
||||
/// <param name="synchronizeType">Disabled/Discrete/Continuous</param>
|
||||
public void SetLayerSynchronized(int layerIndex, SynchronizeType synchronizeType)
|
||||
{
|
||||
if (Application.isPlaying == true)
|
||||
{
|
||||
this.m_WasSynchronizeTypeChanged = true;
|
||||
}
|
||||
|
||||
int index = this.m_SynchronizeLayers.FindIndex(item => item.LayerIndex == layerIndex);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
this.m_SynchronizeLayers.Add(new SynchronizedLayer {LayerIndex = layerIndex, SynchronizeType = synchronizeType});
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_SynchronizeLayers[index].SynchronizeType = synchronizeType;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the how a parameter should be synchronized
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the parameter.</param>
|
||||
/// <param name="type">The type of the parameter.</param>
|
||||
/// <param name="synchronizeType">Disabled/Discrete/Continuous</param>
|
||||
public void SetParameterSynchronized(string name, ParameterType type, SynchronizeType synchronizeType)
|
||||
{
|
||||
if (Application.isPlaying == true)
|
||||
{
|
||||
this.m_WasSynchronizeTypeChanged = true;
|
||||
}
|
||||
|
||||
int index = this.m_SynchronizeParameters.FindIndex(item => item.Name == name);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
this.m_SynchronizeParameters.Add(new SynchronizedParameter {Name = name, Type = type, SynchronizeType = synchronizeType});
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_SynchronizeParameters[index].SynchronizeType = synchronizeType;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Serialization
|
||||
|
||||
private void SerializeDataContinuously()
|
||||
{
|
||||
if (this.m_Animator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
if (this.m_SynchronizeLayers[i].SynchronizeType == SynchronizeType.Continuous)
|
||||
{
|
||||
this.m_StreamQueue.SendNext(this.m_Animator.GetLayerWeight(this.m_SynchronizeLayers[i].LayerIndex));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
SynchronizedParameter parameter = this.m_SynchronizeParameters[i];
|
||||
|
||||
if (parameter.SynchronizeType == SynchronizeType.Continuous)
|
||||
{
|
||||
switch (parameter.Type)
|
||||
{
|
||||
case ParameterType.Bool:
|
||||
this.m_StreamQueue.SendNext(this.m_Animator.GetBool(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Float:
|
||||
this.m_StreamQueue.SendNext(this.m_Animator.GetFloat(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Int:
|
||||
this.m_StreamQueue.SendNext(this.m_Animator.GetInteger(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Trigger:
|
||||
if (!TriggerUsageWarningDone)
|
||||
{
|
||||
TriggerUsageWarningDone = true;
|
||||
Debug.Log("PhotonAnimatorView: When using triggers, make sure this component is last in the stack.\n" +
|
||||
"If you still experience issues, implement triggers as a regular RPC \n" +
|
||||
"or in custom IPunObservable component instead",this);
|
||||
|
||||
}
|
||||
this.m_StreamQueue.SendNext(this.m_Animator.GetBool(parameter.Name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void DeserializeDataContinuously()
|
||||
{
|
||||
if (this.m_StreamQueue.HasQueuedObjects() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
if (this.m_SynchronizeLayers[i].SynchronizeType == SynchronizeType.Continuous)
|
||||
{
|
||||
this.m_Animator.SetLayerWeight(this.m_SynchronizeLayers[i].LayerIndex, (float) this.m_StreamQueue.ReceiveNext());
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
SynchronizedParameter parameter = this.m_SynchronizeParameters[i];
|
||||
|
||||
if (parameter.SynchronizeType == SynchronizeType.Continuous)
|
||||
{
|
||||
switch (parameter.Type)
|
||||
{
|
||||
case ParameterType.Bool:
|
||||
this.m_Animator.SetBool(parameter.Name, (bool) this.m_StreamQueue.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Float:
|
||||
this.m_Animator.SetFloat(parameter.Name, (float) this.m_StreamQueue.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Int:
|
||||
this.m_Animator.SetInteger(parameter.Name, (int) this.m_StreamQueue.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Trigger:
|
||||
this.m_Animator.SetBool(parameter.Name, (bool) this.m_StreamQueue.ReceiveNext());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SerializeDataDiscretly(PhotonStream stream)
|
||||
{
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
if (this.m_SynchronizeLayers[i].SynchronizeType == SynchronizeType.Discrete)
|
||||
{
|
||||
stream.SendNext(this.m_Animator.GetLayerWeight(this.m_SynchronizeLayers[i].LayerIndex));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
|
||||
SynchronizedParameter parameter = this.m_SynchronizeParameters[i];
|
||||
|
||||
if (parameter.SynchronizeType == SynchronizeType.Discrete)
|
||||
{
|
||||
switch (parameter.Type)
|
||||
{
|
||||
case ParameterType.Bool:
|
||||
stream.SendNext(this.m_Animator.GetBool(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Float:
|
||||
stream.SendNext(this.m_Animator.GetFloat(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Int:
|
||||
stream.SendNext(this.m_Animator.GetInteger(parameter.Name));
|
||||
break;
|
||||
case ParameterType.Trigger:
|
||||
if (!TriggerUsageWarningDone)
|
||||
{
|
||||
TriggerUsageWarningDone = true;
|
||||
Debug.Log("PhotonAnimatorView: When using triggers, make sure this component is last in the stack.\n" +
|
||||
"If you still experience issues, implement triggers as a regular RPC \n" +
|
||||
"or in custom IPunObservable component instead",this);
|
||||
|
||||
}
|
||||
// here we can't rely on the current real state of the trigger, we might have missed its raise
|
||||
stream.SendNext(this.m_raisedDiscreteTriggersCache.Contains(parameter.Name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reset the cache, we've synchronized.
|
||||
this.m_raisedDiscreteTriggersCache.Clear();
|
||||
}
|
||||
|
||||
private void DeserializeDataDiscretly(PhotonStream stream)
|
||||
{
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
if (this.m_SynchronizeLayers[i].SynchronizeType == SynchronizeType.Discrete)
|
||||
{
|
||||
this.m_Animator.SetLayerWeight(this.m_SynchronizeLayers[i].LayerIndex, (float) stream.ReceiveNext());
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
SynchronizedParameter parameter = this.m_SynchronizeParameters[i];
|
||||
|
||||
if (parameter.SynchronizeType == SynchronizeType.Discrete)
|
||||
{
|
||||
switch (parameter.Type)
|
||||
{
|
||||
case ParameterType.Bool:
|
||||
if (stream.PeekNext() is bool == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.m_Animator.SetBool(parameter.Name, (bool) stream.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Float:
|
||||
if (stream.PeekNext() is float == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.m_Animator.SetFloat(parameter.Name, (float) stream.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Int:
|
||||
if (stream.PeekNext() is int == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.m_Animator.SetInteger(parameter.Name, (int) stream.ReceiveNext());
|
||||
break;
|
||||
case ParameterType.Trigger:
|
||||
if (stream.PeekNext() is bool == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((bool) stream.ReceiveNext())
|
||||
{
|
||||
this.m_Animator.SetTrigger(parameter.Name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SerializeSynchronizationTypeState(PhotonStream stream)
|
||||
{
|
||||
byte[] states = new byte[this.m_SynchronizeLayers.Count + this.m_SynchronizeParameters.Count];
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
states[i] = (byte) this.m_SynchronizeLayers[i].SynchronizeType;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
states[this.m_SynchronizeLayers.Count + i] = (byte) this.m_SynchronizeParameters[i].SynchronizeType;
|
||||
}
|
||||
|
||||
stream.SendNext(states);
|
||||
}
|
||||
|
||||
private void DeserializeSynchronizationTypeState(PhotonStream stream)
|
||||
{
|
||||
byte[] state = (byte[]) stream.ReceiveNext();
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeLayers.Count; ++i)
|
||||
{
|
||||
this.m_SynchronizeLayers[i].SynchronizeType = (SynchronizeType) state[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.m_SynchronizeParameters.Count; ++i)
|
||||
{
|
||||
this.m_SynchronizeParameters[i].SynchronizeType = (SynchronizeType) state[this.m_SynchronizeLayers.Count + i];
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (this.m_Animator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.IsWriting == true)
|
||||
{
|
||||
if (this.m_WasSynchronizeTypeChanged == true)
|
||||
{
|
||||
this.m_StreamQueue.Reset();
|
||||
this.SerializeSynchronizationTypeState(stream);
|
||||
|
||||
this.m_WasSynchronizeTypeChanged = false;
|
||||
}
|
||||
|
||||
this.m_StreamQueue.Serialize(stream);
|
||||
this.SerializeDataDiscretly(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if PHOTON_DEVELOP
|
||||
if( ReceivingSender != null )
|
||||
{
|
||||
ReceivingSender.OnPhotonSerializeView( stream, info );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if (stream.PeekNext() is byte[])
|
||||
{
|
||||
this.DeserializeSynchronizationTypeState(stream);
|
||||
}
|
||||
|
||||
this.m_StreamQueue.Deserialize(stream);
|
||||
this.DeserializeDataDiscretly(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b8c4a61274f60b4ea5fb4299cfdbf14
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
@ -0,0 +1,111 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// <copyright file="PhotonRigidbody2DView.cs" company="Exit Games GmbH">
|
||||
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Component to synchronize 2d rigidbodies via PUN.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Photon.Pun
|
||||
{
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[AddComponentMenu("Photon Networking/Photon Rigidbody 2D View")]
|
||||
public class PhotonRigidbody2DView : MonoBehaviourPun, IPunObservable
|
||||
{
|
||||
private float m_Distance;
|
||||
private float m_Angle;
|
||||
|
||||
private Rigidbody2D m_Body;
|
||||
|
||||
private Vector2 m_NetworkPosition;
|
||||
|
||||
private float m_NetworkRotation;
|
||||
|
||||
[HideInInspector]
|
||||
public bool m_SynchronizeVelocity = true;
|
||||
[HideInInspector]
|
||||
public bool m_SynchronizeAngularVelocity = false;
|
||||
|
||||
[HideInInspector]
|
||||
public bool m_TeleportEnabled = false;
|
||||
[HideInInspector]
|
||||
public float m_TeleportIfDistanceGreaterThan = 3.0f;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
this.m_Body = GetComponent<Rigidbody2D>();
|
||||
|
||||
this.m_NetworkPosition = new Vector2();
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
if (!this.photonView.IsMine)
|
||||
{
|
||||
this.m_Body.position = Vector2.MoveTowards(this.m_Body.position, this.m_NetworkPosition, this.m_Distance * (1.0f / PhotonNetwork.SerializationRate));
|
||||
this.m_Body.rotation = Mathf.MoveTowards(this.m_Body.rotation, this.m_NetworkRotation, this.m_Angle * (1.0f / PhotonNetwork.SerializationRate));
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (stream.IsWriting)
|
||||
{
|
||||
stream.SendNext(this.m_Body.position);
|
||||
stream.SendNext(this.m_Body.rotation);
|
||||
|
||||
if (this.m_SynchronizeVelocity)
|
||||
{
|
||||
stream.SendNext(this.m_Body.velocity);
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
stream.SendNext(this.m_Body.angularVelocity);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_NetworkPosition = (Vector2)stream.ReceiveNext();
|
||||
this.m_NetworkRotation = (float)stream.ReceiveNext();
|
||||
|
||||
if (this.m_TeleportEnabled)
|
||||
{
|
||||
if (Vector3.Distance(this.m_Body.position, this.m_NetworkPosition) > this.m_TeleportIfDistanceGreaterThan)
|
||||
{
|
||||
this.m_Body.position = this.m_NetworkPosition;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeVelocity || this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
|
||||
|
||||
if (m_SynchronizeVelocity)
|
||||
{
|
||||
this.m_Body.velocity = (Vector2)stream.ReceiveNext();
|
||||
|
||||
this.m_NetworkPosition += this.m_Body.velocity * lag;
|
||||
|
||||
this.m_Distance = Vector2.Distance(this.m_Body.position, this.m_NetworkPosition);
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
this.m_Body.angularVelocity = (float)stream.ReceiveNext();
|
||||
|
||||
this.m_NetworkRotation += this.m_Body.angularVelocity * lag;
|
||||
|
||||
this.m_Angle = Mathf.Abs(this.m_Body.rotation - this.m_NetworkRotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e7cb724808c322458aa4d15f5035fa9
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
@ -0,0 +1,112 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// <copyright file="PhotonRigidbodyView.cs" company="Exit Games GmbH">
|
||||
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Component to synchronize rigidbodies via PUN.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Photon.Pun
|
||||
{
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[AddComponentMenu("Photon Networking/Photon Rigidbody View")]
|
||||
public class PhotonRigidbodyView : MonoBehaviourPun, IPunObservable
|
||||
{
|
||||
private float m_Distance;
|
||||
private float m_Angle;
|
||||
|
||||
private Rigidbody m_Body;
|
||||
|
||||
private Vector3 m_NetworkPosition;
|
||||
|
||||
private Quaternion m_NetworkRotation;
|
||||
|
||||
[HideInInspector]
|
||||
public bool m_SynchronizeVelocity = true;
|
||||
[HideInInspector]
|
||||
public bool m_SynchronizeAngularVelocity = false;
|
||||
|
||||
[HideInInspector]
|
||||
public bool m_TeleportEnabled = false;
|
||||
[HideInInspector]
|
||||
public float m_TeleportIfDistanceGreaterThan = 3.0f;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
this.m_Body = GetComponent<Rigidbody>();
|
||||
|
||||
this.m_NetworkPosition = new Vector3();
|
||||
this.m_NetworkRotation = new Quaternion();
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
if (!this.photonView.IsMine)
|
||||
{
|
||||
this.m_Body.position = Vector3.MoveTowards(this.m_Body.position, this.m_NetworkPosition, this.m_Distance * (1.0f / PhotonNetwork.SerializationRate));
|
||||
this.m_Body.rotation = Quaternion.RotateTowards(this.m_Body.rotation, this.m_NetworkRotation, this.m_Angle * (1.0f / PhotonNetwork.SerializationRate));
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (stream.IsWriting)
|
||||
{
|
||||
stream.SendNext(this.m_Body.position);
|
||||
stream.SendNext(this.m_Body.rotation);
|
||||
|
||||
if (this.m_SynchronizeVelocity)
|
||||
{
|
||||
stream.SendNext(this.m_Body.velocity);
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
stream.SendNext(this.m_Body.angularVelocity);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_NetworkPosition = (Vector3)stream.ReceiveNext();
|
||||
this.m_NetworkRotation = (Quaternion)stream.ReceiveNext();
|
||||
|
||||
if (this.m_TeleportEnabled)
|
||||
{
|
||||
if (Vector3.Distance(this.m_Body.position, this.m_NetworkPosition) > this.m_TeleportIfDistanceGreaterThan)
|
||||
{
|
||||
this.m_Body.position = this.m_NetworkPosition;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeVelocity || this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
|
||||
|
||||
if (this.m_SynchronizeVelocity)
|
||||
{
|
||||
this.m_Body.velocity = (Vector3)stream.ReceiveNext();
|
||||
|
||||
this.m_NetworkPosition += this.m_Body.velocity * lag;
|
||||
|
||||
this.m_Distance = Vector3.Distance(this.m_Body.position, this.m_NetworkPosition);
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeAngularVelocity)
|
||||
{
|
||||
this.m_Body.angularVelocity = (Vector3)stream.ReceiveNext();
|
||||
|
||||
this.m_NetworkRotation = Quaternion.Euler(this.m_Body.angularVelocity * lag) * this.m_NetworkRotation;
|
||||
|
||||
this.m_Angle = Quaternion.Angle(this.m_Body.rotation, this.m_NetworkRotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64179f3720bbfe947b7724caa67b7c1d
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
@ -0,0 +1,194 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// <copyright file="PhotonTransformView.cs" company="Exit Games GmbH">
|
||||
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Component to synchronize Transforms via PUN PhotonView.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Photon.Pun
|
||||
{
|
||||
using UnityEngine;
|
||||
|
||||
[AddComponentMenu("Photon Networking/Photon Transform View")]
|
||||
[HelpURL("https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state")]
|
||||
public class PhotonTransformView : MonoBehaviourPun, IPunObservable
|
||||
{
|
||||
private float m_Distance;
|
||||
private float m_Angle;
|
||||
|
||||
private Vector3 m_Direction;
|
||||
private Vector3 m_NetworkPosition;
|
||||
private Vector3 m_StoredPosition;
|
||||
|
||||
private Quaternion m_NetworkRotation;
|
||||
|
||||
public bool m_SynchronizePosition = true;
|
||||
public bool m_SynchronizeRotation = true;
|
||||
public bool m_SynchronizeScale = false;
|
||||
|
||||
[Tooltip("Indicates if localPosition and localRotation should be used. Scale ignores this setting, and always uses localScale to avoid issues with lossyScale.")]
|
||||
public bool m_UseLocal;
|
||||
|
||||
bool m_firstTake = false;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
m_StoredPosition = transform.localPosition;
|
||||
m_NetworkPosition = Vector3.zero;
|
||||
|
||||
m_NetworkRotation = Quaternion.identity;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
// Only default to true with new instances. useLocal will remain false for old projects that are updating PUN.
|
||||
m_UseLocal = true;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_firstTake = true;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var tr = transform;
|
||||
|
||||
if (!this.photonView.IsMine)
|
||||
{
|
||||
if (m_UseLocal)
|
||||
|
||||
{
|
||||
tr.localPosition = Vector3.MoveTowards(tr.localPosition, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
|
||||
tr.localRotation = Quaternion.RotateTowards(tr.localRotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
|
||||
}
|
||||
else
|
||||
{
|
||||
tr.position = Vector3.MoveTowards(tr.position, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
|
||||
tr.rotation = Quaternion.RotateTowards(tr.rotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
var tr = transform;
|
||||
|
||||
// Write
|
||||
if (stream.IsWriting)
|
||||
{
|
||||
if (this.m_SynchronizePosition)
|
||||
{
|
||||
if (m_UseLocal)
|
||||
{
|
||||
this.m_Direction = tr.localPosition - this.m_StoredPosition;
|
||||
this.m_StoredPosition = tr.localPosition;
|
||||
stream.SendNext(tr.localPosition);
|
||||
stream.SendNext(this.m_Direction);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_Direction = tr.position - this.m_StoredPosition;
|
||||
this.m_StoredPosition = tr.position;
|
||||
stream.SendNext(tr.position);
|
||||
stream.SendNext(this.m_Direction);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeRotation)
|
||||
{
|
||||
if (m_UseLocal)
|
||||
{
|
||||
stream.SendNext(tr.localRotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.SendNext(tr.rotation);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeScale)
|
||||
{
|
||||
stream.SendNext(tr.localScale);
|
||||
}
|
||||
}
|
||||
// Read
|
||||
else
|
||||
{
|
||||
if (this.m_SynchronizePosition)
|
||||
{
|
||||
this.m_NetworkPosition = (Vector3)stream.ReceiveNext();
|
||||
this.m_Direction = (Vector3)stream.ReceiveNext();
|
||||
|
||||
if (m_firstTake)
|
||||
{
|
||||
if (m_UseLocal)
|
||||
tr.localPosition = this.m_NetworkPosition;
|
||||
else
|
||||
tr.position = this.m_NetworkPosition;
|
||||
|
||||
this.m_Distance = 0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
|
||||
this.m_NetworkPosition += this.m_Direction * lag;
|
||||
if (m_UseLocal)
|
||||
{
|
||||
this.m_Distance = Vector3.Distance(tr.localPosition, this.m_NetworkPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_Distance = Vector3.Distance(tr.position, this.m_NetworkPosition);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeRotation)
|
||||
{
|
||||
this.m_NetworkRotation = (Quaternion)stream.ReceiveNext();
|
||||
|
||||
if (m_firstTake)
|
||||
{
|
||||
this.m_Angle = 0f;
|
||||
|
||||
if (m_UseLocal)
|
||||
{
|
||||
tr.localRotation = this.m_NetworkRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
tr.rotation = this.m_NetworkRotation;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_UseLocal)
|
||||
{
|
||||
this.m_Angle = Quaternion.Angle(tr.localRotation, this.m_NetworkRotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.m_Angle = Quaternion.Angle(tr.rotation, this.m_NetworkRotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.m_SynchronizeScale)
|
||||
{
|
||||
tr.localScale = (Vector3)stream.ReceiveNext();
|
||||
}
|
||||
|
||||
if (m_firstTake)
|
||||
{
|
||||
m_firstTake = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 627855c7f81362d41938ffe0b1475957
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,557 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// <copyright file="PhotonTransformViewClassic.cs" company="Exit Games GmbH">
|
||||
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Component to synchronize Transforms via PUN PhotonView.
|
||||
// </summary>
|
||||
// <author>developer@exitgames.com</author>
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Photon.Pun
|
||||
{
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This class helps you to synchronize position, rotation and scale
|
||||
/// of a GameObject. It also gives you many different options to make
|
||||
/// the synchronized values appear smooth, even when the data is only
|
||||
/// send a couple of times per second.
|
||||
/// Simply add the component to your GameObject and make sure that
|
||||
/// the PhotonTransformViewClassic is added to the list of observed components
|
||||
/// </summary>
|
||||
[AddComponentMenu("Photon Networking/Photon Transform View Classic")]
|
||||
public class PhotonTransformViewClassic : MonoBehaviourPun, IPunObservable
|
||||
{
|
||||
//As this component is very complex, we separated it into multiple classes.
|
||||
//The PositionModel, RotationModel and ScaleMode store the data you are able to
|
||||
//configure in the inspector while the "control" objects below are actually moving
|
||||
//the object and calculating all the inter- and extrapolation
|
||||
|
||||
[HideInInspector]
|
||||
public PhotonTransformViewPositionModel m_PositionModel = new PhotonTransformViewPositionModel();
|
||||
|
||||
[HideInInspector]
|
||||
public PhotonTransformViewRotationModel m_RotationModel = new PhotonTransformViewRotationModel();
|
||||
|
||||
[HideInInspector]
|
||||
public PhotonTransformViewScaleModel m_ScaleModel = new PhotonTransformViewScaleModel();
|
||||
|
||||
PhotonTransformViewPositionControl m_PositionControl;
|
||||
PhotonTransformViewRotationControl m_RotationControl;
|
||||
PhotonTransformViewScaleControl m_ScaleControl;
|
||||
|
||||
PhotonView m_PhotonView;
|
||||
|
||||
bool m_ReceivedNetworkUpdate = false;
|
||||
|
||||
/// <summary>
|
||||
/// Flag to skip initial data when Object is instantiated and rely on the first deserialized data instead.
|
||||
/// </summary>
|
||||
bool m_firstTake = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
this.m_PhotonView = GetComponent<PhotonView>();
|
||||
|
||||
this.m_PositionControl = new PhotonTransformViewPositionControl(this.m_PositionModel);
|
||||
this.m_RotationControl = new PhotonTransformViewRotationControl(this.m_RotationModel);
|
||||
this.m_ScaleControl = new PhotonTransformViewScaleControl(this.m_ScaleModel);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_firstTake = true;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (this.m_PhotonView == null || this.m_PhotonView.IsMine == true || PhotonNetwork.IsConnectedAndReady == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.UpdatePosition();
|
||||
this.UpdateRotation();
|
||||
this.UpdateScale();
|
||||
}
|
||||
|
||||
void UpdatePosition()
|
||||
{
|
||||
if (this.m_PositionModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
transform.localPosition = this.m_PositionControl.UpdatePosition(transform.localPosition);
|
||||
}
|
||||
|
||||
void UpdateRotation()
|
||||
{
|
||||
if (this.m_RotationModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
transform.localRotation = this.m_RotationControl.GetRotation(transform.localRotation);
|
||||
}
|
||||
|
||||
void UpdateScale()
|
||||
{
|
||||
if (this.m_ScaleModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
transform.localScale = this.m_ScaleControl.GetScale(transform.localScale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// These values are synchronized to the remote objects if the interpolation mode
|
||||
/// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
|
||||
/// the current speed (in units/second) and turning speed (in angles/second) so the remote
|
||||
/// object can use them to predict the objects movement.
|
||||
/// </summary>
|
||||
/// <param name="speed">The current movement vector of the object in units/second.</param>
|
||||
/// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
|
||||
public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
|
||||
{
|
||||
this.m_PositionControl.SetSynchronizedValues(speed, turnSpeed);
|
||||
}
|
||||
|
||||
|
||||
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
this.m_PositionControl.OnPhotonSerializeView(transform.localPosition, stream, info);
|
||||
this.m_RotationControl.OnPhotonSerializeView(transform.localRotation, stream, info);
|
||||
this.m_ScaleControl.OnPhotonSerializeView(transform.localScale, stream, info);
|
||||
|
||||
if (stream.IsReading == true)
|
||||
{
|
||||
this.m_ReceivedNetworkUpdate = true;
|
||||
|
||||
// force latest data to avoid initial drifts when player is instantiated.
|
||||
if (m_firstTake)
|
||||
{
|
||||
m_firstTake = false;
|
||||
|
||||
if (this.m_PositionModel.SynchronizeEnabled)
|
||||
{
|
||||
this.transform.localPosition = this.m_PositionControl.GetNetworkPosition();
|
||||
}
|
||||
|
||||
if (this.m_RotationModel.SynchronizeEnabled)
|
||||
{
|
||||
this.transform.localRotation = this.m_RotationControl.GetNetworkRotation();
|
||||
}
|
||||
|
||||
if (this.m_ScaleModel.SynchronizeEnabled)
|
||||
{
|
||||
this.transform.localScale = this.m_ScaleControl.GetNetworkScale();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class PhotonTransformViewPositionModel
|
||||
{
|
||||
public enum InterpolateOptions
|
||||
{
|
||||
Disabled,
|
||||
FixedSpeed,
|
||||
EstimatedSpeed,
|
||||
SynchronizeValues,
|
||||
Lerp
|
||||
}
|
||||
|
||||
|
||||
public enum ExtrapolateOptions
|
||||
{
|
||||
Disabled,
|
||||
SynchronizeValues,
|
||||
EstimateSpeedAndTurn,
|
||||
FixedSpeed,
|
||||
}
|
||||
|
||||
|
||||
public bool SynchronizeEnabled;
|
||||
|
||||
public bool TeleportEnabled = true;
|
||||
public float TeleportIfDistanceGreaterThan = 3f;
|
||||
|
||||
public InterpolateOptions InterpolateOption = InterpolateOptions.EstimatedSpeed;
|
||||
public float InterpolateMoveTowardsSpeed = 1f;
|
||||
|
||||
public float InterpolateLerpSpeed = 1f;
|
||||
|
||||
public ExtrapolateOptions ExtrapolateOption = ExtrapolateOptions.Disabled;
|
||||
public float ExtrapolateSpeed = 1f;
|
||||
public bool ExtrapolateIncludingRoundTripTime = true;
|
||||
public int ExtrapolateNumberOfStoredPositions = 1;
|
||||
}
|
||||
|
||||
public class PhotonTransformViewPositionControl
|
||||
{
|
||||
PhotonTransformViewPositionModel m_Model;
|
||||
float m_CurrentSpeed;
|
||||
double m_LastSerializeTime;
|
||||
Vector3 m_SynchronizedSpeed = Vector3.zero;
|
||||
float m_SynchronizedTurnSpeed = 0;
|
||||
|
||||
Vector3 m_NetworkPosition;
|
||||
Queue<Vector3> m_OldNetworkPositions = new Queue<Vector3>();
|
||||
|
||||
bool m_UpdatedPositionAfterOnSerialize = true;
|
||||
|
||||
public PhotonTransformViewPositionControl(PhotonTransformViewPositionModel model)
|
||||
{
|
||||
m_Model = model;
|
||||
}
|
||||
|
||||
Vector3 GetOldestStoredNetworkPosition()
|
||||
{
|
||||
Vector3 oldPosition = m_NetworkPosition;
|
||||
|
||||
if (m_OldNetworkPositions.Count > 0)
|
||||
{
|
||||
oldPosition = m_OldNetworkPositions.Peek();
|
||||
}
|
||||
|
||||
return oldPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// These values are synchronized to the remote objects if the interpolation mode
|
||||
/// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
|
||||
/// the current speed (in units/second) and turning speed (in angles/second) so the remote
|
||||
/// object can use them to predict the objects movement.
|
||||
/// </summary>
|
||||
/// <param name="speed">The current movement vector of the object in units/second.</param>
|
||||
/// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
|
||||
public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
|
||||
{
|
||||
m_SynchronizedSpeed = speed;
|
||||
m_SynchronizedTurnSpeed = turnSpeed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the new position based on the values setup in the inspector
|
||||
/// </summary>
|
||||
/// <param name="currentPosition">The current position.</param>
|
||||
/// <returns>The new position.</returns>
|
||||
public Vector3 UpdatePosition(Vector3 currentPosition)
|
||||
{
|
||||
Vector3 targetPosition = GetNetworkPosition() + GetExtrapolatedPositionOffset();
|
||||
|
||||
switch (m_Model.InterpolateOption)
|
||||
{
|
||||
case PhotonTransformViewPositionModel.InterpolateOptions.Disabled:
|
||||
if (m_UpdatedPositionAfterOnSerialize == false)
|
||||
{
|
||||
currentPosition = targetPosition;
|
||||
m_UpdatedPositionAfterOnSerialize = true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
|
||||
currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateMoveTowardsSpeed);
|
||||
break;
|
||||
|
||||
case PhotonTransformViewPositionModel.InterpolateOptions.EstimatedSpeed:
|
||||
if (m_OldNetworkPositions.Count == 0)
|
||||
{
|
||||
// special case: we have no previous updates in memory, so we can't guess a speed!
|
||||
break;
|
||||
}
|
||||
|
||||
// knowing the last (incoming) position and the one before, we can guess a speed.
|
||||
// note that the speed is times sendRateOnSerialize! we send X updates/sec, so our estimate has to factor that in.
|
||||
float estimatedSpeed = (Vector3.Distance(m_NetworkPosition, GetOldestStoredNetworkPosition()) / m_OldNetworkPositions.Count) * PhotonNetwork.SerializationRate;
|
||||
|
||||
// move towards the targetPosition (including estimates, if that's active) with the speed calculated from the last updates.
|
||||
currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * estimatedSpeed);
|
||||
break;
|
||||
|
||||
case PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues:
|
||||
if (m_SynchronizedSpeed.magnitude == 0)
|
||||
{
|
||||
currentPosition = targetPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * m_SynchronizedSpeed.magnitude);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
|
||||
currentPosition = Vector3.Lerp(currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateLerpSpeed);
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_Model.TeleportEnabled == true)
|
||||
{
|
||||
if (Vector3.Distance(currentPosition, GetNetworkPosition()) > m_Model.TeleportIfDistanceGreaterThan)
|
||||
{
|
||||
currentPosition = GetNetworkPosition();
|
||||
}
|
||||
}
|
||||
|
||||
return currentPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last position that was received through the network
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Vector3 GetNetworkPosition()
|
||||
{
|
||||
return m_NetworkPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates an estimated position based on the last synchronized position,
|
||||
/// the time when the last position was received and the movement speed of the object
|
||||
/// </summary>
|
||||
/// <returns>Estimated position of the remote object</returns>
|
||||
public Vector3 GetExtrapolatedPositionOffset()
|
||||
{
|
||||
float timePassed = (float)(PhotonNetwork.Time - m_LastSerializeTime);
|
||||
|
||||
if (m_Model.ExtrapolateIncludingRoundTripTime == true)
|
||||
{
|
||||
timePassed += (float)PhotonNetwork.GetPing() / 1000f;
|
||||
}
|
||||
|
||||
Vector3 extrapolatePosition = Vector3.zero;
|
||||
|
||||
switch (m_Model.ExtrapolateOption)
|
||||
{
|
||||
case PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues:
|
||||
Quaternion turnRotation = Quaternion.Euler(0, m_SynchronizedTurnSpeed * timePassed, 0);
|
||||
extrapolatePosition = turnRotation * (m_SynchronizedSpeed * timePassed);
|
||||
break;
|
||||
case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
|
||||
Vector3 moveDirection = (m_NetworkPosition - GetOldestStoredNetworkPosition()).normalized;
|
||||
|
||||
extrapolatePosition = moveDirection * m_Model.ExtrapolateSpeed * timePassed;
|
||||
break;
|
||||
case PhotonTransformViewPositionModel.ExtrapolateOptions.EstimateSpeedAndTurn:
|
||||
Vector3 moveDelta = (m_NetworkPosition - GetOldestStoredNetworkPosition()) * PhotonNetwork.SerializationRate;
|
||||
extrapolatePosition = moveDelta * timePassed;
|
||||
break;
|
||||
}
|
||||
|
||||
return extrapolatePosition;
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(Vector3 currentPosition, PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (m_Model.SynchronizeEnabled == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.IsWriting == true)
|
||||
{
|
||||
SerializeData(currentPosition, stream, info);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeserializeData(stream, info);
|
||||
}
|
||||
|
||||
m_LastSerializeTime = PhotonNetwork.Time;
|
||||
m_UpdatedPositionAfterOnSerialize = false;
|
||||
}
|
||||
|
||||
void SerializeData(Vector3 currentPosition, PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
stream.SendNext(currentPosition);
|
||||
m_NetworkPosition = currentPosition;
|
||||
|
||||
if (m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
|
||||
m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues)
|
||||
{
|
||||
stream.SendNext(m_SynchronizedSpeed);
|
||||
stream.SendNext(m_SynchronizedTurnSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
void DeserializeData(PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
Vector3 readPosition = (Vector3)stream.ReceiveNext();
|
||||
if (m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
|
||||
m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues)
|
||||
{
|
||||
m_SynchronizedSpeed = (Vector3)stream.ReceiveNext();
|
||||
m_SynchronizedTurnSpeed = (float)stream.ReceiveNext();
|
||||
}
|
||||
|
||||
if (m_OldNetworkPositions.Count == 0)
|
||||
{
|
||||
// if we don't have old positions yet, this is the very first update this client reads. let's use this as current AND old position.
|
||||
m_NetworkPosition = readPosition;
|
||||
}
|
||||
|
||||
// the previously received position becomes the old(er) one and queued. the new one is the m_NetworkPosition
|
||||
m_OldNetworkPositions.Enqueue(m_NetworkPosition);
|
||||
m_NetworkPosition = readPosition;
|
||||
|
||||
// reduce items in queue to defined number of stored positions.
|
||||
while (m_OldNetworkPositions.Count > m_Model.ExtrapolateNumberOfStoredPositions)
|
||||
{
|
||||
m_OldNetworkPositions.Dequeue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class PhotonTransformViewRotationModel
|
||||
{
|
||||
public enum InterpolateOptions
|
||||
{
|
||||
Disabled,
|
||||
RotateTowards,
|
||||
Lerp,
|
||||
}
|
||||
|
||||
|
||||
public bool SynchronizeEnabled;
|
||||
|
||||
public InterpolateOptions InterpolateOption = InterpolateOptions.RotateTowards;
|
||||
public float InterpolateRotateTowardsSpeed = 180;
|
||||
public float InterpolateLerpSpeed = 5;
|
||||
}
|
||||
|
||||
public class PhotonTransformViewRotationControl
|
||||
{
|
||||
PhotonTransformViewRotationModel m_Model;
|
||||
Quaternion m_NetworkRotation;
|
||||
|
||||
public PhotonTransformViewRotationControl(PhotonTransformViewRotationModel model)
|
||||
{
|
||||
m_Model = model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last rotation that was received through the network
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Quaternion GetNetworkRotation()
|
||||
{
|
||||
return m_NetworkRotation;
|
||||
}
|
||||
|
||||
public Quaternion GetRotation(Quaternion currentRotation)
|
||||
{
|
||||
switch (m_Model.InterpolateOption)
|
||||
{
|
||||
default:
|
||||
case PhotonTransformViewRotationModel.InterpolateOptions.Disabled:
|
||||
return m_NetworkRotation;
|
||||
case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
|
||||
return Quaternion.RotateTowards(currentRotation, m_NetworkRotation, m_Model.InterpolateRotateTowardsSpeed * Time.deltaTime);
|
||||
case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
|
||||
return Quaternion.Lerp(currentRotation, m_NetworkRotation, m_Model.InterpolateLerpSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(Quaternion currentRotation, PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (m_Model.SynchronizeEnabled == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.IsWriting == true)
|
||||
{
|
||||
stream.SendNext(currentRotation);
|
||||
m_NetworkRotation = currentRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_NetworkRotation = (Quaternion)stream.ReceiveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class PhotonTransformViewScaleModel
|
||||
{
|
||||
public enum InterpolateOptions
|
||||
{
|
||||
Disabled,
|
||||
MoveTowards,
|
||||
Lerp,
|
||||
}
|
||||
|
||||
|
||||
public bool SynchronizeEnabled;
|
||||
|
||||
public InterpolateOptions InterpolateOption = InterpolateOptions.Disabled;
|
||||
public float InterpolateMoveTowardsSpeed = 1f;
|
||||
public float InterpolateLerpSpeed;
|
||||
}
|
||||
|
||||
public class PhotonTransformViewScaleControl
|
||||
{
|
||||
PhotonTransformViewScaleModel m_Model;
|
||||
Vector3 m_NetworkScale = Vector3.one;
|
||||
|
||||
public PhotonTransformViewScaleControl(PhotonTransformViewScaleModel model)
|
||||
{
|
||||
m_Model = model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last scale that was received through the network
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Vector3 GetNetworkScale()
|
||||
{
|
||||
return m_NetworkScale;
|
||||
}
|
||||
|
||||
public Vector3 GetScale(Vector3 currentScale)
|
||||
{
|
||||
switch (m_Model.InterpolateOption)
|
||||
{
|
||||
default:
|
||||
case PhotonTransformViewScaleModel.InterpolateOptions.Disabled:
|
||||
return m_NetworkScale;
|
||||
case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
|
||||
return Vector3.MoveTowards(currentScale, m_NetworkScale, m_Model.InterpolateMoveTowardsSpeed * Time.deltaTime);
|
||||
case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
|
||||
return Vector3.Lerp(currentScale, m_NetworkScale, m_Model.InterpolateLerpSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPhotonSerializeView(Vector3 currentScale, PhotonStream stream, PhotonMessageInfo info)
|
||||
{
|
||||
if (m_Model.SynchronizeEnabled == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.IsWriting == true)
|
||||
{
|
||||
stream.SendNext(currentScale);
|
||||
m_NetworkScale = currentScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_NetworkScale = (Vector3)stream.ReceiveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8efc6b1d64977384eb3405357896c656
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user