目录
1、需求:间隔时间随机在几个诞生地产生蝴蝶预制件,产生在制定文件夹下面,并且都是数组,可以随时添加诞生地和蝴蝶预制件
一、目的:
1、需求:间隔时间随机在几个诞生地产生蝴蝶预制件,产生在制定文件夹下面,并且都是数组,可以随时添加诞生地和蝴蝶预制件
一、代码
1、需要的类成员变量
/// <summary>Image数组类型:蝴蝶的预制件</summary>
public GameObject[] arr_obj_butterfly;
/// <summary>GameObject类型:蝴蝶诞生地</summary>
public GameObject obj_butterflyParent;
/// <summary>GameObject数组类型:蝴蝶的诞生地</summary>
public GameObject[] arr_obj_butterfly_birthplace;
/// <summary>float类型:蝴蝶的诞生的间隔</summary>
public float f_interval_createButterfly;
/// <summary>int类型:蝴蝶在场景中同时出现的总数量</summary>
public int i_butterfly_totalNum;
/// <summary>int类型:蝴蝶在蝴蝶的预制件中的序号,从第一个开始</summary>
private int i_butterfly_currentIndex;
1、 Start
// Use this for initialization
void Start()
{
i_butterfly_currentIndex = 1;
InvokeRepeating("CreateButterfly", f_interval_createButterfly, f_interval_createButterfly);
}
1、 CreateButterfly
/// 功能:通过游戏模式来产生、摧毁蝴蝶
/// </summary>
private void CreateButterfly()
{
GameObject[] arr_butterfly = GameObject.FindGameObjectsWithTag("butterfly");
if (GameObject.FindGameObjectWithTag("ScriptsHold").GetComponent<My_test10_catchButterflyNumber>().catchButterflyNumber < 5)
{
if (arr_butterfly.Length >= i_butterfly_totalNum)
{
return;
}
else
{
//蝴蝶数量不满足,继续产生
int tmp_index_birthplace = Random.Range(0, arr_obj_butterfly_birthplace.Length - 1);
int tmp_index_butterfly = i_butterfly_currentIndex % arr_obj_butterfly.Length;
i_butterfly_currentIndex++;
GameObject butterfly = GameObject.Instantiate(arr_obj_butterfly[tmp_index_butterfly], arr_obj_butterfly_birthplace[tmp_index_birthplace].transform.position, Quaternion.identity);
butterfly.transform.SetParent(obj_butterflyParent.transform);
butterfly.transform.localScale = new Vector3(50,50, 50);
}
}
}
1、文件设置:诞生地、蝴蝶预制件都是数组,可以不断的叠加
1、运行效果
蝴蝶会间隔一段时间随机在诞生地产生,蝴蝶预制件也是循环的产生