2016-02-26 53 views
-1

如果你能幫我解決這個問題,我將不勝感激。如何防止在IEnumerator方法中銷燬添加到List的類的實例?

OpponentItem類的字段應該填充來自collectItems的數據。

我們的對手的物品將在opponentList中列出,其數據來自incomingData。由於我不知道什麼時候StartCoroutine結束了collectData方法集progressBar和Update檢查了opponentList的一個元素。

不幸的是,在該方法中實例化的OpponentItem對象不見了,因此列表爲空。

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

public class OpponentItem { 

    public int item_id ; 
    public string item_type; 
    public string item_name; 
    public int item_star; 
    public int item_bonus; 

    public OpponentItem() : base(){ 

    } 

} 

public class TestBug : MonoBehaviour { 

    public List<OpponentItem> opponentList; 
    private float barValue; 
    public bool isDone; 

    public static string[] collectItems = new string[5]{ 

     "item_id=12", 
     "item_type=Weapon", 
     "item_name=Sword", 
     "item_star=2", 
     "item_bonus=10", 

    }; 

    public string[] incomingData; 

    public float progressBar { 

     get{ 

      return barValue; 

     } 
     set{ 

      barValue = value; 
      isDone = true; 
     } 

    } 

    void Start() { 

     isDone = false; 

     string joined = System.String.Join("|", collectItems); 
     incomingData = new string[5]; 
     incomingData[0] = joined; 
     incomingData[1] = joined; 
     incomingData[2] = joined; 
     incomingData[3] = joined; 
     incomingData[4] = joined; 

     opponentList = new List<OpponentItem>(); 

     StartCoroutine(collectData<OpponentItem>(opponentList, incomingData, collectItems)); 

    } 

    void Update(){ 

     if (isDone){ 

      isDone = false; 
      Debug.Log(opponentList[1].item_id); 

     } 
    } 

    public IEnumerator collectData<T>(List<T> list, string[] tempArray, string[] queryArray) where T : new() { 

     list = new List<T>(tempArray.Length); 

     for(int h = 0; h < tempArray.Length ; h++){ 

      list.Add(new T()); 

      string[] mybox = new string[queryArray.Length]; 
      mybox = tempArray[h].Split('|'); 

      for (int k = 0; k < queryArray.Length ; k++){ 

       string[] inbox = new string[2]; 
       inbox = mybox[k].Split('='); 

       if (list[h].GetType().GetField(inbox[0]).FieldType.FullName == "System.Int32"){ 

        list[h].GetType().GetField(inbox[0]).SetValue(list[h], Int32.Parse(inbox[1])); 
        Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h])); 

       } 
       else if(list[h].GetType().GetField(inbox[0]).FieldType.FullName == "System.Single"){ 

        list[h].GetType().GetField(inbox[0]).SetValue(list[h], Single.Parse(inbox[1])); 
        Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h])); 
       } 
       else{ 

        list[h].GetType().GetField(inbox[0]).SetValue(list[h], inbox[1]); 
        Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h])); 
       } 

      } 

     } 

     yield return new WaitForSeconds(0.2f); 
     progressBar += 0.5f; 
    } 


} 

回答

1

這是一個問題。您的列表將永遠是空的,因爲默認情況下參數是按值傳遞(見本MSDN鏈接)

opponentList = new List<OpponentItem>();   
StartCoroutine(collectData<OpponentItem>(opponentList, incomingData, collectItems)); 

一旦指定list = new List<T>(tempArray.Length)你把你的資料轉移到新名單的參考,但不能傳遞到一個收集數據()。

你有2種選擇:

1)清除列表,而不是重新分配

public IEnumerator collectData<T>(List<T> list, string[] tempArray, string[] queryArray) where T : new() { 

    // list = new List<T>(tempArray.Length); // ** problem 
    list.Clear(); // ** try clearing the list instead 

    for(int h = 0; h < tempArray.Length ; h++){ 

2)按引用傳遞列表

opponentList = new List<OpponentItem>(); 

StartCoroutine(collectData<OpponentItem>(ref opponentList, incomingData, collectItems)); 

} 

    public IEnumerator collectData<T>(ref List<T> list, string[] tempArray, string[] queryArray) where T : new() { 

    list = new List<T>(tempArray.Length); 
+0

第一種選擇作品 – Malekius