温锦文欧普厨电及净水器总代理 发表于 2022-11-16 08:04:57

简易血槽残影设计方案(使用协程)

  今天看了段DNF视频,有发现到血条变化效果是这样的:
 https://img2022.cnblogs.com/blog/1695941/202211/1695941-20221116020930879-2112438963.gif
 
  这里为了突出Boss受到的伤害之大,也就是玩家的伤害之高,以至于Boss的血条变化会出现残影效果。
  那么,就简单使用协程来实现了一下这种效果:
  https://img2022.cnblogs.com/blog/1695941/202211/1695941-20221116021722717-815240182.gif
 
  实现思路也蛮简单的:就是在Canvas下创建两个Slider,分别是Slider和Slider01,先将每个Slider中的Fill Area下的Fill拖到其父项下,然后除了Background、Fill,其余子项全部删除,再将Slider01放入Slider中。
  这里,就把Slider01作为Slider的残影。
  因为想最快写出效果,所以就直接用了协程来实现的,当然,如果在实际项目中有很多需要优化的地方。
  把脚本绑定到最外边的Slider组件下。
  [锤子猫原创代码,转载请标注来源]
  测试代码如下:
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 public class HealControl : MonoBehaviour
7 {
8   public float curHeal; //当前血量
9
10   public float allHeal; //总血量
11
12   public float reHeal; //增量
13
14   public float reTime = 0.01f; //递减时间
15
16   public float[] tempData; //临时数据组
17
18   public Slider fatherSlider, sonSlider; //父子血条
19
20   // Start is called before the first frame update
21   void Start()
22   {
23         tempData = new float { 0f, 0f }; //初始化 0:旧父血条值1:子血条值
24
25         fatherSlider = this.GetComponent<Slider>(); //获取父血条组件
26         sonSlider = this.GetComponent<Slider>().transform.GetChild(1).GetComponent<Slider>(); //获取子血条组件
27
28         tempData = curHeal; //给旧父血条值赋初值
29         tempData = tempData; //给子血条值赋初值
30
31         fatherSlider.value = curHeal / allHeal; //计算血量值后赋给血条
32         sonSlider.value = tempData / allHeal; //计算子血量值后赋给血条
33   }
34
35   // Update is called once per frame
36   void Update()
37   {
38         Blood0peration();
39   }
40
41   public void Blood0peration() //血量值换算成血条值
42   {
43         if (curHeal < tempData && Mathf.Abs(curHeal - tempData) > 0.1f) //判断父血量发生变化
44         {
45             StartCoroutine(TimerForFBloodReduce());
46         }
47         else if (curHeal > tempData && Mathf.Abs(curHeal - tempData) > 0.1f)
48         {
49             StartCoroutine(TimerForFBloodAdd());
50         }
51         else
52         {
53             StopCoroutine(TimerForFBloodReduce());
54             StopCoroutine(TimerForFBloodAdd());
55             tempData = curHeal;
56         }
57
58         if (curHeal < tempData && Mathf.Abs(curHeal - tempData) > 0.1f) //判断子血量发生变化
59         {
60             StartCoroutine(TimerForSBloodReduce());
61         }
62         else
63         {
64             StopCoroutine(TimerForSBloodReduce());
65             tempData = curHeal;
66         }
67   }
68
69   private IEnumerator TimerForFBloodReduce()
70   {
71         while (Mathf.Abs(curHeal - tempData) > 0.1f)
72         {
73             tempData -= reHeal;
74             fatherSlider.value = tempData / allHeal;
75             yield return new WaitForSeconds(reTime);
76         }
77   } //父血条计算定时器-增
78
79   private IEnumerator TimerForFBloodAdd()
80   {
81         while (Mathf.Abs(curHeal - tempData) > 0.1f)
82         {
83             tempData += reHeal;
84             fatherSlider.value = tempData / allHeal;
85             yield return new WaitForSeconds(reTime);
86         }
87   } //父血条计算定时器-减
88
89   private IEnumerator TimerForSBloodReduce()
90   {
91         while (Mathf.Abs(curHeal - tempData) > 0.1f)
92         {
93             tempData -= reHeal;
94             sonSlider.value = tempData / allHeal;
95             yield return new WaitForSeconds(reTime + 0.02f);
96         }
97   } //子血条计算定时器-减
98 }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 简易血槽残影设计方案(使用协程)