Digital count down in unity

Advertisemen

Let's take a look, how to add images to show count down. First, create some images like 0, 1, 2, 3 in photoshop or others and drag them to unity editor.




Now, open up unity and create one C# Script and name it whatever you want and then attached it to camera.
Let's edit script, type the followings -

using UnityEngine;
using System.Collections;

public class countDown : MonoBehaviour {
        public Texture2D img0;
        public Texture2D img1;
        public Texture2D img2;
        public Texture2D img3;

        private int totalTime;
        private float initialTime;

        void Awake(){
                totalTime 3;
                initialTime Time.time;
        }

        void OnGUI(){
                GUILayout.Labelimages() );
        }
     

        int TimeRemaining(){
                int elapsedTime = (int)(Time.time - initialTime);
"color: #f7f7ec;">                int timeLeft = (totalTime - elapsedTime);
                return timeLeft;
        }

        Texture2D images(){
                switch(TimeRemaining() ){
                case 3:
                        return img3;
                case 2:
                        return img2;
                case 1:
                        return img1;
                default:
                        return img0;
                }
        }
}

As you can see, we have created Texture2D variables to hold the images we created earlier. Inside, Awake function, we have totalTime = 3; which means we are counting down from 3, if you want to start count down from 5, go ahead and type 5 but remember you have to have 5 images.And initialTime = Time.time; what this one does is holds the time value on Awake of the scene.Function TimeRemaining() returns the time left after deducting difference of Time.time on Awake and Time.time started later.

Drag the images to image place holder 1, 2, 3 respectively.
Now, go ahead and run, you will see counting down...



Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D