CUI_ChangeValueOnHold.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace CurvedUI
  6. {
  7. public class CUI_ChangeValueOnHold : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  8. {
  9. bool pressed = false;
  10. bool selected = false;
  11. #pragma warning disable 0649
  12. [SerializeField]
  13. Image bg;
  14. [SerializeField]
  15. Color SelectedColor;
  16. [SerializeField]
  17. Color NormalColor;
  18. [SerializeField]
  19. CanvasGroup IntroCG;
  20. [SerializeField]
  21. CanvasGroup MenuCG;
  22. #pragma warning restore 0649
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. pressed = Input.GetKey(KeyCode.Space) || Input.GetButton("Fire1");
  27. ChangeVal();
  28. }
  29. void ChangeVal()
  30. {
  31. if (this.GetComponent<Slider>().normalizedValue == 1)
  32. {
  33. //fade intro screen if we reached max slider value
  34. IntroCG.alpha -= Time.deltaTime;
  35. MenuCG.alpha += Time.deltaTime;
  36. }
  37. else {
  38. //change slider value - increase if its selected and button is pressed
  39. this.GetComponent<Slider>().normalizedValue += (pressed && selected) ? Time.deltaTime : -Time.deltaTime;
  40. }
  41. //change if intro screen can block interactions based on its opacity
  42. IntroCG.blocksRaycasts = IntroCG.alpha > 0;
  43. }
  44. public void OnPointerEnter(PointerEventData data)
  45. {
  46. bg.color = SelectedColor;
  47. bg.GetComponent<CurvedUIVertexEffect>().TesselationRequired = true;
  48. selected = true;
  49. }
  50. public void OnPointerExit(PointerEventData data)
  51. {
  52. bg.color = NormalColor;
  53. bg.GetComponent<CurvedUIVertexEffect>().TesselationRequired = true;
  54. selected = false;
  55. }
  56. }
  57. }