PhotonLagSimulationGui.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PhotonLagSimulationGui.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities,
  4. // </copyright>
  5. // <summary>
  6. // This MonoBehaviour is a basic GUI for the Photon client's network-simulation feature.
  7. // It can modify lag (fixed delay), jitter (random lag) and packet loss.
  8. // Part of the [Optional GUI](@ref optionalGui).
  9. // </summary>
  10. // <author>developer@exitgames.com</author>
  11. // --------------------------------------------------------------------------------------------------------------------
  12. using UnityEngine;
  13. using Photon.Pun;
  14. using Photon.Realtime;
  15. using ExitGames.Client.Photon;
  16. namespace Photon.Pun.UtilityScripts
  17. {
  18. /// <summary>
  19. /// This MonoBehaviour is a basic GUI for the Photon client's network-simulation feature.
  20. /// It can modify lag (fixed delay), jitter (random lag) and packet loss.
  21. /// </summary>
  22. /// \ingroup optionalGui
  23. public class PhotonLagSimulationGui : MonoBehaviour
  24. {
  25. /// <summary>Positioning rect for window.</summary>
  26. public Rect WindowRect = new Rect(0, 100, 120, 100);
  27. /// <summary>Unity GUI Window ID (must be unique or will cause issues).</summary>
  28. public int WindowId = 101;
  29. /// <summary>Shows or hides GUI (does not affect settings).</summary>
  30. public bool Visible = true;
  31. /// <summary>The peer currently in use (to set the network simulation).</summary>
  32. public PhotonPeer Peer { get; set; }
  33. public void Start()
  34. {
  35. this.Peer = PhotonNetwork.NetworkingClient.LoadBalancingPeer;
  36. }
  37. public void OnGUI()
  38. {
  39. if (!this.Visible)
  40. {
  41. return;
  42. }
  43. if (this.Peer == null)
  44. {
  45. this.WindowRect = GUILayout.Window(this.WindowId, this.WindowRect, this.NetSimHasNoPeerWindow, "Netw. Sim.");
  46. }
  47. else
  48. {
  49. this.WindowRect = GUILayout.Window(this.WindowId, this.WindowRect, this.NetSimWindow, "Netw. Sim.");
  50. }
  51. }
  52. private void NetSimHasNoPeerWindow(int windowId)
  53. {
  54. GUILayout.Label("No peer to communicate with. ");
  55. }
  56. private void NetSimWindow(int windowId)
  57. {
  58. GUILayout.Label(string.Format("Rtt:{0,4} +/-{1,3}", this.Peer.RoundTripTime, this.Peer.RoundTripTimeVariance));
  59. bool simEnabled = this.Peer.IsSimulationEnabled;
  60. bool newSimEnabled = GUILayout.Toggle(simEnabled, "Simulate");
  61. if (newSimEnabled != simEnabled)
  62. {
  63. this.Peer.IsSimulationEnabled = newSimEnabled;
  64. }
  65. float inOutLag = this.Peer.NetworkSimulationSettings.IncomingLag;
  66. GUILayout.Label("Lag " + inOutLag);
  67. inOutLag = GUILayout.HorizontalSlider(inOutLag, 0, 500);
  68. this.Peer.NetworkSimulationSettings.IncomingLag = (int)inOutLag;
  69. this.Peer.NetworkSimulationSettings.OutgoingLag = (int)inOutLag;
  70. float inOutJitter = this.Peer.NetworkSimulationSettings.IncomingJitter;
  71. GUILayout.Label("Jit " + inOutJitter);
  72. inOutJitter = GUILayout.HorizontalSlider(inOutJitter, 0, 100);
  73. this.Peer.NetworkSimulationSettings.IncomingJitter = (int)inOutJitter;
  74. this.Peer.NetworkSimulationSettings.OutgoingJitter = (int)inOutJitter;
  75. float loss = this.Peer.NetworkSimulationSettings.IncomingLossPercentage;
  76. GUILayout.Label("Loss " + loss);
  77. loss = GUILayout.HorizontalSlider(loss, 0, 10);
  78. this.Peer.NetworkSimulationSettings.IncomingLossPercentage = (int)loss;
  79. this.Peer.NetworkSimulationSettings.OutgoingLossPercentage = (int)loss;
  80. // if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
  81. if (GUI.changed)
  82. {
  83. this.WindowRect.height = 100;
  84. }
  85. GUI.DragWindow();
  86. }
  87. }
  88. }