UserIdField.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="UserIdField.cs" company="Exit Games GmbH">
  3. // Part of: Pun Cockpit
  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. /// User identifier InputField.
  13. /// </summary>
  14. public class UserIdField : MonoBehaviour
  15. {
  16. public PunCockpit Manager;
  17. public InputField PropertyValueInput;
  18. string _cache;
  19. bool registered;
  20. void OnEnable()
  21. {
  22. if (!registered)
  23. {
  24. registered = true;
  25. PropertyValueInput.onEndEdit.AddListener(OnEndEdit);
  26. }
  27. }
  28. void OnDisable()
  29. {
  30. registered = false;
  31. PropertyValueInput.onEndEdit.RemoveListener(OnEndEdit);
  32. }
  33. void Update()
  34. {
  35. if (Manager.UserId != _cache)
  36. {
  37. _cache = Manager.UserId;
  38. PropertyValueInput.text = _cache;
  39. }
  40. }
  41. // new UI will fire "EndEdit" event also when loosing focus. So check "enter" key and only then submit form.
  42. public void OnEndEdit(string value)
  43. {
  44. if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter) || Input.GetKey(KeyCode.Tab))
  45. {
  46. this.SubmitForm(value.Trim());
  47. }
  48. else
  49. {
  50. this.SubmitForm(value);
  51. }
  52. }
  53. public void SubmitForm(string value)
  54. {
  55. _cache = value;
  56. Manager.UserId = _cache;
  57. //Debug.Log("PunCockpit.UserId = " + Manager.UserId, this);
  58. }
  59. }
  60. }