Per aspera ad astra.循此苦旅,以达天际。|

flyall

园龄:4年2个月粉丝:10关注:8

2023-03-03 22:59阅读: 67评论: 0推荐: 0

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();
	}
}

本文作者:flyall

本文链接:https://www.cnblogs.com/flyall/p/17177310.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   flyall  阅读(67)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.