C#UWP计时器替代

我使用计时器在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);

    }

}


缥缈止盈
浏览 345回答 2
2回答

慕的地6264312

使用协程或更新方法编写一个计时器。IEnumerator Start() {     while (true) {          yield return new WaitForSeconds(1.0f);          Debug.Log("Elapsed.");     }}
打开App,查看更多内容
随时随地看视频慕课网APP