LoginSignup
0
1

More than 5 years have passed since last update.

unity > System.DateTime > 現在のmsecを取得する実装

Last updated at Posted at 2015-09-13
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

System.DateTimeを普通に使う限りはミリ秒の値は取れないようだ。

http://stackoverflow.com/questions/4016483/get-time-in-milliseconds-using-c-sharp
のJosua Schmidさんの投稿を元に実装してみた。
もともとはJan1St1970 を使っていたが、Nowから生成するnowMsec0を使えばよさそう。

CheckMsec.cs
using UnityEngine;
using System.Collections;

using System;

public class CheckMsec : MonoBehaviour {

    long get_msec() {
        System.DateTime now = System.DateTime.Now;
        System.DateTime nowMsec0 = new DateTime (now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
        long TotalMsec = (long)((DateTime.Now - nowMsec0).TotalMilliseconds);

        return TotalMsec;
    }

    void Start () {
        StartCoroutine("WaitCoRoutine");
    }

    private IEnumerator WaitCoRoutine() {
        for (int idx=0; idx<4; idx++) {
            yield return new WaitForSeconds(0.3f); // every 300 msec

            long msec = get_msec ();
            Debug.Log ("msec=" + msec.ToString ());
        }
    }
}

コルーチンを使って300msecのwaitをつけている。

下記が実行例。
(552-236)msecで300msecプラスアルファの処理になっているのが分かる。

Untitled_-_150913-nowMsec_-_PC__Mac___Linux_Standalone__Personal_.jpg

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1