U3D中延时多少秒之后去 执行某个操作

使用了IEnumerator 和 WaitForSeconds

以下是示例代码

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class AnimatePanel : MonoBehaviour

{

    public AnimationCurve showCurve;

    public AnimationCurve hideCurve;

    public float animationSpeed;

    public GameObject panel;

    IEnumerator ShowPanel(GameObject gameObject)

    {

        float timer = 0;

        while (timer <= 1)

        {

            gameObject.transform.localScale = Vector3.one * showCurve.Evaluate(timer);

            timer += Time.deltaTime * animationSpeed;

            yield return null;

        }

    }

    IEnumerator HidePanel(GameObject gameObject)

    {

        float timer = 0;

        while (timer <= 1)

        {

            gameObject.transform.localScale = Vector3.one * hideCurve.Evaluate(timer);

            timer += Time.deltaTime * animationSpeed;

            yield return null;

        }

        if (gameObject.transform.localScale.x > 0)

        {

            gameObject.transform.localScale = new Vector3(0, 0, 0);

        }

    }

    IEnumerator DelayHidePanel(GameObject gameObject)

    {

        yield return new WaitForSeconds(3.0f);

        StartCoroutine(HidePanel(gameObject));

    }

    private void Update()

    {

        if (Input.GetMouseButtonDown(0))

        {

            StartCoroutine(ShowPanel(panel));

            StartCoroutine(DelayHidePanel(panel));

        }

        else if (Input.GetMouseButtonDown(1))

        {

            StartCoroutine(HidePanel(panel));

        }

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.生命周期初始化阶段Awake:仅被调用一次,不同的物体顺序随机调用OnEnableStart:仅被调用一次物理...
    APP4x阅读 554评论 0 2
  • using System.Collections; using System.Collections.Generi...
    潇千忘阅读 2,027评论 0 1
  • 1、MonoBehaviour类(UnityEngine命名空间中定义): Awake:最开始调用,做一些初始化工...
    夕望有你阅读 1,032评论 0 0
  • Unity脚本基础 1.Unity3D中的协程(coroutine)和C#线程之间的区别是什么? 多线程程序同时运...
    豆铮阅读 3,659评论 0 3
  • 这是以前刚开始学unity时遇到的一些问题的笔记,现在看来有些幼稚,但是还是发出来给刚入门的同学参考一下。 1. ...
    JervieQin阅读 1,382评论 0 1