[unity3d] 制作人物血条并随血量减少而变红

血条是用GUI做的矩形,给矩形添加的颜色会变成Image, 当血满时Color为(1,1,0)就是黄的,(1,0,0)是红的,当血掉时(1,剩余血/满血,0)血条就会变红。当然想牛逼一点,可以先(0,1,0)绿色,前面一半血时可以((满血-剩血)/半血,1,0),到后面一半时可以(1,剩血/半血,0)这样血就从绿色渐变成红色,半血时是黄的。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameController : MonoBehaviour {

	public GameObject LifeBar;
	private Image image;

	public GameObject Coin;
	public GameObject Heart;
	public GameObject Bomb;
	public int EnemyCount;
	public Vector3 spawnValues;
	public Vector3 spawnValues2;
	public float[] a=new float[7];
	public int totalTime;
	private float nextTime=1;

	public GUIText textScore;
	public GUIText textHP;
	public GUIText textGameOver;
	public Text textClock;

	public float HP;
	private float HP_s;
	private float Width;
	private float Height;
	private RectTransform rect_T;

	private int totalScore = 0;
	private bool GameOver = false;

	//private Image image;
	//private RectTransform rect_T;

	// Use this for initialization
	void Start () {
		HP_s = HP;
		textScore.text = "Score: 0";
		textHP.text = "HP: " + HP;
		textGameOver.gameObject.SetActive (false);

		
			/*LifeBar=GameObject.FindGameObjectWithTag("LifeBar");
			image = LifeBar.GetComponent ();
			rect_T = LifeBar.GetComponent();
			Rect rect = rect_T.rect;
			Width = rect.width;
				Height = rect.height;*/
		
		image = LifeBar.GetComponent ();
		rect_T = LifeBar.GetComponent();
		Rect rect = rect_T.rect;
		Width = rect.width;
		Height = rect.height;
		StartCoroutine ("ClockTime");
	}

    public void DecreaseHP(int damage){
		HP -= damage;
		textHP.text = "HP: " + HP;

		rect_T.sizeDelta = new Vector2 ((HP / HP_s) * Width, Height);

		image.color = new Color (1,(HP / HP_s) , 0,1);
	}
	
	

	
}

 

你可能感兴趣的:(其它)