从最后一个实例化到第一个实例化,一个一个地销毁具有相同标签的实例化预制件?

Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first?

我正在尝试销毁具有相同标签的实例化预制件,从最后一个实例化到第一个实例化,一个一个地销毁,但我一直在思考如何实现这一点。

我的想法是我想要一个自定义编辑器,它允许我实例化多个预制件,然后 "undo" 最后一个实例化有两个 GUI 按钮 - "Place object" 和 "Undo", 分别.

截至目前,我能够成功实例化预制件,为它们添加相同的标签,然后将它们一个接一个地销毁,但它们从第一个实例化到最后一个实例化都会被销毁。

我目前的代码:

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

public class ObjectInstantiateControl : MonoBehaviour {

 public List<GameObject> prefabList = new List<GameObject>();
 [HideInInspector]
 public int objectSelectionIndex = 0;
 GameObject instance;

public void PlaceObject()
 {
     switch (objectSelectionIndex)
     {
         case 1:

             Debug.Log("Just received a number 1 from the editor");
             GameObject object_A = Resources.Load("Object_A") as GameObject;
             instance = Instantiate(object_A, this.transform.position, this.transform.rotation,  this.transform);

             instance.tag = "UsedObject";
             prefabList.Add(instance);

             break;

         case 2:

             Debug.Log("Just received a number 2 from the editor");
             GameObject object_B = Resources.Load("Object_B") as GameObject;
             instance = Instantiate(object_B, this.transform.position, this.transform.rotation,  this.transform);

             instance.tag = "UsedObject";
             prefabList.Add(instance);
             break;

         case 3:

             Debug.Log("Just received a number 3 from the editor");
             GameObject object_C = Resources.Load("Object_C") as GameObject;
             instance = Instantiate(object_C, this.transform.position, this.transform.rotation,  this.transform);

             instance.tag = "UsedObject";
             prefabList.Add(instance);
             break;

         case 4:

             Debug.Log("Just received a number 4 from the editor, deleting the object");

             prefabList.Remove(GameObject.FindWithTag("UsedObject"));
             DestroyImmediate(GameObject.FindWithTag("UsedObject"));
             break;
     }
 }

以及来自 GUI 按钮 "Place object" 和 "Undo" 的编辑器脚本的部分:

                     GUILayout.BeginHorizontal();

                     if (GUILayout.Button("Place object", GUILayout.Height(25)))
                     {
                         ObjectInstantiateControl myScript = (ObjectInstantiateControl)target;
                         myScript.objectSelectionIndex = 1;
                         myScript.PlaceObject()();

                     }

                     if (GUILayout.Button("Undo", GUILayout.Height(25)))
                     {
                         ObjectInstantiateControl myScript = (ObjectInstantiateControl)target;
                         myScript.objectSelectionIndex = 4;
                         myScript.PlaceObject();

                     }
                     GUILayout.EndHorizontal();

真的需要帮助,欢迎任何想法或建议。提前致谢 ;)

在这种情况下,我会选择队列或链接列表。您可以将所有游戏对象添加到其中,并通过其他方式管理它。

https://msdn.microsoft.com/en-us/library/system.collections.queue(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/he2s3bh7(v=vs.110).aspx