你好我使用New Unity 4.6 Ui工具来创建一个健康栏,我已经创建了健康栏纹理,但我已经有一个健康脚本,它使用一个Old OnGui Button功能,当点击时在角落显示一个黑色按钮玩,我想禁用OnGui健康sript纹理,并对我的新健康栏UI纹理产生影响。请帮忙
//This is my health script that displays a black bar at the corner
var health = 300;
function OnGUI(){
if(GUI.Button(Rect(10,10,health,10), "")){
health += 25;
}
}
答案 0 :(得分:2)
很抱歉听到您仍然遇到此问题。在这里查看此图片。它显示了可以将OnClick事件添加到新UI按钮的位置。 您看到UIManager的部分只是将具有您的运行状况的对象放在那里,然后您就可以访问所需的事件。 只需删除你的OnGUI()代码就可以添加像
这样的公共函数 public void GainHealth()
{
health +=25;
}
一旦你改变了,你将在列表中看到你的功能,就像在这里的图像。一定要设置编辑器和运行时才能在编辑器中工作。一旦你设置它,你的按钮将执行你在那里设置的任何功能
答案 1 :(得分:0)
这是我的健康栏系统
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));
}