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]));
        }
    }
相关推荐
王维志2 小时前
使用Asp.Net WebApi(.net 8)托管Unity WebGL
unity·游戏引擎·webgl
Zuckjet_7 小时前
第 7 篇:交互的乐趣 - 响应用户输入
前端·javascript·webgl
xhload3d10 小时前
智慧钢厂高炉冶炼仿真分析 | 图扑数字孪生
3d·智慧城市·html5·webgl·数字孪生·可视化·热力图·智慧工厂·工业互联网·工业组态·高炉炼铁·数字工厂·高炉炉体·智慧高炉·高炉
猪哥帅过吴彦祖10 小时前
第 8 篇:更广阔的世界 - 加载 3D 模型
前端·javascript·webgl
lrh302519 小时前
Custom SRP 12 - HDR
3d·unity·srp·render pipeline
霜绛20 小时前
Unity:Json笔记——Json文件格式、JsonUtlity序列化和反序列化
学习·unity·json·游戏引擎
猪哥帅过吴彦祖1 天前
第 7 篇:交互的乐趣 - 响应用户输入
前端·webgl
TYayyyyy1 天前
unity 事件、委托
unity
L X..1 天前
Unity反射调用 ReactiveProperty<T>(泛型类型)内部方法时崩溃
unity·c#·游戏引擎·.net
关山月1 天前
如何使用WebGL创建伪3D图像效果
webgl