Unity开发——获取服务器xLua文件,并保存到本地使用

一、将xlua文件放到服务器上

1、服务器

本文使用小皮面板(PhpStudy),快捷搭建本地服务器环境进行测试使用。

官方地址:https://www.xp.cn,本地服务器根目录地址为:http://localhost

2、服务器地址、本地路径

对应lua文件地址为:http://localhost/lua文件名,或http://localhost/各个层级文件夹名/文件名

本地存放读取lua文件的地址为:C:\Test\LuaFiles;

3、代码
cs 复制代码
//使用@,避免字符转义麻烦,注意路径地址中 /与\的使用

private string localhost = @"http://localhost/LuaFiles/FixhotUpdates.lua.txt";

private string LocalFile = @"C:\Test\LuaFiles\";

二、异步协程获取服务器lua文件并把内容复制到本地文件中

1、获取服务器数据

使用UnityWebRequest.Get(url);

cs 复制代码
UnityWebRequest request = UnityWebRequest.Get(url);
2、将数据写入本地文件

使用System.IO里的File系统;

cs 复制代码
File.WriteAllText(localPath, strData);
3、从服务器获取数据说明
(1)打开服务器

获取服务器数据时,记得把服务器打开。。。,真的会忘记导致报错或无法正常执行;

(2)向服务器发送请求

UnityWebRequest.SendWebRequest();将HTTP请求发送到远程服务器并处理服务器的响应;

(3)UnityWebRequest的方法

获得服务器响应后,可使用UnityWebRequest里的方法,进一步执行判断数据获取是否成功,处理返回的错误信息、使用服务器传回的数据等操作。

1)判断是否下载错误 :

cs 复制代码
if (request.isHttpError || request.isNetworkError)
    Debug.Log(request.error); 

isHttpError:HTTP是否出现错误; isNetworkError:系统是否出现错误; error:错误信息

**2)判断从服务器获取数据的进度:**float progress = request.downloadProgress

**3)以获取下载的数据:**string strData = request.downloadHandler.text;

三、特别说明:

1、普通数据获取与AssetBundle资源获取方式不同:
(1)获取文件数据,获取数据流即可
cs 复制代码
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
string strData = request.downloadHandler.text;
(2)获取ab资源,要转为ab格式

使用UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);

若使用UnityWebRequest request = UnityWebRequest.Get(url);后面会出现类型转化问题。

关于ab资源生成、获取等具体操作说明,可看本人的另一篇关于AssetBundles的博文;

cs 复制代码
//正确用法:
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);

yield return request.SendWebRequest();

AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

GameObject obj = ab.LoadAsset<GameObject>(abName);


//错误用法:
UnityWebRequest request = UnityWebRequest.Get(url);

yield return request.SendWebRequest();

//报转化格式无效的错误
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

//转化结果,ab = null
AssetBundle ab = (request.DownloadHandler as DownloadHandler).assetbundle;
2、异步请求时,请求操作两种等待方式说明

1)yield return request;等待操作完成,在执行后续代码;

2)yield return request.IsDone;等待请求成功,但是不确定是否操作完成,继续执行后续代码;

3)常用yield return request;等待请求操作

三、功能代码

cs 复制代码
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using System.IO;

public class LoadGame : MonoBehaviour
{
    private string localhost = @"http://localhost/LuaFiles/";
    private string LocalFile = @"C:\Temp\LuaFiles\";

    void Start()
    {
        LoadGameMethod();
    }

    /// <summary>
    /// 加载服务器上lua脚本并保存本地,
    /// 之后场景读取本地保存的lua文件
    /// </summary>
    public void LoadGameMethod()
    {
        StartCoroutine(LoadLuaResource("FishUpdates.lua.txt", localhost, LocalFile));
        StartCoroutine(LoadLuaResource("FishDispose.lua.txt", localhost, LocalFile));
    }

    IEnumerator LoadLuaResource(string fileName, string severPath, string localPath)
    {
        //注意要把服务器打开
        UnityWebRequest request = UnityWebRequest.Get(severPath + fileName);
        yield return request.SendWebRequest();

        yield return request.isDone;
        string str = request.downloadHandler.text;

        File.WriteAllText(localPath + fileName, str);
    }
}
相关推荐
茶杯梦轩1 小时前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
晨星shine17 小时前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530141 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526741 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526741 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang2 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
YuMiao2 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
Sinclair5 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Scout-leaf5 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530145 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net