类似英雄联盟里面血条的显示效果

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HeroBlood : MonoBehaviour {
<span style="white-space:pre">	</span>//基于NGUI的设计效果
    //单个血条的预制体
    public GameObject bloodClip;
    //整体血条的父类
    public GameObject bloodParterBG;
    //血条的值
    private int BloodValue;
    //小块血条的数量
    private int clipCount;
    //小块血条的长度
    private const int ClipWidth = 50;
    //输入框
    public UIInput text;
    //
    public List<GameObject> uis=new List<GameObject>();
    //整体血条的宽度
    private float ParentWidth;
    //是否销毁小血条
    private bool isDestroy;
    //
    private float ChildValue=1;
    void Start()
    {
        //赋值整体血条的宽度
        ParentWidth = bloodParterBG.GetComponent<UISprite>().width;
    }
    /// <summary>
    /// 创建小血条
    /// </summary>
    /// <param name="Index"></param>
    void CreateClip(int Index)
    {
        //实例化小血条
        GameObject go = NGUITools.AddChild(bloodParterBG, bloodClip) as GameObject;
        //计算需要多少小血条
        go.transform.localScale = new Vector3((ParentWidth / clipCount) / ClipWidth, 1, 1);
        //计算小血条在整体血条的长度
        float ClipLength = go.transform.localScale.x * ClipWidth;
        float x = Index * ClipLength - ParentWidth / 2 + ClipLength / 2;
        go.transform.localPosition = new Vector3(x, 0, 0);
        //加入数列
        uis.Add(go);
    }

    void Init()
    {
        //初始化血条的数量
        clipCount = BloodValue / 100;
        for (int i = 0; i < clipCount; i++)
        {
            CreateClip(i);
        }
    }
    /// <summary>
    /// 增加血条长度,即是加血
    /// </summary>
    void AddBlood()
    {
        if (uis.Count < clipCount)
        {
            CreateClip(uis.Count);
        }
    }
    /// <summary>
    /// 血条清零
    /// </summary>
    void Clear()
    {
        //销毁血条
        foreach (var obj in uis)
        {
            Destroy(obj);
        }
        //清空数列,小血条数量,输入框
        uis.Clear();
        clipCount = 0;
        text.text = "";
    }
    /// <summary>
    /// 减少血条
    /// </summary>
    void ReduceBlood()
    {
        if (uis.Count != 0) {
            //减少数列中血条
            GameObject go = uis[uis.Count - 1];
            //销毁血条
            go.GetComponent<UISprite>().enabled = false;
            isDestroy = true;
        }
    }
    /// <summary>
    /// 创建总的血条的数值
    /// </summary>
    void Create()
    {
        BloodValue = int.Parse(text.text);
        if (uis.Count==0&&BloodValue <= 6000&&BloodValue>=600) 
        {
            Init();
        }
    }
   
    void Update()
    {
        if (isDestroy)
        {
            Transform child = uis[uis.Count - 1].transform.FindChild("late");
            //衰减显示
            ChildValue -= Time.deltaTime*5;
            child.localScale = new Vector3(ChildValue, 1, 1);
            //销毁血条
            if (ChildValue < 0)
            {
                isDestroy = false;
                uis.RemoveAt(uis.Count - 1);
                Destroy(child.transform.parent.gameObject);
                ChildValue = 1;
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值