前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习记录——Unity读取解析外部文本

学习记录——Unity读取解析外部文本

原创
作者头像
MrLi001
发布2022-04-20 14:53:15
1.1K0
发布2022-04-20 14:53:15
举报
文章被收录于专栏:xixixixi

话不多说直接上代码,其实就是调用已有函数,并解析到的是StreamingAssets中的文本,这样 不用访问绝对路径

关键代码:

代码语言:c#
复制
string txt = File.ReadAllText(Application.streamingAssetsPath + configPath);

对于读取到的文本内容进行分割:

代码语言:c#
复制
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;

public class Begimnread : MonoBehaviour
{
    // Start is called before the first frame update
     string configPath = "/Config.txt";
     public Text[] texts;
    void Start()
    {
        string txt = File.ReadAllText(Application.streamingAssetsPath + configPath);
        // 输出该文本的内容
        Debug.Log(txt);
        // 以换行符作为分割点,将该文本分割成若干行字符串,并以数组的形式来保存每行字符串的内容
        string[] str = txt.Split('|');
        // 将该文本中的字符串输出
        Debug.Log("str[0]= " + str[0]);
        Debug.Log("str[1]= " + str[1]);
        Debug.Log("str[2]= " + str[2]);
        // 将每行字符串的内容以逗号作为分割点,并将每个逗号分隔的字符串内容遍历输出
        for (int i = 0; i < 8; i++)
        {
            string[] ss = str[i].Split('[');
            
                texts[2*i].text = ss[0];
                texts[2*i+1].text = ss[1];
                Debug.Log(ss[0]);
                Debug.Log(ss[1]);
                //Debug.Log(ss[2]);
                //Debug.Log(ss[3]);
        }
    }
    // Update is called once per frame
    void Update()
    {   
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档