WebGLInputManipulator.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. namespace WebGLSupport
  4. {
  5. public class WebGLInputManipulator : Manipulator
  6. {
  7. private GameObject go;
  8. private bool showHtmlElement;
  9. public WebGLInputManipulator(bool showHtmlElement = false)
  10. {
  11. this.showHtmlElement = showHtmlElement;
  12. }
  13. protected override void RegisterCallbacksOnTarget()
  14. {
  15. // uitoolkit is already support mobile.
  16. if (!Application.isMobilePlatform)
  17. {
  18. var textInput = target.Q("unity-text-input");
  19. textInput.RegisterCallback<FocusInEvent>(OnFocusInEvent);
  20. textInput.RegisterCallback<FocusOutEvent>(OnFocusOutEvent);
  21. }
  22. }
  23. protected override void UnregisterCallbacksFromTarget()
  24. {
  25. // uitoolkit is already support mobile.
  26. if (!Application.isMobilePlatform)
  27. {
  28. var textInput = target.Q("unity-text-input");
  29. textInput.UnregisterCallback<FocusInEvent>(OnFocusInEvent);
  30. textInput.UnregisterCallback<FocusOutEvent>(OnFocusOutEvent);
  31. }
  32. }
  33. private void OnFocusInEvent(FocusInEvent evt)
  34. {
  35. if (go != null)
  36. {
  37. GameObject.Destroy(go);
  38. go = null;
  39. }
  40. go = new GameObject("WebGLInputManipulator");
  41. // add WebGLUIToolkitMonoBehaviour for hold TextField!
  42. var uitoolkit = go.AddComponent<WebGLUIToolkitTextField>();
  43. uitoolkit.TextField = target as TextField;
  44. // add WebGLInput to handle the event!
  45. var webglInput = go.AddComponent<WebGLInput>();
  46. webglInput.showHtmlElement = showHtmlElement;
  47. // select it!!
  48. webglInput.OnSelect();
  49. }
  50. private void OnFocusOutEvent(FocusOutEvent evt)
  51. {
  52. if (go != null)
  53. {
  54. GameObject.Destroy(go);
  55. go = null;
  56. }
  57. }
  58. }
  59. }