协程的用法

using UnityEngine;
using System.Collections;


public class BackgroundPropSpawner : MonoBehaviour
{
public Rigidbody2D backgroundProp; // The prop to be instantiated.
public float leftSpawnPosX; // The x coordinate of position if it's instantiated on the left.
public float rightSpawnPosX; // The x coordinate of position if it's instantiated on the right.
public float minSpawnPosY; // The lowest possible y coordinate of position.
public float maxSpawnPosY; // The highest possible y coordinate of position.
public float minTimeBetweenSpawns; // The shortest possible time between spawns.
public float maxTimeBetweenSpawns; // The longest possible time between spawns.
public float minSpeed; // The lowest possible speed of the prop.
public float maxSpeed; // The highest possible speeed of the prop.


void Start ()
{
// Set the random seed so it's not the same each game.
Random.seed = System.DateTime.Today.Millisecond;


// 开始程序
StartCoroutine("Spawn"); 
}




IEnumerator Spawn () //协程的定义
{
//随机生成一个时间,推迟此函数继续执行
float waitTime = Random.Range(minTimeBetweenSpawns, maxTimeBetweenSpawns);


// waitTime秒后回来继续执行
yield return new WaitForSeconds(waitTime);


// 向左还是向右
bool facingLeft = Random.Range(0,2) == 0;


// If the prop is facing left, it should start on the right hand side, otherwise it should start on the left.
float posX = facingLeft ? rightSpawnPosX : leftSpawnPosX;


// Create a random y coordinate for the prop.
float posY = Random.Range(minSpawnPosY, maxSpawnPosY);


// 精灵出现的位置
Vector3 spawnPos = new Vector3(posX, posY, transform.position.z);


// 生成精灵
Rigidbody2D propInstance = Instantiate(backgroundProp, spawnPos, Quaternion.identity) as Rigidbody2D;


// 设置精灵的面向方向
if(!facingLeft)
{
// ... flip the scale in the x axis.
Vector3 scale = propInstance.transform.localScale;
scale.x *= -1;
propInstance.transform.localScale = scale;
}


// Create a random speed.
float speed = Random.Range(minSpeed, maxSpeed);


// These speeds would naturally move the prop right, so if it's facing left, multiply the speed by -1.
speed *= facingLeft ? -1f : 1f;


// Set the prop's velocity to this speed in the x axis.
propInstance.velocity = new Vector2(speed, 0);


//继续创建下一个精灵
StartCoroutine(Spawn());


//此代码用来判断怪物是否有超出场景,若超出就杀死怪物
while(propInstance != null)
{
// ... and if it's facing left...
if(facingLeft)
{
// ... and if it's beyond the left spawn position...
if(propInstance.transform.position.x < leftSpawnPosX - 0.5f)
// ... destroy the prop.
Destroy(propInstance.gameObject);
}
else
{
// Otherwise, if the prop is facing right and it's beyond the right spawn position...
if(propInstance.transform.position.x > rightSpawnPosX + 0.5f)
// ... destroy the prop.
Destroy(propInstance.gameObject);
}


// 必须加这个,否则就死循环了,因为如果怪物没有超出距离的话将永远的执行下去
yield return null; 
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值