SendRateField.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SendRateField.cs" company="Exit Games GmbH">
  3. // Part of: Pun Cockpit Demo
  4. // </copyright>
  5. // <author>developer@exitgames.com</author>
  6. // --------------------------------------------------------------------------------------------------------------------
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace Photon.Pun.Demo.Cockpit
  10. {
  11. /// <summary>
  12. /// PhotonNetwork.SendRate InputField.
  13. /// </summary>
  14. public class SendRateField : MonoBehaviour
  15. {
  16. public InputField PropertyValueInput;
  17. int _cache;
  18. bool registered;
  19. void OnEnable()
  20. {
  21. if (!registered)
  22. {
  23. registered = true;
  24. PropertyValueInput.onEndEdit.AddListener(OnEndEdit);
  25. }
  26. }
  27. void OnDisable()
  28. {
  29. registered = false;
  30. PropertyValueInput.onEndEdit.RemoveListener(OnEndEdit);
  31. }
  32. void Update()
  33. {
  34. if (PhotonNetwork.SendRate != _cache)
  35. {
  36. _cache = PhotonNetwork.SendRate;
  37. PropertyValueInput.text = _cache.ToString();
  38. }
  39. }
  40. // new UI will fire "EndEdit" event also when loosing focus. So check "enter" key and only then StartChat.
  41. public void OnEndEdit(string value)
  42. {
  43. if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter) || Input.GetKey(KeyCode.Tab))
  44. {
  45. this.SubmitForm(value.Trim());
  46. }
  47. else
  48. {
  49. this.SubmitForm(value);
  50. }
  51. }
  52. public void SubmitForm(string value)
  53. {
  54. _cache = int.Parse(value);
  55. PhotonNetwork.SendRate = _cache;
  56. //Debug.Log("PhotonNetwork.SendRate = " + PhotonNetwork.SendRate, this);
  57. }
  58. }
  59. }