首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建一个健康条来响应我的健康脚本?

如何创建一个健康条来响应我的健康脚本?
EN

Stack Overflow用户
提问于 2015-02-06 20:03:12
回答 2查看 1.1K关注 0票数 0

你好,我正在使用新的Unity 4.6 Ui工具来创建一个健康条,我已经创建了一个健康条纹理,但我已经有了一个健康脚本,它使用一个旧的OnGui按钮功能,当点击播放时在角落显示一个黑色按钮,我想禁用OnGui健康斑点纹理并使其影响我的新健康条UI纹理。请帮帮忙

代码语言:javascript
复制
//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;
}
}
EN

回答 2

Stack Overflow用户

发布于 2015-02-06 22:38:02

很遗憾听到你在这方面仍然有问题。在这里看看这张图片。它显示了可以将OnClick事件添加到新UI按钮的位置。您看到UIManager的部分只需将包含您的健康脚本的对象放在那里,然后您就可以访问您想要的事件。只需删除OnGUI()代码,添加一个公共函数,如下所示

public void GainHealth() { health +=25; }更改后,您将在列表中看到您的函数,如下图所示。确保设置编辑器和运行时,使其在编辑器中工作。一旦你设置了它,你的按钮就可以完成你在那里设置的任何功能

票数 2
EN

Stack Overflow用户

发布于 2015-10-16 16:09:35

这是我的健康栏系统

代码语言:javascript
复制
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;

    }

}

这可以从图层脚本中调用,如下所示

代码语言:javascript
复制
void OnGUI(){
        HealthSystem.Set_HealthBar(
            transform,
            2,
            70,
            100,
            HealthSystem.Colors(0,0,0),
            HealthSystem.Colors(0,255,0));
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28365402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档