这是我的代码
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
public class NewMonoBehaviour : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]
GameObject player;
void Start()
{
}
// Update is called once per frame
async void Update()
{
if (Input.GetKeyDown("space"))
{
Debug.Log("Start");
await Task.Delay(3000);
Debug.Log("End");
}
if (Input.GetKey(KeyCode.A))
{
player.transform.Translate(new Vector3(-1,0,0) * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
player.transform.Translate(new Vector3(1,0,0) * Time.deltaTime);
}
}
}
当我按下空格键时,即使延迟尚未结束,我也可以左右移动。此外,我可以反复按空格键以获得
Start
Start
Start
End
End
End
这就像我想要的那样,但我想了解为什么。我想这个方法中的所有内容都会被阻止,直到 Task.Delay 完成。
德玛西亚99
精慕HU
相关分类