在协程中获取时间
Get time passed in Coroutine
我正在使用 Unity3d 开发一个简单的游戏。
我创建了一个 Coroutine
,它在特定时间后使用 WaitForSecond
执行某些操作。
但在某些州它以 StopCoroutine()
结束。
我想知道是否有可能在Coroutine
停止时获取它的时间。
我的意思是当 Coroutine
停止给我一个数字,比如 23 秒或 54 秒或类似的数字。
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
yield return new WaitForSeconds(10);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Debug.Log("RunTime " + elapsedTime);
如果您只想在停止时获取时间,请尝试这样的操作:
Coroutine testCoroutine = StartCoroutine(yourCoroutine());
然后,当您使用 StopCoroutine(testCoroutine);
停止协程时,您可以 return 从上面记录的 Stopwatch 函数。或者,您始终可以将 Time.deltaTime
添加到变量,并将 return 添加为浮点数。那会给你秒和毫秒。
编辑 在作者发布他的代码之后。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class sdfsd : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(WaitUntilAPressed());
}
IEnumerator WaitUntilAPressed()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
yield return new WaitUntil(()=> Input.GetKeyDown(KeyCode.A));
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Debug.Log(elapsedTime);
}
}
GlobalScope.launch(Dispatchers.IO) {
val millis = measureTimeMillis {
// Do Stuff
}
val seconds = TimeUnit.MILLISECONDS.toSeconds(millis)
Log.d(TAG, "Time: $seconds seconds")
}
我正在使用 Unity3d 开发一个简单的游戏。
我创建了一个 Coroutine
,它在特定时间后使用 WaitForSecond
执行某些操作。
但在某些州它以 StopCoroutine()
结束。
我想知道是否有可能在Coroutine
停止时获取它的时间。
我的意思是当 Coroutine
停止给我一个数字,比如 23 秒或 54 秒或类似的数字。
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
yield return new WaitForSeconds(10);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Debug.Log("RunTime " + elapsedTime);
如果您只想在停止时获取时间,请尝试这样的操作:
Coroutine testCoroutine = StartCoroutine(yourCoroutine());
然后,当您使用 StopCoroutine(testCoroutine);
停止协程时,您可以 return 从上面记录的 Stopwatch 函数。或者,您始终可以将 Time.deltaTime
添加到变量,并将 return 添加为浮点数。那会给你秒和毫秒。
编辑 在作者发布他的代码之后。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class sdfsd : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(WaitUntilAPressed());
}
IEnumerator WaitUntilAPressed()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
yield return new WaitUntil(()=> Input.GetKeyDown(KeyCode.A));
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Debug.Log(elapsedTime);
}
}
GlobalScope.launch(Dispatchers.IO) {
val millis = measureTimeMillis {
// Do Stuff
}
val seconds = TimeUnit.MILLISECONDS.toSeconds(millis)
Log.d(TAG, "Time: $seconds seconds")
}