Simple time counter written in C#.

 

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

// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net

public class TimeCounter : MonoBehaviour {
    [SerializeField]
    private bool _countDown;

    [SerializeField]
    private int _seconds;

    [SerializeField]
    private Text _timeText;

    private float _timeCounted = 0.0f;

    private string _timeTextText;

    public int seconds
    {
        get
        {
            return _seconds;
        }
        set
        {
            _seconds = value;
        }
    }


  // Use this for initialization
  void Start () {
        if (!_countDown)
        {
            _timeCounted += _seconds;
        }

        _timeTextText = _timeText.text;
    }

    // Update is called once per frame
    void Update() {
        if (_countDown)
        {
            _timeCounted -= Time.deltaTime;

            if(_timeCounted <= _seconds)
            {
                _timeText.text = _timeTextText + _seconds;

                _seconds--;
            }
        }
        else
        {
            _timeCounted += Time.deltaTime;

            if (_timeCounted >= _seconds)
            {
                _timeText.text = _timeTextText + _seconds;

                _seconds++;
            }
        }
  }
}

 

Unity C# UI Simple TimeCounter
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.