如何将 csv 文件中的值分配给数组或列表 [英] How assign values from a csv file to a array or list

查看:24
本文介绍了如何将 csv 文件中的值分配给数组或列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一种问答游戏.我已经设法开发了它的基本工作.我正在使用序列化字段来更新问题.现在我打算使用 CSV 文件来回答问题.

Currently i'm developing a quiz kind of game. I've managed to develop the basic working of it. I was using the serialized field for updating the questions. Now i'm planning to use a CSV file for the questions.

CSV阅读器

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

我正在尝试将值分配给这个问题类

I'm trying to assign the values to this question class

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;
    }
} 

}

我的游戏经理,用于生成问题

My game manager for generating questions

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天.有什么办法可以做到这一点.我知道我错过了一些东西.抱歉我弄乱了代码.

Getting a NullReferenceException on SetCurrentFact() every time i try doing this. Even tried assigning the values directly on that method. Being stuck up on this for 2 days. Is there any way i can do this. I know i'm missing something. Sorry for my messed up code.

推荐答案

在使用之前,facts字段似乎没有初始化.

it appears facts field is not initialized before using.

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

but that would cause the error in Start method instead of SetCurrentfact

这篇关于如何将 csv 文件中的值分配给数组或列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆