unity3D使用协程控制怪物的生命周期03

分析

九个坑位要随机生成怪物,随机时间生成
类似打地鼠

协程(Coroutines)

协程:协程是一个分部执行,遇到条件(yield return语句)时会挂起,直到条件满足时才会被唤醒继续执行后面的代码。

IEnumerator AliveTimer()
	{
	yield return new WaitForSeconds(Random.Range(1,5));
	ActivateMonster();
	}
StartCoroutine("AliveTimer");

类似于js setTimeOut吧。延时执行操作。

  • IEnumerator(迭代器),当迭代器方法运行到yield return语句时,会返会一个expression表达式并保留当前在代码中的位置。当下次调用迭代器函数时执行从该位置重新启动。
  • yield return new WaitForSeconds(1);等待一秒后执行后面的代码。
  • startCoroutine() 调用协程函数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetManager : MonoBehaviour
{
    //1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
    //2.建立数组
    public GameObject[] monsters;
    //6.获得激活状态的怪物
    public GameObject activeMonster = null;

    //9.调用测试
    private void Start()
    {
        //10.遍历初始化目标怪的状态以及boxcollider状态
        foreach (GameObject monster in monsters)
        {
            monster.GetComponent<BoxCollider>().enabled = false;
            monster.SetActive(false);
        }
        //ActiveMonster();
        //12.调用迭代器方法:先注释上一句的直接调用
        StartCoroutine("AliveTimer");
    }

    //3.是否激活各种状态函数
    private void ActiveMonster()
    {
        //4.随机激活:得到index
        int index = Random.Range(0, monsters.Length);

        //5.激活怪物:需要先获得激活状态的怪物
        //赋值
        activeMonster = monsters[index];
        //7.激活
        activeMonster.SetActive(true);
        //8.激活box collider
        activeMonster.GetComponent<BoxCollider>().enabled = true;
    }
    //11.协程控制迭代器 设置怪物生成的等待时间
    IEnumerator AliveTimer()
    {
        yield return new WaitForSeconds(Random.Range(1, 5));
        ActiveMonster();
	}
}

可测试查看延时效果

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

public class TargetManager : MonoBehaviour
{
    //1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
    //2.建立数组
    public GameObject[] monsters;
    //6.获得激活状态的怪物
    public GameObject activeMonster = null;
    //14.定义timer
    private float timer = 0;

    //9.调用测试
    private void Start()
    {
        //10.遍历初始化目标怪的状态以及boxcollider状态
        foreach (GameObject monster in monsters)
        {
            monster.GetComponent<BoxCollider>().enabled = false;
            monster.SetActive(false);
        }
        //ActiveMonster();
        //12.调用迭代器方法:先注释上一句调用
        StartCoroutine("AliveTimer");
    }

    //14.测试
    private void Update()
    {
        timer += Time.deltaTime;
        Debug.Log(timer);
    }
    //3.是否激活各种状态函数
    private void ActiveMonster()
    {
        //4.随机激活:得到index
        int index = Random.Range(0, monsters.Length);

        //5.激活怪物:需要先获得激活状态的怪物
        //赋值
        activeMonster = monsters[index];
        //7.激活
        activeMonster.SetActive(true);
        //8.激活box collider
        activeMonster.GetComponent<BoxCollider>().enabled = true;
    }
    //11.协程控制
    IEnumerator AliveTimer()
    {
        yield return new WaitForSeconds(Random.Range(1, 5));
        ActiveMonster();
	}
}
posted @ 2023-03-03 22:59  flyall  阅读(95)  评论(0)    收藏  举报