这里是我的血条系统
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