Unity 实用技巧 之 简便的物体一波一波自动生成

2025-10-05 07:58:29

1、Object.Instantiate:

1)功能描述

Clones the object original and returns the clone.

This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject then you can also optionally specify its position and rotation (these default to the original GameObject's position and rotation otherwise). If you are cloning a Component then the GameObject it is attached to will also be cloned, again with an optional position and rotation.When you clone a GameObject or Component, all child objects and components will also be cloned with their properties set like those of the original object.

2)使用案例

using UnityEngine; 

using System.Collections;

public class ExampleClass : MonoBehaviour {

    public Rigidbody projectile;

    void Update() {

        if (Input.GetButtonDown("Fire1")) {

            Rigidbody clone;

            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;

            clone.velocity = transform.TransformDirection(Vector3.forward * 10);

        }

    }

 }

2、方法要点:

1)设置生成单体的计时器和时间间隔

    private float timerOne = 1f;    private float timeOne = 1.0f;

2)设置生成一波的计时器和时间间隔

    private float timerWave = 0f;    private float timeWave = 10.0f;

3)设置生成一波的数量和预制体

    private int countPerWave = 0;    public GameObject spawnPerfab;

4)计时的方法

    timerWave += Time.deltaTime;

1、打开Unity,新建一个空工程,具体如下

Unity 实用技巧 之 简便的物体一波一波自动生成

2、在场景中,新建一个“Plane”,和“Cube”,调小“Cube”的比例,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

3、新建2个脚本,命名为“MoveDestroyTest”、“SpawnTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

4、在打开的“MoveDestroyTest”脚本上编写代码,首先在Start函数里面调用3庙后自我销毁函数,然后在Update函数里面设置自我移动,最后实现自我销毁函数,代码及代码说明如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

5、“MoveDestroyTest”脚本具体内容如下

using UnityEngine;

public class MoveDestroyTest : MonoBehaviour {

    // Use this for initialization

    void Start () {

        DeatroySlef3Second ();

    }

        // Update is called once per frame

    void Update () {

        transform.Translate (Vector3.right * Time.deltaTime * 2    );

    }

    private void DeatroySlef3Second() {

        Destroy (transform.gameObject, 3);

    }

}

6、在打开的“SpawnTest”脚本上编写代码,首先设置生成一个预制体的计时器和时间间隔,以及一波预制体生成的计时器和时间间隔,然后设置生成的数量计数和生成预制体,接着先计时一波的时间,在时间内以及数量内进行每一个的计时与生成计数,随后一波时间到达后,重新计时与重置生成预制体数量,代码及代码说明如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

7、“SpawnTest”脚本具体内容如下:

using UnityEngine;

public class SpawnTest : MonoBehaviour {

    private float timerOne = 1f;

    private float timeOne = 1.0f;

    private float timerWave = 0f;

    private float timeWave = 10.0f;

    private int countPerWave = 0;

    public GameObject spawnPerfab;

        // Update is called once per frame

    void Update () {

        timerWave += Time.deltaTime;

        if(timerWave < timeWave && countPerWave != 5) {

            timerOne += Time.deltaTime;

            if(timerOne > timeOne) {

                Instantiate (spawnPerfab, new Vector3(-3.5f , 0.5f,

                    Random.Range (-4.0f, 4.0f)), 

                    spawnPerfab.transform.rotation);

                                countPerWave++;

                timerOne -= timeOne;

            }

        }

        if (timerWave >= timeWave) {

            timerWave -= timeWave;

            countPerWave = 0;

        }

            }

}

8、脚本编译正确后,回到 Unity 界面,把“MoveDestroyTest”赋给“Cube”,并把“Cube”做成预制体,场景中的“Cube”,可以删除掉,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

9、在场景中新建一个“GameObject”,把脚本“SpawnTest”赋给“GameObject”,并把预制体“Cube”赋值给脚本“SpawnTest”,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

10、运行场景,即可看到场景中预制体一波一波的自动生成,自动移动,自动销毁,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

11、到此,《Unity 实用技巧 之 简便的物体一波一波自动生成》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢