Progress bar and percentage display in Slider in Unity

Display of progress bar and loading number in Slider in Unity

1. Set the basic components Slider and Text
in the unity panel 2. Set an empty object
3. Hang the script
4 Drag the set components Slider and Text
(in the Slider component, the maximum value I set is 100, you can adjust it according to your own Requirements setting and code modification)
The script is as follows:

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

public class LoadingManager : MonoBehaviour {
    
    
   
    public Slider slider;  //进度条

    public Text Load_Data;  //加载数


    // Use this for initialization
    IEnumerator Start () {
    
    
    for (float i = 0;i<100;) //从零开始,到100结束
        {
    
    
            i += Random.Range(0.1f, 1.5f); //每次循环累加数浮点数(从0.1到1.5随机)
            slider.value = i; //将累加的数赋予进度条的值
            Load_Data.text = (int)(slider.value) + "%"; //将进度条值转化成int类型加上百分比赋予加载数。
            yield return new WaitForEndOfFrame();

        }
  
        yield return null;
    }
	
	// Update is called once per frame
	void Update () {
    
    
		
	}
}

If it was helpful, please like and save it!

Guess you like

Origin blog.csdn.net/Sea3752/article/details/120925099