1 using UnityEngine; 2 using System.Collections; 3 4 public class xt : MonoBehaviour 5 { 6 7 //红色血条 8 public Texture2D blood_red; 9 //黑色血条 10 public Texture2D blood_black; 11 //当前生命值 12 private int HP = 100; 13 14 void OnGUI() 15 { 16 if (GUILayout.RepeatButton("加血")) 17 { 18 //增加生命值 19 if (HP < 100) 20 { 21 HP++; 22 } 23 } 24 if (GUILayout.RepeatButton("减血")) 25 { 26 //减少生命值 27 if (HP > 0) 28 { 29 HP--; 30 } 31 } 32 //根据当前生命值计算红色血条显示的宽度 33 int blood_width = blood_red.width * HP / 100; 34 //绘制黑色血条 35 GUI.DrawTexture(new Rect(100, 100, blood_black.width, blood_black.height), blood_black); 36 //绘制红色血条 37 GUI.DrawTexture(new Rect(100, 100, blood_width, blood_red.height), blood_red); 38 39 } 40 }
资源下载