User interface 如何创建响应我的健康脚本的健康栏?
您好,我正在使用新的Unity 4.6 Ui工具创建健康栏,我已经创建了健康栏纹理,但是我已经有了一个健康脚本,它使用了一个旧的OnGui按钮功能,在播放时在角落显示一个黑色按钮,我想禁用OnGui健康sript纹理,并对我的新健康栏Ui纹理产生影响。请帮忙User interface 如何创建响应我的健康脚本的健康栏?,user-interface,reference,unity3d,unityscript,User Interface,Reference,Unity3d,Unityscript,您好,我正在使用新的Unity 4.6 Ui工具创建健康栏,我已经创建了健康栏纹理,但是我已经有了一个健康脚本,它使用了一个旧的OnGui按钮功能,在播放时在角落显示一个黑色按钮,我想禁用OnGui健康sript纹理,并对我的新健康栏Ui纹理产生影响。请帮忙 //This is my health script that displays a black bar at the corner var health = 300; function OnGUI(){ if(GUI.Button(Re
//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;
}
}
很抱歉听到你在这方面仍然有问题。看看这张图片。它显示了可以将OnClick事件添加到新UI按钮的位置。 您看到UIManager的部分只是将包含healthscript的对象放在那里,然后您可以访问所需的事件。 只需删除OnGUI()代码,然后添加一个公共函数,如
public void GainHealth()
{
健康+=25;
}
一旦你改变了,你会在列表上看到你的函数,如图所示。确保设置编辑器和运行时,使其在编辑器中工作。一旦你在那里设置了它,你的按钮就会执行你在那里设置的任何功能
这是我的健康酒吧系统
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;
}
}
再次感谢您的回复,我实际上在组件中的my Canvas>img_标题中设置了一个滚动值,图像类型设置为Filled,我想访问此图像。fillAmount=.2f;使用我的玩家的健康脚本,如何?我添加了自己的函数Fill(),但它没有显示在onclick事件中:(哦,我使用一个滑块实现了类似的功能。公共滑块healthSlider;然后将其值设置为health,就像so healthSlider一样。value=currentHealth;我刚刚通过正常的OnGui功能实现了它!通过Gui皮肤,我可以设置我自己的自定义纹理!抱歉,如果我打扰了你,伙计,谢谢!
void OnGUI(){
HealthSystem.Set_HealthBar(
transform,
2,
70,
100,
HealthSystem.Colors(0,0,0),
HealthSystem.Colors(0,255,0));
}