美文网首页
U3D中延时多少秒之后去 执行某个操作

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

作者: Yixian_Huang | 来源:发表于2022-05-01 12:22 被阅读0次

使用了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));

        }

    }

}

相关文章

网友评论

      本文标题:U3D中延时多少秒之后去 执行某个操作

      本文链接:https://www.haomeiwen.com/subject/ahfpyrtx.html