Example.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SC.XR.Unity.Module_Keyboard
  6. {
  7. public class Example : MonoBehaviour
  8. {
  9. SCKeyboard m_Keyboard;
  10. public Vector3 m_Position;
  11. public Vector3 m_Rotation;
  12. public Vector3 m_Scale;
  13. public Transform m_Parent;
  14. public SCKeyboardEnum m_SCKeyboardEnum;
  15. public Button chineseBtn;
  16. public Button englishBtn;
  17. public SCToggleCheckbox3D useSystemLanguageToggle;
  18. void Start()
  19. {
  20. // Register static Events.
  21. SCKeyboard.OnActiveStateChanged += OnActiveStateChanged;
  22. SCKeyboard.RegisterOnWebKeyClickEvent(OnWebKeyClickEvent);
  23. chineseBtn.onClick.AddListener(OnClickChineseBtn);
  24. englishBtn.onClick.AddListener(OnClickEnglishBtn);
  25. useSystemLanguageToggle.onValueChanged.AddListener(OnUseSystemLanguageToggleValueChanged);
  26. }
  27. private void OnDestroy()
  28. {
  29. chineseBtn.onClick.RemoveListener(OnClickChineseBtn);
  30. englishBtn.onClick.RemoveListener(OnClickEnglishBtn);
  31. useSystemLanguageToggle.onValueChanged.RemoveListener(OnUseSystemLanguageToggleValueChanged);
  32. }
  33. void Update()
  34. {
  35. // Open Keyboard.
  36. if (Input.GetKeyDown(KeyCode.O))
  37. {
  38. m_Keyboard = SCKeyboard.Open(SCKeyboardEnum.SCKeyboard2D, "", SCKeyboardType.Default, m_Position, Quaternion.Euler(m_Rotation), m_Scale);
  39. }
  40. if (Input.GetKeyDown(KeyCode.L))
  41. {
  42. m_Keyboard = SCKeyboard.Open(SCKeyboardEnum.SCKeyboard2D, "");
  43. }
  44. if (Input.GetKeyDown(KeyCode.M))
  45. {
  46. if (m_Keyboard != null) m_Keyboard.transform.position = m_Position;
  47. if (m_Keyboard != null) m_Keyboard.transform.rotation = Quaternion.Euler(m_Rotation);
  48. if (m_Keyboard != null) m_Keyboard.transform.localScale = m_Scale;
  49. }
  50. if (Input.GetKeyDown(KeyCode.P))
  51. {
  52. if (m_Keyboard != null) m_Keyboard.transform.SetParent(m_Parent);
  53. }
  54. // Close Keyboard.
  55. if (Input.GetKeyDown(KeyCode.C))
  56. {
  57. if (m_Keyboard != null) SCKeyboard.Close(m_SCKeyboardEnum);
  58. }
  59. // Register button press event.
  60. if (Input.GetKeyDown(KeyCode.R))
  61. {
  62. if (m_Keyboard != null) m_Keyboard.RegisterOnKeyClickEvent(OnKeyClickEvent);
  63. }
  64. }
  65. public void OnActiveStateChanged(bool val)
  66. {
  67. Debug.Log("OnActiveStateChanged :"+val);
  68. }
  69. public void OnKeyClickEvent(string val)
  70. {
  71. Debug.Log("OnKeyClickEvent :" + val);
  72. }
  73. public void OnWebKeyClickEvent(string val)
  74. {
  75. Debug.Log("OnWebKeyClickEvent :" + val);
  76. }
  77. public void OnClickChineseBtn()
  78. {
  79. SCKeyboard.SetSystemLanguage(KeyboardSystemLanguage.Chinese);
  80. chineseBtn.GetComponentInChildren<Text>().text = "设置键盘系统语言为中文";
  81. englishBtn.GetComponentInChildren<Text>().text = "设置键盘系统语言为英文";
  82. useSystemLanguageToggle.GetComponentInChildren<TextMesh>().text = "自动获取系统语言";
  83. }
  84. public void OnClickEnglishBtn()
  85. {
  86. SCKeyboard.SetSystemLanguage(KeyboardSystemLanguage.English);
  87. chineseBtn.GetComponentInChildren<Text>().text = "Change Keyboard System Language To Chinese";
  88. englishBtn.GetComponentInChildren<Text>().text = "Change Keyboard System Language To English";
  89. useSystemLanguageToggle.GetComponentInChildren<TextMesh>().text = "Auto Get System Language";
  90. }
  91. public void OnUseSystemLanguageToggleValueChanged(bool value)
  92. {
  93. SCKeyboard.useSystemLanguage = value;
  94. }
  95. }
  96. }