如何将csv文件中的值分配给数组或列表

时间:2017-03-04 09:17:27

标签: c# unity3d unity5

目前我正在开发一种测验游戏。我已经成功地开发了它的基本工作。我正在使用序列化字段来更新问题。现在我打算使用CSV文件来解决问题。

CSVReader

using UnityEngine;
using System.Collections.Generic;

public class CSVReader : MonoBehaviour
{
public TextAsset csvFile;


[System.Serializable]
public class Row
{

    public string Id;
    public string Fact;
    public string IsTrue;

}




public static List<Row> rowList = new List<Row>();
bool isLoaded = false;

void Start()
{
    Load(csvFile);


}

public bool IsLoaded()
{
    return isLoaded;
}

public  List<Row> GetRowList()
{
    return rowList;
}

public void Load(TextAsset csv)
{
    rowList.Clear();
    string[][] grid = CsvParser2.Parse(csv.text);
    for (int i = 1; i < grid.Length; i++)
    {
        Row row = new Row();
        row.Id = grid[i][0];
        row.Fact = grid[i][1];
        row.IsTrue = grid[i][2];

        rowList.Add(row);
    }
    isLoaded = true;
}

public int NumRows()
{
    return rowList.Count;
}

public static Row GetAt(int i)
{
    if (rowList.Count <= i)
        return null;
    return rowList[i];
}

public static Row Find_Id(string find)
{
    return rowList.Find(x => x.Id == find);
}
public List<Row> FindAll_Id(string find)
{
    return rowList.FindAll(x => x.Id == find);
}
public static Row Find_Fact(string find)
{
    return rowList.Find(x => x.Fact == find);
}
public List<Row> FindAll_Fact(string find)
{
    return rowList.FindAll(x => x.Fact == find);
}
public static Row Find_IsTrue(string find)
{
    return rowList.Find(x => x.IsTrue == find);
}
public List<Row> FindAll_IsTrue(string find)
{
    return rowList.FindAll(x => x.IsTrue == find);
}
}

我试图将值分配给此问题类

using UnityEngine;



public class Question : MonoBehaviour
{


CSVReader csvr = new CSVReader();


public string Fact;


public bool IsTrue;



    public void Start()
{
    GameObject PlayCon = GameObject.FindWithTag("GameController");
    if (PlayCon != null)
    {
        csvr = PlayCon.GetComponent<CSVReader>();
    }
    if (csvr == null)
    {
        Debug.Log("Cannot find 'GameController' script");
    }


}

public void FixedUpdate()
{
    for (int i = 1; i <= 2; i++)
    {
        Fact = CSVReader.Find_Id("i").Fact;
        Debug.Log(Fact);
        if (CSVReader.Find_Id("i").IsTrue == "true")
        {
            IsTrue = true;
        }
        else
            IsTrue = false;
    }
} 

}

我的游戏经理提出问题

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

using UnityEngine.UI;


using System.Linq;


using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {




public Question[] facts;

private static List<Question> unansweredfacts;


private Question currentfacts;


[SerializeField]
private Text FactText;

[SerializeField]
private float TimeBetweenFacts = 1f;


[SerializeField]
private Text TrueAnswerText;


[SerializeField]
private Text FalseAnswerText;


[SerializeField]
private Animator animator;



[SerializeField]
public GameObject canvasquiz;


CSVReader csvr = new CSVReader();






void Start()
{


    if (unansweredfacts == null || unansweredfacts.Count == 0)
    {
        unansweredfacts = facts.ToList<Question>();
    }


    SetCurrentfact();

    Debug.Log(currentfacts.Fact + "is" + currentfacts.IsTrue);




}

void SetCurrentfact()
{
    int RandomFactIndex = Random.Range(0, unansweredfacts.Count);
    currentfacts = unansweredfacts[RandomFactIndex];




        FactText.text = currentfacts.Fact;



    if (currentfacts.IsTrue)
    {
        TrueAnswerText.text = "CORRECT !";
        FalseAnswerText.text = "WRONG !";
    }
    else
    {
        TrueAnswerText.text = "WRONG !";
        FalseAnswerText.text = "CORRECT !";
    }


}

IEnumerator TransitionToNextFact()
{
    unansweredfacts.Remove(currentfacts);
    canvasquiz.SetActive(false); 
    yield return new WaitForSeconds(TimeBetweenFacts);

    SetCurrentfact(); 

    canvasquiz.SetActive(true); 


}



 public void UserSelected(bool isTrue)
 {
    animator.SetTrigger(isTrue.ToString());
    //Debug.Log(isTrue.ToString);
    if (currentfacts.IsTrue == isTrue)
    {
        Debug.Log("CORRECT !");

  }
    else
    {
        Debug.Log("WRONG !");

    }


    StartCoroutine(TransitionToNextFact());
}

每次尝试这样做时,在SetCurrentFact()上获取NullReferenceException。甚至尝试直接在该方法上分配值。被困在这2天。有什么方法可以做到这一点。我知道我错过了什么。对不起我乱糟糟的代码。

1 个答案:

答案 0 :(得分:0)

看起来事实字段在使用前未初始化。

但这会导致Star​​t方法中的错误而不是SetCurrentfact