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]));
        }
    }
相关推荐
星河耀银海4 小时前
Unity基础:UI组件详解:Toggle开关的状态控制
ui·unity·lucene
mxwin4 小时前
Unity URP 法线贴图:世界空间 vs 切线空间 深度解析两种法线贴图格式在实时渲染中的核心差异、适用场景与性能权衡
unity·游戏引擎·贴图
℡枫叶℡1 天前
Unity - 全局配置Unity工程的资源检索的目录
unity·资源检索配置
mxwin1 天前
Unity URP 下 TBN 矩阵学习 切线空间、tangent.w 与镜像 UV 的那些坑
学习·unity·矩阵·shader
程序猿多布1 天前
Unity导表工具解决方案-Luban使用教程
unity·luban
mxwin1 天前
Unity URP Shader 混合模式完全指南
unity·游戏引擎
mxwin1 天前
Unity URP 下 HDR 与 Tonemapping 的 Shader 意识
unity·游戏引擎
沉默金鱼1 天前
U3D高级编程:主程手记——第二章2.1读书笔记
unity·游戏引擎
mxwin2 天前
Unity Shader 深度写入与关闭ZWrite Off · 半透明排序 · 粒子穿插
unity·游戏引擎·shader
张老师带你学2 天前
宇宙飞船完整Unity项目
科技·游戏·unity·游戏引擎·模型