我试图从最后一次实例化到第一次使用相同的标签逐个销毁实例化的预制件,我仍然坚持如何实现这一点。
我的想法是,我希望有一个自定义编辑器,这将允许我实例化多个预制件,然后"撤消"最后一次实例化,有两个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按钮编辑器脚本的部分&#34;放置对象&#34;和&#34;撤消&#34; :
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();
真的需要帮助,欢迎任何想法或建议。在此先感谢;)
答案 0 :(得分:0)
在这种情况下,我将选择Queue或LinkList。您可以将所有游戏对象添加到其中,管理它将以其他方式简单而精细。
print