unity 安卓和Pc平台读取本地文件

经过测试在安卓平台无法用File来读取StraemingAssets文件夹里的内容

 

 会找不到文件

  string path;

    void Awake()
    {
        string filepath = Application.streamingAssetsPath;
        path = filepath + "/Lua/test.txt";
        LoadTextAsset();
        GetAllFileTextInfos();
        StartCoroutine(StreamReaderJsonFromSSPath());
    }

    public string LoadTextAsset()
    { 
        return File.ReadAllText(path);
    }

    public string GetAllFileTextInfos()
    {
        try
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                fs.Seek(0, SeekOrigin.Begin);
                var bytes = new byte[fs.Length];
                fs.Read(bytes, 0, (int)fs.Length);
                string s = Encoding.UTF8.GetString(bytes);
                fs.Flush();
                fs.Dispose();
                fs.Close();
                return s;
            }
            //Debug.Log("所有文件资源信息Assets下文件夹数量:" + fileInfos.Count);
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            return null;
        }
    }

    IEnumerator StreamReaderJsonFromSSPath()
    {
        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;
                }
            }
        }
        Debug.Log(str);
        webRequest.Dispose();
    }

    public void GetFiles()
    {
        //路径
        string filepath = Application.streamingAssetsPath;

        //获取指定路径下面的所有资源文件
        if (Directory.Exists(filepath))
        {
            DirectoryInfo direction = new DirectoryInfo(filepath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

            Debug.Log(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                Debug.Log("Name:" + files[i].Name);
                Debug.Log("FullName:" + files[i].FullName);
                Debug.Log("DirectoryName:" + files[i].DirectoryName);
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity WebGL应用程序中,由于浏览器的安全性限制,它无法直接访问用户的本地文件系统。因此,如果您需要读取用户的本地文件,您需要让用户手动选择文件并上传到您的服务器,然后从服务器端读取文件。 但是,如果您只是需要让用户选择文件并在WebGL应用程序中加载该文件,您可以使用HTML5的File API和Unity的WWW类来实现。具体步骤如下: 1. 在HTML页面中创建一个文件输入框,并在用户选择文件后触发一个回调函数。 ```html <input type="file" id="fileInput" onchange="loadFile()" /> ``` 2. 在回调函数中使用File API读取用户选择的文件,将文件数据传递给Unity的JavaScript代码。 ```javascript function loadFile() { var fileInput = document.getElementById("fileInput"); var file = fileInput.files[0]; var reader = new FileReader(); reader.onload = function (e) { var fileData = e.target.result; unityInstance.SendMessage("YourGameObjectName", "LoadFile", fileData); }; reader.readAsArrayBuffer(file); } ``` 3. 在Unity的JavaScript代码中,使用WWW类加载从HTML页面中传递过来的文件数据。 ```javascript function LoadFile(fileData) { var url = URL.createObjectURL(new Blob([fileData])); var www = new WWW(url); // Do something with the loaded file } ``` 需要注意的是,由于加载本地文件是一个异步操作,因此您需要在代码中处理异步加载的情况。另外,由于这种方法需要上传文件到服务器,因此对于大型文件或需要频繁读写的文件,可能不是一个有效的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值