Unity加载CSV配置表
上代码,不需要用插件乱七八糟的东西,因为我这里导出webgl被各种插件折磨到了
注意:格式需为 xlsx转csv的 CSV UTF-8格式文件
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class CsvData
{
public string id;
public string
}
public class CSVReader : MonoBehaviour
{
// Start is called before the first frame update
public Text txt_value,txt_value1;
void Start()
{
//这个 放在streamingAssets的反而没用---
txt_value.text = "";
string filePath =Application.streamingAssetsPath+"/Language1.csv";
Debug.Log(filePath);
StartCoroutine(ReadCSV(filePath));
//Res的方式加载:打开文件Language1.csv,不用加后缀
TextAsset EnemyDatas = Resources.Load<TextAsset>("Language1");
string[] str_row = EnemyDatas.text.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);//拿到所有行
string[] value = str_row[str_row.Length-1].Split(',');//拆分到哪个
txt_value1.text =value[0] + " " + value[1] + " " + value[2] + " " + value[3];
}
// Update is called once per frame
void Update()
{
}
private StringBuilder stbLine = new StringBuilder();
private IEnumerator ReadCSV(string path)
{
UnityWebRequest www = UnityWebRequest.Get(path);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: " + www.error);
}
else
{
Debug.Log("下载完毕:"+www.downloadHandler.text);
string[] lines = www.downloadHandler.text.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
//Debug.Log(line);
string[] values = line.Split(',');
stbLine.Clear();
stbLine.Append(values[0] + " " + values[1] + " " + values[2] + " " + values[3]);
Debug.Log(stbLine);
txt_value.text += stbLine.ToString() + "\n";
foreach (string value in values)
{
//Debug.Log(value);
}
}
}
}
}