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;
}
}
}
}