简体   繁体   English

统一的双健康栏 c#

[英]Dual Health-bar in unity c#

I have been looking around and there isn't much on this topic.我一直在环顾四周,关于这个话题的内容并不多。 I'm currently trying to make a dual health bar where if one is low the other rises, similar to a struggle system.我目前正在尝试制作一个双重健康栏,如果一个低另一个上升,类似于斗争系统。

Like this:像这样:

在此处输入图像描述

So far I have created two 2d sliders in unity one with left to right anchor (the red one) and the other with a right to left anchor, however one is overlapped and and doesn't go below its original length but can go higher, I have tried changing the min and max values but this doesn't help.到目前为止,我已经创建了两个统一的 2d 滑块,一个具有从左到右的锚(红色),另一个具有从右到左的锚,但是一个是重叠的,并且 go 不会低于其原始长度,但 go 可以更高,我试过改变最小值和最大值,但这没有帮助。

在此处输入图像描述

How can I get it to become lower but at the same time make the other slider rise?我怎样才能让它变得更低,但同时让另一个 slider 上升?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class healthbar : MonoBehaviour
{
    public Slider slider;

    public void SetHealth(int health)
    {
        slider.value = health;
    }
}

This is my script for the red bar so far.到目前为止,这是我的红条脚本。

You don't need 2 health bars, you can cheat instead.您不需要 2 个健康条,您可以作弊。

You can use just one red health bar with a green background.您只能使用一个带有绿色背景的红色健康条。

the slider value being the ratio red/green slider 值是红/绿比

// note that both RedHealth and GreenHealth should be float
slider.value = RedHealth / (RedHealth + GreenHealth);

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

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