为了账号安全,请及时绑定邮箱和手机立即绑定

C#UWP计时器替代

C#UWP计时器替代

C#
缥缈止盈 2021-03-30 17:09:27
我使用计时器在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 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

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


IEnumerator Start() {

     while (true) {

          yield return new WaitForSeconds(1.0f);

          Debug.Log("Elapsed.");

     }

}


查看完整回答
反对 回复 2021-04-10
  • 2 回答
  • 0 关注
  • 299 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信