SendRateOnSerializeField.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SendRateOnSerializeField.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.SerializationRate InputField
  13. /// </summary>
  14. public class SendRateOnSerializeField : 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.SerializationRate != _cache)
  35. {
  36. _cache = PhotonNetwork.SerializationRate;
  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(PropertyValueInput.text);
  55. PhotonNetwork.SerializationRate = _cache;
  56. //Debug.Log("PhotonNetwork.SerializationRate = " + PhotonNetwork.SerializationRate, this);
  57. }
  58. }
  59. }