using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class Test2 : MonoBehaviour
{
    string path;
    int loadNum = 0;
    List<Coroutine> coroutines = new List<Coroutine>();

    void Awake()
    {
        string filepath = Application.streamingAssetsPath;
        path = filepath + "/file.bytes";
        coroutines.Add(StartCoroutine(StreamReaderJsonFromSSPath(path, false,"", "")));
    }

    IEnumerator StreamReaderJsonFromSSPath(string path,bool isLoad,string filePath,string fileName)
    {
        UnityWebRequest webRequest = UnityWebRequest.Get(path);
        webRequest.SendWebRequest();//读取数据
        string str;
        while (true)
        {
            yield return new WaitForSeconds(0.1f);
            if (webRequest.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.LogError(path + "请求错误:" + webRequest.error);
            }
            else
            {
                if (webRequest.downloadHandler.isDone)//是否读取完数据
                {
                    //读取
                    //str = Encoding.UTF8.GetString(webRequest.downloadHandler.data);
                    str = webRequest.downloadHandler.text;
                    break;
                }
            }
        }
        webRequest.Dispose();
        if (!isLoad)
        {
            DisplayString(StringSplit(str.TrimEnd(), '\n', ','));
        }
        else
        {
            CreateFile(filePath,fileName,str);
            loadNum--;
            DetectionLoadProcess();
        }
    }

    string[] StringSplit(string s, char split1,char split2)
    {
        return s.Split(split1, split2);
    }

    void DisplayString(string[] s)
    {
        string pt= "";
        for (int i = 0; i < s.Length; i++)
        { 
            if (i % 2 != 0)
            {
                pt = Application.streamingAssetsPath + "/" + s[i];
                StartCoroutine(StreamReaderJsonFromSSPath(pt, true, s[i],s[i-1]));
                loadNum++;
            }
        }
    }

    void CreateFile(string path, string name, string info)
    {
        Directory.CreateDirectory(Application.persistentDataPath + "/" + path.Replace(name, ""));
        //文件流信息
        StreamWriter sw;
        FileInfo t = new FileInfo(Application.persistentDataPath + "/" + path);
        sw = t.CreateText();
        //以行的形式写入信息
        sw.WriteLine(info);
        //关闭流
        sw.Close();
        //销毁流
        sw.Dispose();
    }

    string LoadFile(string path)
    {
        //使用流的形式读取
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(path);
        }
        catch (Exception e)
        {
            //路径与名称未找到文件则直接返回空
            Debug.Log(e);
            return null;
        }
        string line;
        line = sr.ReadToEnd();
        //关闭流
        sr.Close();
        //销毁流
        sr.Dispose();
        //将数组链表容器返回
        return line;
    }

    void DeleteFile(string path)
    {
        File.Delete(path);
    }

    void DetectionLoadProcess()
    {
        if (loadNum <= 0)
        {
            Debug.Log("获取完成。。");
            //停止加载携程
            StopLoadCoroutinue();
            Debug.Log(LoadFile(Application.persistentDataPath + "/Config.txt"));
        }
    }

    void StopLoadCoroutinue()
    {
        for (int i = 0; i < coroutines.Count; i++)
        {
            StopCoroutine(coroutines[i]);
        }
    }

}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐