简体   繁体   English

Unity3d健康栏GUI元素

[英]Unity3d health bar GUI element

I want to create a health bar using information that I'm currently displaying in a GUItext. 我想使用当前在GUItext中显示的信息来创建健康栏。 The behaviour I want is for the health to decrease over time (like a countdown timer), but increase by a small amount whenever the player collects an object. 我想要的行为是为了使健康状况随着时间的推移而减少(例如倒数计时器),但是每当玩家收集一个对象时,其行为就会增加一点点。

Here is my current code: 这是我当前的代码:

using UnityEngine;
using System.Collections;

public class TimeText : MonoBehaviour {

    public GUIText timeText;
    //countdown
    public float timer = 99.00f;

    // Update is called once per frame
    void Update () 
    {
        timer -= Time.deltaTime*5;

        timeText.text = "Health: " + timer.ToString("0");

    }
}

Here is what I would like my health bar to look like: 这是我希望健康栏显示的样子: 在此处输入图片说明

As @JoeBlow mentioned above, avoid the old GUI mechanics. 如上文@JoeBlow所述,请避免使用旧的GUI机制。 They are super heavy, crude, and not performance-helpful. 它们非常笨重,粗糙,并且对性能没有帮助。

@Serlite broke down the question for you. @Serlite为您分解了这个问题。 Those are what you need. 这些就是您所需要的。

For the health-bar, I suggest using Unity UI's Image component. 对于健康栏,我建议使用Unity UI的图像组件。 Use the image you want (possibly the same one you posted in your question?) for the health bar and change the fillAmount based on your timer variable (though personally, I would rename that to something like "health" to represent its purpose better). 使用您想要的图像(可能与您在问题中发布的图像相同)用于健康状况栏,并根据您的timer变量更改fillAmount (尽管就我个人而言,我将其重命名为“ health”,以更好地表示其目的) 。

It looks like you've already taken care of reducing the health over time. 看来您已经采取了逐步减少健康的措施。 To add to the health, use Colliders as triggers in the scene, and OnTriggerEnter or any of the similar default monobehavior methods on the appropriate objects. 要增加健康状况,请在场景中使用“碰撞器”作为触发器,并在适当的对象上使用OnTriggerEnter或任何类似的默认单行为方法。

I suggest going through a couple tutorials online, so that you get a better understanding of programming logic in Unity. 我建议在线阅读一些教程,以便您更好地了解Unity中的编程逻辑。 I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM