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]));
        }
    }
相关推荐
不伤欣6 小时前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
快乐觉主吖12 小时前
Unity的日志管理类
android·unity·游戏引擎
WarPigs19 小时前
Unity性能优化笔记
笔记·unity·游戏引擎
魂断蓝桥66621 小时前
如何基于three.js(webgl)引擎架构,实现3D密集架库房,3D档案室智能巡检
webgl·threejs·3d建筑·3d档案室·3d定位、三维室内定位、3d建筑·3d库房·3d密集架
T.D.C1 天前
【业务框架】3C-相机-Cinemachine
unity
Clank的游戏栈2 天前
Unity基于GraphView的可视化关卡编辑器开发指南
unity·编辑器·游戏引擎
海尔辛2 天前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化
ak啊2 天前
WebGL魔法:从立方体到逼真阴影的奇妙之旅
前端·webgl
XR-AI-JK2 天前
Unity VR/MR开发-VR设备与适用场景分析
unity·vr·mr
ChiLi_Lin3 天前
Unity异常上报飞书工具
unity·游戏引擎·飞书