UIController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.InputSystem;
  5. namespace Unity.RenderStreaming.Samples
  6. {
  7. [RequireComponent(typeof(RectTransform))]
  8. class UIController : MonoBehaviour
  9. {
  10. [SerializeField] Text text;
  11. [SerializeField] CanvasGroup canvasGroup;
  12. [SerializeField] Image pointer;
  13. [SerializeField] GameObject noticeTouchControl;
  14. [SerializeField] private AnimationCurve transitionCurve =
  15. new AnimationCurve(
  16. new Keyframe(0.75f, 1f, 0f, 0f),
  17. new Keyframe(1f, 0f, 0f, 0f));
  18. private float timeTransition = 0f;
  19. private Color transparentColor = new Color(0, 0, 0, 0);
  20. private RectTransform m_rectTransform = null;
  21. private Keyboard m_keyboard;
  22. private Mouse m_mouse;
  23. private Touchscreen m_screen;
  24. public void SetDevice(InputDevice device, bool add = false)
  25. {
  26. switch (device)
  27. {
  28. case Mouse mouse:
  29. m_mouse = add ? mouse : null;
  30. return;
  31. case Keyboard keyboard:
  32. m_keyboard = add ? keyboard : null;
  33. if (add)
  34. m_keyboard.onTextInput += OnTextInput;
  35. return;
  36. case Touchscreen screen:
  37. m_screen = add ? screen : null;
  38. if (noticeTouchControl != null)
  39. {
  40. noticeTouchControl.SetActive(add);
  41. }
  42. return;
  43. }
  44. }
  45. void Start()
  46. {
  47. m_rectTransform = GetComponent<RectTransform>();
  48. canvasGroup.alpha = 0;
  49. text.text = string.Empty;
  50. }
  51. void FixedUpdate()
  52. {
  53. if (m_keyboard != null && !m_keyboard.anyKey.isPressed &&
  54. !Mathf.Approximately(canvasGroup.alpha, 0f))
  55. {
  56. timeTransition += Time.deltaTime;
  57. canvasGroup.alpha = transitionCurve.Evaluate(timeTransition);
  58. if (Mathf.Approximately(canvasGroup.alpha, 0f))
  59. {
  60. text.text = string.Empty;
  61. }
  62. }
  63. bool pointerFromMouse = HighlightPointerFromMouse(
  64. m_mouse, new Vector2Int(Screen.width, Screen.height));
  65. if (pointerFromMouse)
  66. return;
  67. var touches = m_screen?.GetTouches();
  68. if (touches != null && touches.Count() > 0)
  69. {
  70. var position = Vector2.zero;
  71. var count = touches.Count();
  72. var activeTouches = touches.ToArray();
  73. for (var i = 0; i < count; i++)
  74. {
  75. position += activeTouches[i].screenPosition;
  76. }
  77. pointer.rectTransform.anchoredPosition = position / (float)count;
  78. pointer.color = Color.red;
  79. }
  80. else
  81. {
  82. pointer.color = transparentColor;
  83. }
  84. }
  85. //----------------------------------------------------------------------------------------------------------------------
  86. bool HighlightPointerFromMouse(Mouse mouse, Vector2Int screenSize)
  87. {
  88. if (mouse == null)
  89. return false;
  90. if (!Screen.safeArea.Contains(mouse.position.ReadValue()))
  91. return false;
  92. if (!mouse.leftButton.isPressed && !mouse.rightButton.isPressed)
  93. return false;
  94. Vector2 mousePos = mouse.position.ReadValue();
  95. Vector2 pos = mousePos / screenSize * new Vector2(m_rectTransform.rect.width, m_rectTransform.rect.height);
  96. pointer.rectTransform.anchoredPosition = pos;
  97. pointer.color = Color.red;
  98. return true;
  99. }
  100. void OnTextInput(char c)
  101. {
  102. canvasGroup.alpha = 1f;
  103. text.text = c.ToString();
  104. timeTransition = 0;
  105. }
  106. }
  107. }