Unity中间隔时间产生预制件

目录

一、目的:

1、需求:间隔时间随机在几个诞生地产生蝴蝶预制件,产生在制定文件夹下面,并且都是数组,可以随时添加诞生地和蝴蝶预制件

一、代码

1、需要的类成员变量

1、 Start

1、 CreateButterfly

1、文件设置:诞生地、蝴蝶预制件都是数组,可以不断的叠加

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、运行效果

蝴蝶会间隔一段时间随机在诞生地产生,蝴蝶预制件也是循环的产生

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值