NamePickGui.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright company="Exit Games GmbH"/>
  3. // <summary>Demo code for Photon Chat in Unity.</summary>
  4. // <author>developer@exitgames.com</author>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace Photon.Chat.Demo
  9. {
  10. [RequireComponent(typeof(ChatGui))]
  11. public class NamePickGui : MonoBehaviour
  12. {
  13. private const string UserNamePlayerPref = "NamePickUserName";
  14. public ChatGui chatNewComponent;
  15. public InputField idInput;
  16. public void Start()
  17. {
  18. this.chatNewComponent = FindObjectOfType<ChatGui>();
  19. string prefsName = PlayerPrefs.GetString(UserNamePlayerPref);
  20. if (!string.IsNullOrEmpty(prefsName))
  21. {
  22. this.idInput.text = prefsName;
  23. }
  24. }
  25. // new UI will fire "EndEdit" event also when loosing focus. So check "enter" key and only then StartChat.
  26. public void EndEditOnEnter()
  27. {
  28. if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))
  29. {
  30. this.StartChat();
  31. }
  32. }
  33. public void StartChat()
  34. {
  35. ChatGui chatNewComponent = FindObjectOfType<ChatGui>();
  36. chatNewComponent.UserName = this.idInput.text.Trim();
  37. chatNewComponent.Connect();
  38. this.enabled = false;
  39. PlayerPrefs.SetString(UserNamePlayerPref, chatNewComponent.UserName);
  40. }
  41. }
  42. }