2017-02-21 2 views
0

私は、最後にインスタンス化されたものから最初にインスタンス化されたものから、同じタグを持つインスタンス化されたプレハブを一つずつ破棄しようとしています。最後にインスタンス化されたものから最初にインスタンス化されたものから、同じタグを持つインスタンス化されたプレハブを1つずつ破棄しますか?

私は、複数のプレハブをインスタンス化し、最後に2つのGUIボタン「オブジェクトを配置」と「元に戻す」でそれぞれ「元に戻す」ことができるカスタムエディタを用意したいと考えています。

現在のところ、私は正常にプレハブをインスタンス化し、それらに同じタグを追加し、それらを1つずつ破棄することができますが、最初から最後までインスタンス化されています。

私が持っているコードを、これまで:

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

は本当にこの上で助けが必要な

、任意のアイデアや提案は大歓迎です。ありがとうございます;)

+0

「元に戻す」部分は「オブジェクトを配置」部分とまったく同じに見えますか? –

+0

リストの最後のアイテムをポップして破棄するのではなく、 'FindWithTag'を使用している理由はありますか? – rutter

答えて

0

この場合、私はキューまたは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 
関連する問題