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

2022-05-01  本文已影响0人  Yixian_Huang

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

        }

    }

}

上一篇 下一篇

猜你喜欢

热点阅读