如何在运行时销毁多个gameObject? [英] How to Destroy multiple gameObject at runtime?

查看:426
本文介绍了如何在运行时销毁多个gameObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在unity3D中,我正在运行时动态创建和销毁胶囊.我使用 space 创建胶囊,并使用 C 进行销毁.

In unity3D I am creating and destroying capsule dynamically at run-time. I used space to create capsule and C for destroying.

我想创建多个对象并同时销毁多个对象.当我多次按 Space (对象)键多次创建对象时,效果很好.

I want create multiple object and destroy multiple object at time. When I pressed Space multiple times object is creating multiple time it fine.

但是问题是,当我多次按下 C 时,只有一个物体被摧毁.如何实现销毁多个对象?一对一.

But the problem is when I pressed C multiple times only one object is destroying. How I can achieve destroying multiple object? One by one.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class DynamicCreate : MonoBehaviour 
 {     
     public GameObject caps;

     // Update is called once per frame
     void Update () 
     {
          if (Input.GetKeyDown(KeyCode.Space))
          {
               createObject();
          }

          if (Input.GetKeyDown(KeyCode.C))
          {
               destroyObject();
          }
     }

     private void createObject()
     {
          caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
     }

     public void destroyObject()
     {
          Destroy(caps);
     }
}

推荐答案

我想在bolkay答案中添加更多内容.如果要添加和删除一个要使用的最佳数据结构,那就是一个队列.

I would like to add more to bolkay answer. If you want to add and remove one by one best datastructure to use is a queue.

Queue<GameObject> Capsules;
void Start()
{
    Capsules = new Queue<GameObject>();
}

private void createObject()
{
    GameObject caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    Capsules.Enqueue(caps);
}

public void destroyObject()
{
    if(Capsules.Count > 0)
        Destroy(Capsules.Dequeue());   
}

Queue 数据结构将采用先进先出的方式.因此,它将给出您在运行时添加到队列中的第一个游戏对象,并且您将不需要像List一样需要变量来管理索引.

Queue data structure will give a first in first out. So it will give out first gameobject which you added to the queue at runtime and you wont need a variable to manage the index as in the state of List.

注意:该解决方案仍然是非常基础的,但是我建议您使用对象池而不是在运行时创建和销毁对象,因为在运行时创建和销毁对象非常费力.因此,除非绝对必要,否则应避免创建和删除.

Note: This solution is still very basic but I would recommend you to use object pooling instead of creating and destroying objects at runtime as creating and destorying objects is quite intensive at runtime. So you should unless abosolutely necessary avoid creating and deleting.

Queue<GameObject> pooledObjects;
Queue<GameObject> Capsules;
void Start()
{
    pooledObjects = new Queue<GameObject>();

    Capsules = new Queue<GameObject>();
}

private void createObject()
{
    if(pooledObjects.Count > 0)
    {
        GameObject fetchedObject = pooledObjects.Dequeue();
        fetchedObject.SetActive(true);
        //Do translations,scaling or other stuff on this reference

        //Add it back to main list of things present in scene.
        Capsules.Enqueue(fetchedObject);
    } else
    {
        //Only create objects if necessary
        GameObject caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        Capsules.Enqueue(caps);
    }            
}

public void destroyObject()
{
    //here instead of destorying the object we will just disable them and keep them out if scene so we dont have to recreate them everytime
    if(Capsules.Count > 0)
    {
        GameObject fetchedObject = Capsules.Dequeue();
        fetchedObject.SetActive(false);//Turn the object off from scene
        pooledObjects.Equals(fetchedObject);
    }   
}


以下是您可能想使用视您的情况而定.


Here is list of supported datastructures in C# you might want to use depending on your required case.

这篇关于如何在运行时销毁多个gameObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆