Monday, 14 January 2019

Unity C# 3D Progress bar - Creating a Progress bar which scales over time

1. Create a progress bar with outer frame(fixed) and a inner slider (scales over time) something as shown in the below pic.
2. Attach the below script to an empty game object and drag the slider game object on to the progress slider



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

public class ProgressBar : MonoBehaviour
{
    public GameObject progressSlider;
    float totalWidth;
    float totalDuration = 100.0f;
    float currentTime;
    float currentWidth;


// Use this for initialization
void Start ()
    {
        totalWidth = progressSlider.transform.localScale.x;
        currentWidth = totalWidth / 100;
        progressSlider.transform.localScale = new Vector3(currentWidth, progressSlider.transform.localScale.y, progressSlider.transform.localScale.z);


    }

// Update is called once per frame
void Update ()
    {
        currentTime += Time.deltaTime;
        if(currentTime<totalDuration )
        {
            if(currentWidth <= totalWidth)
            {
                currentWidth += 0.01f;
                progressSlider.transform.localScale = new Vector3(currentWidth, progressSlider.transform.localScale.y, progressSlider.transform.localScale.z);
            }
           
        }


    }
}


That's it run the scene.

No comments:

Post a Comment