Unity 协程 等待

该博客介绍了如何在Unity中使用协程实现每隔0.5秒实例化一个预设物体,并在所有预设物体实例化完成后,等待0.5秒,然后按照创建顺序逐一销毁这些预设物体。
摘要由CSDN通过智能技术生成

每隔0.5s实例化一个预设,结束后等待0.5s,之后按顺序销毁预设

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

public class Wait : MonoBehaviour {
    public GameObject preNum;
    public Transform preParent;
    private GameObject[] orderObj = new GameObject[10];

	void Start () {
        float textX = preNum.transform.GetComponent<RectTransform>().localPosition.x;

        StartCoroutine(waitCreate(0.5f, (int num) =>
        {
            GameObject obj = Instantiate(preNum) as GameObject;
            obj.transform.parent = preParent.transform;
            obj.transform.GetComponent<RectTransform>().localPosition = new Vector3(textX, 0, 0);
            textX = obj.transform.GetComponent<RectTransform>().localPosition.x + 100;
            obj.GetComponent<Text>().text = num.ToString();
            orderObj[num] = obj
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值