2015-02-06 38 views
0

你好我使用新的Unity 4.6 UI工具来创建一个健康栏,我已经创建了健康栏纹理,但我已经有一个健康脚本使用旧的OnGui按钮功能,在角落时在角落显示一个黑色按钮,我想禁用OnGui健康条纹纹理,并对我的New healthbar 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; 
} 
} 

回答

2

对不起,听到你仍然有这个问题。看看这个图像在这里。它显示了您可以将OnClick事件添加到新的用户界面按钮的位置。 您看到UIManager的部分只是将包含您的健康记录的对象放在那里,然后就可以访问所需的事件。 只是删除您OnGUI()的代码添加像

public void GainHealth() { health +=25; } 公共职能一旦你的改变,你会看到你的名单上的函数喜欢这里的形象。一定要设置编辑器和运行时才能在编辑器中工作。一旦你在此设置你的按钮将你设置任何功能那里

enter image description here

+0

再次感谢您的回复,我确实有一个滚动值设置为我的Canvas> img_header中的Component,Image类型设置为Filled,我想访问这个image.fillAmount = .2f ;与我的球员的健康脚本,如何?我已经添加了自己的函数Fill(),但它没有在OnClickEvent中显示:( – isaacj11 2015-02-07 14:51:43

+0

哦,我使用滑块实现了类似的效果)public Slider healthSlider;然后将其值设置为健康状态,如healthSlider.value = currentHealth; – 2015-02-07 16:57:09

+0

我刚刚实现了它与正常的OnGui功能!与Gui皮肤我能够把我自己的定制纹理!抱歉,如果我打扰你的队友,谢谢堆! – isaacj11 2015-02-08 12:25:09

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)); 
    }