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

  今天看了段DNF视频,有发现到血条变化效果是这样的:

 

 

  这里为了突出Boss受到的伤害之大,也就是玩家的伤害之高,以至于Boss的血条变化会出现残影效果。

  那么,就简单使用协程来实现了一下这种效果:

  

 

  实现思路也蛮简单的:就是在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[2] { 0f, 0f }; //初始化 0:旧父血条值  1:子血条值
24 
25         fatherSlider = this.GetComponent<Slider>(); //获取父血条组件
26         sonSlider = this.GetComponent<Slider>().transform.GetChild(1).GetComponent<Slider>(); //获取子血条组件
27 
28         tempData[0] = curHeal; //给旧父血条值赋初值
29         tempData[1] = tempData[0]; //给子血条值赋初值
30 
31         fatherSlider.value = curHeal / allHeal; //计算血量值后赋给血条
32         sonSlider.value = tempData[0] / 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[0] && Mathf.Abs(curHeal - tempData[0]) > 0.1f) //判断父血量发生变化
44         {
45             StartCoroutine(TimerForFBloodReduce());
46         }
47         else if (curHeal > tempData[0] && Mathf.Abs(curHeal - tempData[0]) > 0.1f)
48         {
49             StartCoroutine(TimerForFBloodAdd());
50         }
51         else
52         {
53             StopCoroutine(TimerForFBloodReduce());
54             StopCoroutine(TimerForFBloodAdd());
55             tempData[0] = curHeal;
56         }
57 
58         if (curHeal < tempData[1] && Mathf.Abs(curHeal - tempData[1]) > 0.1f) //判断子血量发生变化
59         {
60             StartCoroutine(TimerForSBloodReduce());
61         }
62         else
63         {
64             StopCoroutine(TimerForSBloodReduce());
65             tempData[1] = curHeal;
66         }
67     }
68 
69     private IEnumerator TimerForFBloodReduce()
70     {
71         while (Mathf.Abs(curHeal - tempData[0]) > 0.1f)
72         {
73             tempData[0] -= reHeal;
74             fatherSlider.value = tempData[0] / allHeal;
75             yield return new WaitForSeconds(reTime);
76         }
77     } 
78 
79     private IEnumerator TimerForFBloodAdd()
80     {
81         while (Mathf.Abs(curHeal - tempData[0]) > 0.1f)
82         {
83             tempData[0] += reHeal;
84             fatherSlider.value = tempData[0] / allHeal;
85             yield return new WaitForSeconds(reTime);
86         }
87     } 
88 
89     private IEnumerator TimerForSBloodReduce()
90     {
91         while (Mathf.Abs(curHeal - tempData[1]) > 0.1f)
92         {
93             tempData[1] -= reHeal;
94             sonSlider.value = tempData[1] / allHeal;
95             yield return new WaitForSeconds(reTime + 0.02f);
96         }
97     } 
98 }
posted @ 2022-11-16 02:36  锤子猫  阅读(238)  评论(0编辑  收藏  举报