Unity - the production of two progress bars

1. Progress bar - linear

Step1: Create Slider and Text, put them casually

 Step2: Write the script

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LineLoad : MonoBehaviour
{
    //进度显示
    public Text text;
    //进度条
    public Slider progressSlider;

    private void Start()
    {
        StartCoroutine(LoadScene());
    }
    IEnumerator LoadScene()
    {
        AsyncOperation operation = SceneManager.LoadSceneAsync("ExcelScene");

        while (!operation.isDone)
        {
            progressSlider.value = operation.progress;

            text.text = operation.progress * 100 + "%";

            if (operation.progress >= 0.9f)//如果进度条已经到达90%
            {
                progressSlider.value = 1; //那就让进度条的值编变成1
                text.text = "加载完成!";
            }
            yield return null;
        }
    }
}

Step3: Load the scene into BuildSetting

Done!

2. Progress bar - pie shape

Step1: Find a picture similar to a ring, make it an Image, and add a Text

 Step2: Change the type of Image to Filled type, change the starting point Origin to Top, and change Clockwise to false

              fillAmount changed to 0

 Step3: Write the script

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class CircleLoad : MonoBehaviour
{
    public Image m_Image;
    public Text m_Text;

    private void Start()
    {
        m_Image.fillAmount = 0;

        StartCoroutine(LoadSceneCircle());
    }

    IEnumerator LoadSceneCircle()
    {
        AsyncOperation operation = SceneManager.LoadSceneAsync("ExcelScene");

        while (!operation.isDone)
        {
            m_Image.fillAmount = operation.progress / 100f ;

            m_Text.text = operation.progress * 100 + "%";

            if (operation.progress >= 0.9f)//如果进度条已经到达90%
            {
                m_Image.fillAmount = 1; //那就让进度条的值编变成1
                m_Text.text = "加载完成!";
            }
            yield return null;
        }
    }
}

The difference between this and the above is that the fillAmout attribute is mainly used here, while the progress bar only needs the Process attribute of Slider

How, is not very simple?

Guess you like

Origin blog.csdn.net/leikang111/article/details/130423241