2015-02-06 54 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)); 
    }