Graus Asked:2022-06-16 03:20:32 +0000 UTC2022-06-16 03:20:32 +0000 UTC 2022-06-16 03:20:32 +0000 UTC 如何设置函数运行一定时间? 772 有一个按钮,当你点击它时,函数开始执行,如何让它在这段时间内执行函数? c# 1 个回答 Voted Best Answer Yaroslav 2022-06-16T04:07:45Z2022-06-16T04:07:45Z 手动/协程 private IEnumerator _sometnig; public void DoSomething (float time) { _sometnig = SometnigCoroutine(time); StartCoroutine(_sometnig); } public void StopSomething () { StopCoroutine(_sometnig); } private IEnumerator SometnigCoroutine (float time) { float Timer = 0; while (Timer < time) { yield return null; // wait new frame Timer += Time.deltaTime; Debug.Log("I am fine "+Mathf.FloorToInt(time/Timer*100f)+"%"); } Debug.Log("Iol, i finished =("); }
手动/协程