//2,将平面Cube名字更改为CreateBoxPlane
public class TestSxeneCtrl : MonoBehaviour {
//先定义一个Transform
/// <summary>
/// 创建箱子的区域
/// </summary>
[SerializeField] //将Hierarchy面板CreateBoxPlane拖过来 平面 箱子在游戏场景出现位置
private Transform transCreateBox;
/// <summary>
/// 箱子的父物体 箱子在什么里面出现克隆位置
/// </summary>
[SerializeField] //将Hierarchy面板xiangziParent拖过来 箱子预设体克隆位置
private Transform boxParent;
/// <summary>
/// 箱子预设
/// </summary>
private GameObject BoxPrefab;
/// <summary>
/// 定义克隆上线
/// </summary>
private int MaxCount = 10;
/// <summary>
/// 下次克隆时间
/// </summary>
private float NextCloneTime = 0f;
/// <summary>
/// 当前箱子数量
/// </summary>
private int CurrCount = 0;
// Use this for initialization
void Start () {
//预设是怎么加载的 预设文件路径
BoxPrefab = Resources.Load("Roleprefab/Item/xiangzi")as GameObject;
}
// Update is called once per frame
void Update () {
//如果当前数量小于最大数量那么是可以克隆的
if(CurrCount<MaxCount)
{
//如果当前时间大于下次的克隆时间则进行克隆
if(Time.time>NextCloneTime)
{
//克隆之后吧时间设置一下 下次克隆时间是当前克隆时间+3f
NextCloneTime = Time.time + 3f;
//开始克隆代码 克隆就是用Instantiate这样的方法就可以加上要克隆的名字(BoxPrefab)
GameObject objClone = Instantiate(BoxPrefab) as GameObject;
//克隆出来之后把它放在箱子的父物体里面
objClone.transform.parent = boxParent;
//同时箱子要出现的区域 箱子区域 坐标 随机位置 缩放区域大小
objClone.transform.position = transCreateBox.transform.TransformPoint(new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f)));
//当前箱子的数量++ 自加一
CurrCount++;
}
}
}
}