Unity 利用UGUI之Slider制作进度条

Unity中使用Slider和Text组件可以制作简单的进度条。

首先在场景中右键->UI->Slider,新建一个Slider组件:

同样方法新建一个Text组件,最终如图:

创建一个进度模拟脚本,Slider_Progressbar.cs

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
​
public class Slider_Progressbar : MonoBehaviour
{
    public Slider slider;
    public TextMeshProUGUI text;
​
    private float curCount = 0;  //当前加载量,从0开始加载
    private float allCount = 100f;   //总加载量,这里设置为100
​
    private float smoothSpeed = 0.1f;  //加载的速度
​
    // Start is called before the first frame update
    void Start()
    {
​
    }
​
    // Update is called once per frame
    void Update()
    {
        if (curCount < allCount)
        {
            curCount += smoothSpeed;
            if (curCount > allCount)
            {
                curCount = 100f;
            }
            slider.value = curCount / 100f;
            text.text = (int)curCount / allCount * 100 + "%";
        }
    }
}
​

把该脚本拉到场景中,并把Slider和Text组件拉到脚本中的slider和text变量中:

运行场景,大功告成!

Unity 利用UGUI之Slider制作进度条

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 06:34:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 06:34:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 06:34:01       87 阅读
  4. Python语言-面向对象

    2024-01-09 06:34:01       96 阅读

热门阅读

  1. 【HBase】——整合Phoenix

    2024-01-09 06:34:01       54 阅读
  2. 3.3 设计模式基础

    2024-01-09 06:34:01       53 阅读
  3. 146. LRU 缓存

    2024-01-09 06:34:01       62 阅读