NickNameField.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="NickNameField.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. /// Nickname InputField.
  13. /// </summary>
  14. public class NickNameField : MonoBehaviour
  15. {
  16. public InputField PropertyValueInput;
  17. string _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.NickName != _cache)
  35. {
  36. _cache = PhotonNetwork.NickName;
  37. PropertyValueInput.text = _cache;
  38. }
  39. }
  40. // new UI will fire "EndEdit" event also when loosing focus. So check "enter" key and only then submit form.
  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 = value;
  55. PhotonNetwork.NickName = _cache;
  56. //Debug.Log("PhotonNetwork.NickName = " + PhotonNetwork.NickName, this);
  57. }
  58. }
  59. }