我使用計(jì)時(shí)器在Unity(C#)中編寫了一些代碼。之后,我意識(shí)到我正在編寫的平臺(tái)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貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用協(xié)程或更新方法編寫一個(gè)計(jì)時(shí)器。
IEnumerator Start() {
while (true) {
yield return new WaitForSeconds(1.0f);
Debug.Log("Elapsed.");
}
}
- 2 回答
- 0 關(guān)注
- 395 瀏覽
添加回答
舉報(bào)
0/150
提交
取消