c#uwp timer替代方案

时间:2018-04-10 12:37:57

标签: c# unity3d timer uwp

我使用Timers在Unity(C#)中编写了一些代码。之后我意识到UWP,我正在编写的平台,不能使用它们。我尝试了一些替代品,比如Threadpooltimers,但我无法让它们起作用。使Threadpooltimers工作的任何其他选择或方法?

using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;

public class Spin : MonoBehaviour {

    // Use this for initialization
    void Start() {}

    public bool spin = false;
    public float speed = 36f;
    private static Timer aTimer;
    private static Timer bTimer;
    void StartSpin()
    {
        spin = true;

        // Create a timer and set two full rotation intervals for demo and 
        //another one to adjust the movement of the object (screen).
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2*360/speed * 1000 - 1;

        bTimer = new System.Timers.Timer();
        bTimer.Interval = 360 / speed * 1000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEventa;
        bTimer.Elapsed += OnTimedEventb;

        // Disable autoreset
        aTimer.AutoReset = false;
        bTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;
        bTimer.Enabled = true;
    }

    // At the end of Timer b, change the angle of the object (screen).
    void OnTimedEventb(object bTimer, System.EventArgs e)
    {
        transform.Rotate(Vector3.right, 45);
    }

    // At the end of Timer a, stop the spin movement and stop Timer b from resetting.
    void OnTimedEventa(object aTimer, System.EventArgs e)
    {
        spin = false;
        bTimer.AutoReset = false;
    }

    // Update is called once per frame
    void Update () 
    {
        transform.Rotate(Vector3.back, System.Convert.ToSingle(spin) * speed * Time.deltaTime);
    }
}

2 个答案:

答案 0 :(得分:0)

使用协程或更新方法编写计时器。

IEnumerator Start() {
     while (true) {
          yield return new WaitForSeconds(1.0f);
          Debug.Log("Elapsed.");
     }
}

答案 1 :(得分:0)

如@MD Muzibur Ra​​hman所述,您可以使用DispatcherTimer。

以下是从MSDN获取的有关如何使用DispatcherTimer的代码:

DispatcherTimer dispatcherTimer;
DateTimeOffset startTime;
DateTimeOffset lastTime;
DateTimeOffset stopTime;
int timesTicked = 1;
int timesToTick = 10;

public void DispatcherTimerSetup()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    //IsEnabled defaults to false
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
    startTime = DateTimeOffset.Now;
    lastTime = startTime;
    TimerLog.Text += "Calling dispatcherTimer.Start()\n";
    dispatcherTimer.Start();
    //IsEnabled should now be true after calling start
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
}

void dispatcherTimer_Tick(object sender, object e)
{
    DateTimeOffset time = DateTimeOffset.Now;
    TimeSpan span = time - lastTime;
    lastTime = time;
    //Time since last tick should be very very close to Interval
    TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
    timesTicked++;
    if (timesTicked > timesToTick)
    {
        stopTime = time;
        TimerLog.Text += "Calling dispatcherTimer.Stop()\n";
        dispatcherTimer.Stop();
        //IsEnabled should now be false after calling stop
        TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
        span = stopTime - startTime;
        TimerLog.Text += "Total Time Start-Stop: " + span.ToString() + "\n";
    }
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    DispatcherTimerSetup();
}