unity协程计时器循环调用

用协程 循坏刷新扫光特效

using DG.Tweening;
using Coffee.UIEffects;
private UIShiny[] effList => transform.GetComponentsInChildren<UIShiny>();//获取数组
    IEnumerator RefreshEff()
    {
        foreach (var item in effList)
        {
            item.effectFactor = 0;
        }

        yield return new WaitForSeconds(5);

        foreach (UIShiny item in effList)
        {
            UIShiny temp = item;
            DOTween.To(() => temp.effectFactor,
          x => temp.effectFactor = x,
          (1f), 2);
        }
        StartCoroutine( RefreshEff());
    }

    private void OnEnable()
    {
     
        StartCoroutine(RefreshEff());
     }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity中,程是一种可以轻松实现计时操作的方法。你可以使用程来创建一个定时器。以下是几种常见的Unity程计时方法: 1. 使用StartCoroutine()和WaitForSeconds()函数: ```c# using UnityEngine; using System.Collections; public class TimerExample : MonoBehaviour { public float delayTime = 3f; private void Start() { StartCoroutine(StartTimer()); } private IEnumerator StartTimer() { yield return new WaitForSeconds(delayTime); // 在此处添加计时器结束后要执行的代码 Debug.Log("Timer finished!"); } } ``` 2. 使用Time.deltaTime累加方式: ```c# using UnityEngine; public class Test : MonoBehaviour { public float timer = 2f; void Update() { timer -= Time.deltaTime; if (timer <= 0) { Do(); timer = 2f; // 定时2秒 } } void Do() { Debug.Log("每2秒执行一次"); } } ``` 3. 使用InvokeRepeating函数: ```c# using UnityEngine; public class Test : MonoBehaviour { void Start() { // 0秒后,每1秒执行一次Do InvokeRepeating("Do", 0, 1); } void Do() { Debug.Log("每1秒执行一次"); } } ``` 4. 使用程: ```c# using System.Collections; using UnityEngine; public class Test : MonoBehaviour { void Start() { // 每1秒执行一次Do StartCoroutine(Do()); // 如果写入参数Do()报红,可以写成以下调用形式 // StartCoroutine("Do"); } IEnumerator Do() { while (true) { yield return new WaitForSeconds(1f); Debug.Log("每1秒执行一次"); } } } ``` 以上是Unity中实现程计时的几种常见方法。你可以根据实际需要选择其中一种来实现你的计时功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity_计时器实现的四种方式](https://blog.csdn.net/m0_69778537/article/details/130299794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [unity学习----程与计时器](https://blog.csdn.net/m0_48926221/article/details/126062351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值