Custom_SCInputField.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace SC.XR.Unity.Module_Keyboard
  7. {
  8. public class Custom_SCInputField : Selectable
  9. {
  10. public Text text;
  11. public Vector3 m_Position;
  12. public Vector3 m_Rotation;
  13. public Vector3 m_Scale;
  14. protected SCKeyboard m_Keyboard;
  15. private bool shouldDeactivateOnDeselect
  16. {
  17. get
  18. {
  19. //TODO
  20. return (m_Keyboard == null || m_Keyboard.status > 0)
  21. && EventSystem.current.currentSelectedGameObject != this.gameObject;
  22. }
  23. }
  24. private bool m_ShouldDeactivateNextUpdate = false;
  25. private void LateUpdate()
  26. {
  27. #region SCKeyboard
  28. if (m_Keyboard != null && m_Keyboard.status > 0 && EventSystem.current.currentSelectedGameObject != this.gameObject)
  29. {
  30. this.m_ShouldDeactivateNextUpdate = true;
  31. }
  32. bool shouldDeactivateNextUpdate = this.m_ShouldDeactivateNextUpdate;
  33. if (shouldDeactivateNextUpdate)
  34. {
  35. OnDeselect(null);
  36. this.m_ShouldDeactivateNextUpdate = false;
  37. }
  38. #endregion
  39. }
  40. public override void OnDeselect(BaseEventData eventData)
  41. {
  42. if (shouldDeactivateOnDeselect)
  43. {
  44. //this.DeactivateInputField();
  45. base.OnDeselect(eventData);
  46. SCKeyboard.Close(SCKeyboardEnum.SCKeyboard2D);
  47. m_Keyboard = null;
  48. }
  49. }
  50. public override void OnSelect(BaseEventData eventData)
  51. {
  52. base.OnSelect(eventData);
  53. m_Keyboard = SCKeyboard.Open(SCKeyboardEnum.SCKeyboard2D, "", SCKeyboardType.Default, m_Position, Quaternion.Euler(m_Rotation), m_Scale);
  54. m_Keyboard.transform.SetParent(transform);
  55. SetKeyboardRegistEvents(m_Keyboard);
  56. }
  57. private void SetKeyboardRegistEvents(SCKeyboard scKeyboard)
  58. {
  59. scKeyboard.ClearOnKeyClickEvent();
  60. scKeyboard.ClearOnSpecialKeyClickEvent(SpecialKeyEnum.Delete);
  61. scKeyboard.RegisterOnKeyClickEvent((str) => { text.text += str; });
  62. scKeyboard.RegisterOnSpecialKeyClickEvent(SpecialKeyEnum.Delete, ()=> {
  63. if (text.text.Length == 0)
  64. {
  65. return;
  66. }
  67. text.text = text.text.Substring(0, text.text.Length - 1);
  68. });
  69. }
  70. }
  71. }