Unity加载CSV配置表

上代码,不需要用插件乱七八糟的东西,因为我这里导出webgl被各种插件折磨到了

注意:格式需为 xlsx转csv的 CSV UTF-8格式文件 放在Resources目录下

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD_喵了个咪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值