【问题标题】:How assign values from a csv file to a array or list如何将 csv 文件中的值分配给数组或列表
【发布时间】:2017-03-04 09:17:26
【问题描述】:

目前我正在开发一种问答游戏。我已经设法开发了它的基本工作。我正在使用序列化字段来更新问题。现在我打算使用 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天。有什么办法我可以做到这一点。我知道我错过了一些东西。对不起,我搞砸了代码。

【问题讨论】:

  • facts 的内容分配到哪里去了?看起来它在某处包含一个空值。您必须在调试器下运行它才能找出问题所在。
  • Id,Fact,IsTrue 1,i'm good,true 2,i'm bad,false csvFile.txt
  • 您能告诉我们 SetCurrentFact() 方法中的哪一行导致 NullReferenceException 吗?

标签: c# unity3d unity5


【解决方案1】:

在使用之前,事实字段似乎没有初始化。

但这会导致 Start 方法而不是 SetCurrentfact 的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多