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;
}
}
UI 按钮 图片 血条
最新推荐文章于 2021-12-19 20:40:05 发布