UIControllerV2.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.InputSystem;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. [RequireComponent(typeof(RectTransform))]
  7. class UIControllerV2 : MonoBehaviour
  8. {
  9. [SerializeField] Text text;
  10. [SerializeField] CanvasGroup canvasGroup;
  11. [SerializeField] Image pointer;
  12. [SerializeField] GameObject noticeTouchControl;
  13. [SerializeField] private AnimationCurve transitionCurve =
  14. new AnimationCurve(
  15. new Keyframe(0.75f, 1f, 0f, 0f),
  16. new Keyframe(1f, 0f, 0f, 0f));
  17. private float timeTransition = 0f;
  18. private RectTransform m_rectTransform = null;
  19. private bool isSubscribing = false;
  20. public void SetDevice(InputDevice device, bool add = false)
  21. {
  22. }
  23. void Start()
  24. {
  25. m_rectTransform = GetComponent<RectTransform>();
  26. canvasGroup.alpha = 0;
  27. text.text = string.Empty;
  28. }
  29. private void FixedUpdate()
  30. {
  31. if (!Mathf.Approximately(canvasGroup.alpha, 0f))
  32. {
  33. timeTransition += Time.deltaTime;
  34. canvasGroup.alpha = transitionCurve.Evaluate(timeTransition);
  35. }
  36. }
  37. public void OnPressAnyKey(InputAction.CallbackContext context)
  38. {
  39. var keyboard = context.control.device as Keyboard;
  40. if(!isSubscribing)
  41. {
  42. keyboard.onTextInput += OnTextInput;
  43. isSubscribing = true;
  44. }
  45. }
  46. void OnTextInput(char c)
  47. {
  48. canvasGroup.alpha = 1f;
  49. text.text = c.ToString();
  50. timeTransition = 0;
  51. }
  52. public void OnPoint(InputAction.CallbackContext context)
  53. {
  54. if(m_rectTransform == null)
  55. return;
  56. var position = context.ReadValue<Vector2>();
  57. var screenSize = new Vector2Int(Screen.width, Screen.height);
  58. position = position / screenSize * new Vector2(m_rectTransform.rect.width, m_rectTransform.rect.height);
  59. pointer.rectTransform.anchoredPosition = position;
  60. }
  61. public void OnPress(InputAction.CallbackContext context)
  62. {
  63. var button = context.ReadValueAsButton();
  64. pointer.color = button ? Color.red : Color.clear;
  65. }
  66. }
  67. }