這裏是我的血條系統
using UnityEngine;
using System.Collections;
public class HealthSystem : MonoBehaviour {
static float healthBarLenght = 20; //Fixed length
public static void Set_HealthBar(
Transform transform,
int healthBarHeight,
float CurrentHealth,
float MaxHealth,
Texture2D BackBar,
Texture2D FrontBar)
{
Vector3 screenPosition;
GUIStyle style1 = new GUIStyle();
GUIStyle style2 = new GUIStyle();
float HPDrop = (CurrentHealth/MaxHealth)* healthBarLenght;
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
screenPosition.y = Screen.height - screenPosition.y;
style1.normal.background = BackBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, healthBarLenght,healthBarHeight),"",style1);
style2.normal.background = FrontBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, HPDrop,healthBarHeight),"",style2);
}
//Colors for Health system
public static Texture2D Colors(int r,int g, int b)
{
Texture2D texture = new Texture2D(2, 2);
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
Color color = new Color(r, g, b);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
}
這可以從一層腳本中調用這樣
void OnGUI(){
HealthSystem.Set_HealthBar(
transform,
2,
70,
100,
HealthSystem.Colors(0,0,0),
HealthSystem.Colors(0,255,0));
}
再次感謝您的回覆,我確實有一個滾動值設置爲我的Canvas> img_header中的Component,Image類型設置爲Filled,我想訪問這個image.fillAmount = .2f ;與我的球員的健康腳本,如何?我已經添加了自己的函數Fill(),但它沒有在OnClickEvent中顯示:( – isaacj11 2015-02-07 14:51:43
哦,我使用滑塊實現了類似的效果)public Slider healthSlider;然後將其值設置爲健康狀態,如healthSlider.value = currentHealth; – 2015-02-07 16:57:09
我剛剛實現了它與正常的OnGui功能!與Gui皮膚我能夠把我自己的定製紋理!抱歉,如果我打擾你的隊友,謝謝堆! – isaacj11 2015-02-08 12:25:09