当前位置:   article > 正文

unity UI闪烁

unity ui闪烁
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class UIPalpating : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 物体的Image组件
  9. /// </summary>
  10. Image UIImage = null;
  11. /// <summary>
  12. /// 当前颜色
  13. /// </summary>
  14. Color UIColor = new Color(1, 1, 1, 1);
  15. /// <summary>
  16. /// 保存原有状态
  17. /// </summary>
  18. float oldColorValue = 0;
  19. /// <summary>
  20. /// 闪烁标志
  21. /// </summary>
  22. bool palpatFlag = true;
  23. /// <summary>
  24. /// 闪烁速度
  25. /// </summary>
  26. [Tooltip("闪烁速度")]
  27. [Range(1f, 15f)] public float PalpatSpeed = 1f;
  28. /// <summary>
  29. /// 闪烁标志
  30. /// </summary>
  31. [Tooltip("闪烁标志")]
  32. public bool isOn = false;
  33. void Start()
  34. {
  35. UIImage = GetComponent<Image>();
  36. if (UIImage != null)
  37. {
  38. UIColor = UIImage.color;
  39. oldColorValue = UIColor.a;
  40. }
  41. }
  42. void Update()
  43. {
  44. try
  45. {
  46. if (isOn && UIImage != null)
  47. {
  48. UIAutoPalpating();
  49. }
  50. }
  51. catch (System.Exception ex)
  52. {
  53. Debug.Log(ex.Message);
  54. }
  55. }
  56. /// <summary>
  57. /// UI闪动
  58. /// </summary>
  59. void UIAutoPalpating()
  60. {
  61. if (UIImage.color.a <= 0)
  62. {
  63. palpatFlag = true; // 加
  64. }
  65. else if (UIImage.color.a >= oldColorValue)
  66. {
  67. palpatFlag = false; // 减
  68. }
  69. if (palpatFlag)
  70. {
  71. UIColor.a += Time.deltaTime * 0.5f * PalpatSpeed;
  72. }
  73. else
  74. {
  75. UIColor.a -= Time.deltaTime * 0.5f * PalpatSpeed;
  76. }
  77. UIImage.color = UIColor;
  78. }
  79. /// <summary>
  80. /// UI恢复
  81. /// </summary>
  82. public void UIAutoReset()
  83. {
  84. isOn = false;
  85. UIColor.a = 1f;
  86. UIImage.color = UIColor;
  87. }
  88. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/110118
推荐阅读
相关标签
  

闽ICP备14008679号