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]));
        }
    }
相关推荐
星夜泊客3 小时前
Unity 游戏开发中的防御性编程与空值处理实践
unity·设计模式·游戏引擎
tealcwu9 小时前
【Unity踩坑】Unity测试用例命名空间错误解决方案
unity·游戏引擎·测试用例
爱看书的小沐11 小时前
【小沐杂货铺】基于Three.js绘制三维管道Pipe(WebGL、vue、react)
javascript·vue.js·webgl·three.js·管道·pipe·三维管道
地狱为王13 小时前
Unity使用PP-MattingV2实现人像分割
unity·游戏引擎
在路上看风景15 小时前
7.1 阴影贴图
unity
lrh302515 小时前
Unity 高效 ListView GridView
unity·高效·复用·uilistview·uigridview
星夜泊客18 小时前
[特殊字符] Unity 对象判空机制混乱原因总结(上篇)
unity·游戏引擎
神码编程2 天前
【Unity】HTModuleManager(三)Markdown语法的Unity编辑器方言
unity·markdown·模块管理器
井队Tell2 天前
打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第十二天)
学习·3d·unity
梦凡尘2 天前
webgl 变换矩阵:旋转、平移、缩放
webgl