Unity发布webgl之后修改StreamingAssets 内的配置文件读取到的还是之前的配置文件的解决方案

问题描述

unity发布webgl之后,修改在StreamingAssets 中的配置信息,修改之后读取的还是之前的配置信息

复制代码
读取配置文件的代码

    IEnumerator IE_WebGL_LoadWebSocketServerCopnfig()
    {
        var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, @"Config\WebServerConfig.txt"));
        Debug.Log("输出加载路径:" + uri);
        UnityWebRequest webRequest = UnityWebRequest.Get(uri);

        yield return webRequest.SendWebRequest();

        if (webRequest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError("输出报错:" + webRequest.error);
        }
        else
        {
             var strings = webRequest.downloadHandler.text.Split(':');
            Debug.Log("加载WebSocketServer配置信息:" + strings[0] + ":" + int.Parse(strings[1]));
        }
    }

读取配置文件

修改配置文件之后再次读取,读取到的是修改之前的。

解决方案

设置UnityWebRequest 发送的请求头,设置Cache-Control请求头为no-cache来确保每次请求都不会从缓存中读取数据。

复制代码
    IEnumerator IE_WebGL_LoadWebSocketServerCopnfig()
    {
        var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, @"Config\WebServerConfig.txt"));
        Debug.Log("输出加载路径:" + uri);
        UnityWebRequest webRequest = UnityWebRequest.Get(uri);
        
        webRequest.SetRequestHeader("Cache-Control", "no-cache");//设置无缓存

        yield return webRequest.SendWebRequest();

        if (webRequest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError("输出报错:" + webRequest.error);
        }
        else
        {
             var strings = webRequest.downloadHandler.text.Split(':');
            Debug.Log("加载WebSocketServer配置信息:" + strings[0] + ":" + int.Parse(strings[1]));
        }
    }
相关推荐
jtymyxmz12 小时前
《Unity Shader》6.4.3 半兰伯特模型
unity·游戏引擎
我的golang之路果然有问题14 小时前
mac配置 unity+vscode的坑
开发语言·笔记·vscode·macos·unity·游戏引擎
于小汐在咯15 小时前
【虚拟现实技术】在Unity里创建一个简单的AR项目
unity·ar·vr
HahaGiver66618 小时前
Unity Shader Graph 3D 实例 - 一个简单的红外线扫描全身效果
3d·unity·游戏引擎
l***O5201 天前
免费的WebGL性能优化,纹理压缩与实例化
性能优化·webgl
雪下的新火1 天前
Blender:法线图&黑白图
游戏·unity·游戏引擎·blender·笔记分享
HahaGiver6662 天前
从0到1做一个“字母拼词”Unity小游戏(含源码/GIF)- 实现多单词顺序通关进度逻辑
unity·游戏引擎·游戏程序
二川bro2 天前
第38节:WebGL 2.0与Three.js新特性
开发语言·javascript·webgl
Dr.勿忘2 天前
Unity一分钟思路---UI任务条:宝箱位置如何准确卡在百分比位置上
ui·unity·游戏程序·屏幕适配
weixin_424294672 天前
Unity 实现 ScrollBar 值变化控制 Panel 位置的方法
unity·游戏引擎