本文主要是介绍多重血条效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
描述:
1.当受到伤害较小时,出现“残血”效果
2.当受到伤害较大时,出现“流水”效果
血条由4部分组成1.剩余学条数
2.当前血条剩余血量image
3.过度血条image
4.下一血条显示颜色 image
注意:这里的血条显示image 需要将Image.Type设置为Filled,fill Method设置为水平,
using UnityEngine;
using System.Collections;
using UnityEngine.UI;//如果只有一条血,那么一条血就是所有的血量
//如果有多条血,那么一条血就设定为一个固定值
public class MultiplyBloodBar : MonoBehaviour
{public Image nowBar; //当前血条public Image middleBar; //过渡血条public Image nextBar; //下一血条public Text countText; //剩下的血条数textprivate int count; //剩下的血条数(不包括当前血量)private float nowBlood; //在一条血中的当前血量,如:100/1000则为100 private float oneBarBlood = 10000f; //一条血的容量,如:100/1000则为1000 private int colorIndex = 0;public Color[] colors; //血条的颜色,注意Alpha值,默认为0private float slowSpeed = 0.1f; //受到重伤时( >oneBarBlood)或者处于加血状态,当前血条的流动速度 private float quickSpeed = 1f; //受到轻伤时( <oneBarBlood),当前血条的流动速度 private float speed; //当前血条采用的速度 private float middleBarSpeed = 0.1f; //过渡血条的流动速度 private float nowTargetValue; //当前血条移动的目标点 private float middleTargetValue; //过渡血条移动的目标点 private bool isBloodMove = false; //控制血条的移动 void Update(){MoveNowBar(); //当前血条的流动 MoveMiddleBar(); //过渡血条的流动 if (Input.GetMouseButtonDown(0)){ChangeBlood(-3000);}else if (Input.GetMouseButtonDown(1)){ChangeBlood(-15000);}}private void Awake(){}private void Start(){colors = new Color[] { new Color(1, 0, 0, 1), new Color(0, 1, 0, 1), new Color(0,0,1,1) };InitBlood(100000);}/// <summary> /// 传入总血量,初始化血条 /// </summary> /// <param name="number"></param> public void InitBlood(float number){count = (int)(number / oneBarBlood);//剩下的血条数nowBlood = number % oneBarBlood;//最后一条血的当前血量if (nowBlood == 0) //如果最后一条血的血量刚好充满剩余血条数减一{nowBlood = oneBarBlood;count--; }colorIndex = count % colors.Length;nowBar.color = colors[colorIndex];nowBar.fillAmount = nowBlood / oneBarBlood;if (count != 0){int nextColorIndex = (colorIndex - 1 + colors.Length) % colors.Length;nextBar.color = colors[nextColorIndex];nextBar.gameObject.SetActive(true);}else{nextBar.gameObject.SetActive(false);}middleBar.gameObject.SetActive(false);countText.text = count + "";}/// <summary> /// 血量变化,并根据伤害判断是否使用过渡血条 /// </summary> /// <param name="number"></param> public void ChangeBlood(float number){nowBlood += number;nowTargetValue = nowBlood / oneBarBlood;isBloodMove = true;if ((number < 0) && (Mathf.Abs(number) <= oneBarBlood))//处于受伤状态并且伤害量较低时 {speed = quickSpeed;middleBar.gameObject.SetActive(true);middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 1);middleBar.fillAmount = nowBar.fillAmount;middleTargetValue = nowTargetValue;}else//处于受伤状态并且伤害量较大时,或者处于加血状态 {speed = slowSpeed;middleBar.gameObject.SetActive(false);}}/// <summary>/// 普通血条的流动 /// </summary>void MoveNowBar(){if (!isBloodMove) return;nowBar.fillAmount = Mathf.Lerp(nowBar.fillAmount, nowTargetValue, speed);if (Mathf.Abs(nowBar.fillAmount - nowTargetValue) <= 0.01f)//到达目标点 isBloodMove = false;if (count == 0)nextBar.gameObject.SetActive(false);elsenextBar.gameObject.SetActive(true);if (nowBar.fillAmount >= nowTargetValue)SubBlood();elseAddBlood();}/// <summary>/// 过渡血条的流动 /// </summary>void MoveMiddleBar(){//受到轻伤时( <oneBarBlood),才会出现过渡血条if (speed == quickSpeed){middleBar.fillAmount = Mathf.Lerp(middleBar.fillAmount, middleTargetValue, middleBarSpeed);if (Mathf.Abs(middleBar.fillAmount - 0) < 0.01f){middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 1);middleBar.fillAmount = 1;middleTargetValue++;}}}void AddBlood(){float subValue = Mathf.Abs(nowBar.fillAmount - 1);if (subValue <= 0.0f)//到达1 {count++;countText.text = count.ToString();nowBar.fillAmount = 0;nowTargetValue -= 1;nowBlood -= oneBarBlood;nextBar.color = colors[colorIndex];colorIndex++;colorIndex %= colors.Length;nowBar.color = colors[colorIndex];}}void SubBlood(){float subValue = Mathf.Abs(nowBar.fillAmount - 0);if (subValue <= 0.0f)//到达0 {//当前血条已经流动完,将过渡血条放置最前middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 2);if (count <= 0){middleBar.gameObject.SetActive(false);Destroy(this);return;};count--;countText.text = count.ToString();nowBar.fillAmount = 1;nowTargetValue += 1;nowBlood += oneBarBlood;colorIndex--;colorIndex += colors.Length;colorIndex %= colors.Length;nowBar.color = colors[colorIndex];int nextColorIndex = colorIndex - 1 + colors.Length;nextColorIndex %= colors.Length;nextBar.color = colors[nextColorIndex];}}}
start中color颜色数组初始化时注意设置颜色的透明组,如果不初始化color数组,颜色值则全部为0,透明度为零。
参考链接 http://blog.csdn.net/lyh916/article/details/47038911
源码工程链接 链接点这里
这篇关于多重血条效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!