UI 按钮 图片 血条

using UnityEngine;--------toggle-----------
using UnityEngine.UI;

public class UItoggle : MonoBehaviour
{
   private Toggle _toggle;
   private AudioSource _audioSource;

   private void Start()
   {
      _toggle = GetComponent<Toggle>();
      _toggle.onValueChanged.AddListener(ToggleChange);
      _audioSource = GetComponent<AudioSource>();
   }

   private void ToggleChange(bool isOn)
   {
      _audioSource.mute = !isOn;
      Debug.Log(isOn);
   }
   
}

using System;--------------技能倒计时low------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UITestImage : MonoBehaviour
{
    private Image _image;
    private Text _text;
    //cd时间
    private float timer=5;
    private void Start()
    {
        _image = GetComponent<Image>();
        _text = transform.GetChild(0).GetComponent<Text>();
        _image.fillAmount = 1;
    }

    private void Update()
    {
        _image.fillAmount -= Time.deltaTime / timer;
        _text.text = ((int)(_image.fillAmount*timer)).ToString();
        _image.fillAmount = Mathf.Clamp(_image.fillAmount, 0, 1);
    }
}


--------------技能倒计时high------------
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class UITestButton : MonoBehaviour
{
    private Image _image;
    private Text _text;
    private Text _text1;
    private bool _cd;
    private float cdTimer = 5;

    private Button btn;
    private void Start()
    {
        btn = GetComponent<Button>();
        _image = transform.GetChild(1).GetComponent<Image>();
        _text = transform.GetChild(2).GetComponent<Text>();
        _text1 = transform.GetChild(3).GetComponent<Text>();
        _image.fillAmount = 1;
        btn.onClick.AddListener(() =>
        {
            Debug.Log("点击了按钮");
            BtnClick();
        });
    }

    private void Update()
    {
        if (_cd == false) 
            return;
        _image.fillAmount -= Time.deltaTime / cdTimer;
        _text.text = ((int) (_image.fillAmount * cdTimer)).ToString();
        _image.fillAmount = Mathf.Clamp(_image.fillAmount, 0, 1);
        if (_image.fillAmount<=0)
        {
            _cd = false;
        }


    }
    
    public void BtnClick()
    {
        if (_cd==false)
        {
            _image.fillAmount = 1;
            _cd = true;
            _text1.text = "德玛西亚";
            _text1.color = Random.ColorHSV();
            Debug.Log("德玛西亚");
        }
        else
        {
            _text1.text = "技能正在冷却...";
            _text1.color = Random.ColorHSV();
            Debug.Log("技能正在冷却...");
        }
    }
}




using System;-------------血条加减血量-----------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UITestImageBlood : MonoBehaviour
{
    private Image _image;
    private Text _text;
    private float currentHp;
    private float hp = 100;
    //血条比例
    private float ratio;

    private void Start()
    {
        ratio = currentHp / hp;
        _image = GetComponent<Image>();
        _text = transform.GetChild(0).GetComponent<Text>();
        currentHp = hp;
        _text.text = currentHp.ToString();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            AddHp(-10);
        }

        if (Input.GetMouseButtonDown(1))
        {
            AddHp(10);
        }

        _image.fillAmount = Mathf.Lerp(_image.fillAmount, currentHp / hp, Time.deltaTime * 5);
        _text.text = Mathf.RoundToInt(_image.fillAmount*hp).ToString();
    }

    private void AddHp(float num)
    {
        currentHp += num;
        currentHp = Mathf.Clamp(currentHp, 0, hp);
        ratio = currentHp / hp;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值