当前位置: 首页 > article >正文

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


http://www.kler.cn/a/469446.html

相关文章:

  • 前端同步发送HTTP请求 Ajax、Fetch和Axios实现HTTP同步请求
  • YOLOv5部署到web端(flask+js简单易懂)
  • 第27天:Web开发-PHP应用原生语法全局变量数据接受身份验证变量覆盖任意上传
  • 谷歌浏览器的安全检测工具使用指南
  • 开源复刻 | Matlab实现蚁狮优化算法ALO优化Transformer-LSTM实现负荷数据回归预测
  • Sql中WITH的作用
  • pygame飞机大战
  • SpringMVC(二)原理
  • 【QT】C++线程安全的单例模板
  • Docker容器中Elasticsearch内存不足问题排查与解决方案
  • 【车载网络】BUSOFF状态简述和制造
  • Go语言的 的输入/输出流(I/O Streams)核心知识
  • LeetCode:700.二叉搜索树中的搜索
  • ThreadLocal` 的工作原理
  • Apache zookeeper集群搭建
  • Java-数据结构-时间和空间复杂度
  • Python 标准库:hashlib——安全哈希与消息摘要
  • ARM CCA机密计算安全模型之加密建议
  • 26 go语言(golang) - GC原理
  • 系统架构师考试-MDA模型驱动架构