Unity3D: time since game start + float time interval format

Trabla: Unity3D: time since game start.

Also display time interval with format hh:mm:ss

Unity3D: time since game start + float time interval format

Solving:

float timeNow = Time.realtimeSinceStartup;


Example:


using System; //For TimeSpan
using UnityEngine.UI;
...

private float gameStartedTime;
public Text message;



void Start(){  
       InitGame();

}


void InitGame(){
     this.gameStartedTime = Time.realtimeSinceStartup;


void GameOver(){
      
//Game interval
float  game_time = Time.realtimeSinceStartup - this.gameStartedTime;

// Time formated hh:mm:ss
 string formated_time = TimeSpan.FromSeconds( (int)game_time ).ToString();     

//Display in UI
this.message.text = string.Format( "Game Over \n Time: {0}", formated_time );

}







No comments:

Post a Comment