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]));
        }
    }
相关推荐
GISer_Jing6 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
元气少女小圆丶10 小时前
SenseGlove Nova 2+Unity开发笔记1
笔记·学习·unity
mxwin10 小时前
Unity URP下新技术MSSPT 取代SSR和光线追踪
unity·游戏引擎·shader
LF男男14 小时前
IBuilder.cs 接口
unity
心之所向,自强不息14 小时前
# Unity MCP + Codex CLI 完整教程(Windows)
windows·unity·游戏引擎
小拉达不是臭老鼠21 小时前
Unity数据持久化_Json
学习·unity
heimeiyingwang21 小时前
【架构实战】订单系统架构设计:电商核心系统的演进
unity·架构·系统架构
元气少女小圆丶21 小时前
SenseGlove Nova 2+Unity开发笔记3
笔记·unity·游戏引擎
WMX10121 天前
Unity-shader学习记录
学习·unity·游戏引擎
WMX10121 天前
Hololens 2 上部署 Unity+MRTK 项目_模型着色
unity·游戏引擎·hololens