当前位置:   article > 正文

Unity 通过修改图片透明度实现淡出效果_unity改变image透明度

unity改变image透明度

Sprite类型

绑定即可使用

物体被激活后立即执行

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class transparency : MonoBehaviour
  7. {
  8. //等待时间
  9. public float waittime=0;
  10. //渐变时长
  11. public float time=0.5f;
  12. //定时器
  13. public float timer;
  14. //透明度 范围是0-1
  15. float alpha = 0.9f;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. timer += Time.deltaTime;
  24. if(timer>waittime)
  25. {
  26. alpha = (time - (timer-waittime)) / time;
  27. gameObject.GetComponent<SpriteRenderer>().color = new Color(gameObject.GetComponent<SpriteRenderer>().color.r, gameObject.GetComponent<SpriteRenderer>().color.g, gameObject.GetComponent<SpriteRenderer>().color.b, alpha);
  28. }
  29. }
  30. }

被外界通知后即可生效

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class transparency : MonoBehaviour
  7. {
  8. //等待时间
  9. public float waittime = 0;
  10. //渐变时长
  11. public float time = 0.5f;
  12. //定时器
  13. public float timer;
  14. //透明度 范围是0-1
  15. float alpha = 0.9f;
  16. //启用状态
  17. public bool workingstatus = false;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if(workingstatus == true)
  26. {
  27. timer += Time.deltaTime;
  28. if (timer > waittime)
  29. {
  30. alpha = (time - (timer - waittime)) / time;
  31. gameObject.GetComponent<SpriteRenderer>().color = new Color(gameObject.GetComponent<SpriteRenderer>().color.r, gameObject.GetComponent<SpriteRenderer>().color.g, gameObject.GetComponent<SpriteRenderer>().color.b, alpha);
  32. }
  33. }
  34. if (timer > waittime + time)
  35. {
  36. workingstatus = false;
  37. }
  38. }
  39. }

在其他脚本调用以下代码

guidetip.GetComponent<transparency>().workingstatus = true;

Image类型

绑定即可使用

被外界通知后即可生效

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class transparency : MonoBehaviour
  7. {
  8. //等待时间
  9. public float waittime = 0;
  10. //渐变时长
  11. public float time = 0.5f;
  12. //定时器
  13. public float timer;
  14. //透明度 范围是0-1
  15. float alpha = 0.9f;
  16. //启用状态
  17. public bool workingstatus = false;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if(workingstatus==true)
  26. {
  27. timer += Time.deltaTime;
  28. if (timer > waittime)
  29. {
  30. alpha = (time - (timer - waittime)) / time;
  31. gameObject.GetComponent<Image>().color = new Color(gameObject.GetComponent<Image>().color.r, gameObject.GetComponent<Image>().color.g, gameObject.GetComponent<Image>().color.b, alpha);
  32. }
  33. if(timer>waittime+time)
  34. {
  35. workingstatus = false;
  36. }
  37. }
  38. }
  39. }

在其他脚本调用以下代码

guidemask.GetComponent<transparency>().workingstatus = true;

手动调节图片透明度

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

闽ICP备14008679号