【问题标题】:Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first?从最后一个实例化到第一个实例化的具有相同标签的实例化预制件一个接一个地销毁?
【发布时间】:2017-02-22 11:57:51
【问题描述】:

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

我的想法是我想要一个自定义编辑器,它允许我实例化多个预制件,然后使用两个 GUI 按钮“撤消”最后一个实例化 - 分别是“放置对象”和“撤消”。

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

我目前的代码:

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 按钮“放置对象”和“撤消”的编辑器脚本部分:

                     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();

在这方面确实需要帮助,欢迎任何想法或建议。在此先感谢 ;)

【问题讨论】:

  • “撤消”部分看起来与“放置对象”部分完全相同?
  • 您是否有理由使用FindWithTag 而不是仅仅从列表中弹出最后一项并销毁它?

标签: c# unity3d unity5


【解决方案1】:

在这种情况下,我将选择 Queue 或 LinkList。您可以将所有游戏对象添加到其中,并以其他方式对其进行管理。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多