ulua热更新小demo
发布日期:2021-06-30 19:38:47
浏览次数:2
分类:技术文章
本文共 6556 字,大约阅读时间需要 21 分钟。
最近抽空玩了下ulua,挺好玩的,写了个热更新的小demo,使用的unity版本是5.3.1f,ulua版本版本是ulua_v1.24 ,服务器是用c#写的,非常简单就是接受客户端的连接然后把lua脚本传给客户端,客户端下载成功后就执行lua脚本
先展示下效果:
服务器工程:
lua代码:
客户端工程:
运行结果:
服务器:
客户端:
分割线,代码实现部分-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
服务端:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net;using System.Net.Sockets;using System.IO;namespace luaServer{ class Program { static void Main(string[] args) { IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.66.197.107"), 7000); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(ipep); server.Listen(10); while(true) { Socket client = server.Accept(); Console.WriteLine("Accept Client Ok"); string path = "E:\\UnityPrograme\\luaServer\\luaServer\\luaServer\\TuaTest.lua.txt"; FileInfo EzoneFile = new FileInfo(path); FileStream EzoneStream = EzoneFile.OpenRead(); int PacketSize = 100000; int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize)); int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount))); byte[] data = new byte[PacketSize]; for (int i = 0; i < PacketCount; i++) { EzoneStream.Read(data, 0, data.Length); TransferFiles.SendVarData(client, data); } if (LastDataPacket != 0) { data = new byte[LastDataPacket]; EzoneStream.Read(data, 0, data.Length); TransferFiles.SendVarData(client, data); } //client.Close(); Console.WriteLine("Send File Ok"); EzoneStream.Close(); } } }}class TransferFiles{ public TransferFiles() { } public static int SendVarData(Socket s, byte[] data) // return integer indicate how many data sent. { int total = 0; int size = data.Length; int dataleft = size; int sent; byte[] datasize = new byte[4]; datasize = BitConverter.GetBytes(size); sent = s.Send(datasize);//send the size of data array. while (total < size) { sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; } return total; } public static byte[] ReceiveVarData(Socket s) // return array that store the received data. { int total = 0; int recv; byte[] datasize = new byte[4]; recv = s.Receive(datasize, 0, 4, SocketFlags.None);//receive the size of data array for initialize a array. int size = BitConverter.ToInt32(datasize, 0); int dataleft = size; byte[] data = new byte[size]; while (total < size) { recv = s.Receive(data, total, dataleft, SocketFlags.None); if (recv == 0) { data = null; break; } total += recv; dataleft -= recv; } return data; }}客户端:
using UnityEngine;using System.Net;using System.Net.Sockets;using System.IO;using System;using System.Threading;public class FileRecver { public static void StartLoadLua(System.Action cb) { IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.66.197.107"), 7000); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(ipep); #if UNITY_EDITOR string fileaddr = Application.dataPath + "/luaTest.lua.txt";#else string fileaddr = Application.persistentDataPath + "/luaTest.lua.txt";#endif FileStream MyFileStream = new FileStream(fileaddr, FileMode.Create, FileAccess.Write); byte[] data = TransferFiles.ReceiveVarData(client); if (data.Length == 0) { Debug.LogError("data.Length == 0"); } else { MyFileStream.Write(data, 0, data.Length); } MyFileStream.Close(); client.Close(); if (null != cb) cb(); }}class TransferFiles{ public TransferFiles() { } public static int SendVarData(Socket s, byte[] data) // return integer indicate how many data sent. { int total = 0; int size = data.Length; int dataleft = size; int sent; byte[] datasize = new byte[4]; datasize = BitConverter.GetBytes(size); sent = s.Send(datasize);//send the size of data array. while (total < size) { sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; } return total; } public static byte[] ReceiveVarData(Socket s) // return array that store the received data. { int total = 0; int recv; byte[] datasize = new byte[4]; recv = s.Receive(datasize, 0, 4, SocketFlags.None);//receive the size of data array for initialize a array. int size = BitConverter.ToInt32(datasize, 0); int dataleft = size; byte[] data = new byte[size]; while (total < size) { recv = s.Receive(data, total, dataleft, SocketFlags.None); if (recv == 0) { data = null; break; } total += recv; dataleft -= recv; } return data; }}调用:
using UnityEngine;using System.Collections;using LuaInterface;using System;using System.Reflection;public class StartUpEntry : MonoBehaviour{ private static LuaScriptMgr lsmgr = new LuaScriptMgr(); void Awake() { Debug.Log("StartUpEntry Awake"); lsmgr.Start(); } // Use this for initialization void Start () { FileRecver.StartLoadLua(delegate () { Debug.Log("load lua ok"); StartCoroutine(startLoadLuaFromLocal()); }); } private IEnumerator startLoadLuaFromLocal() { WWW www = new WWW("file://" + Application.dataPath + "/luaTest.lua.txt"); yield return www; if(www.error == null) { lsmgr.DoString(www.text); } }}
转载地址:https://linxinfa.blog.csdn.net/article/details/51920802 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月26日 10时46分55秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
leetcode121 买卖股票的最佳时机
2019-04-30
leetcode 122 买卖股票的最佳时机II
2019-04-30
leetcode 309 最佳买卖股票含冷冻期
2019-04-30
leetcode 714 买卖股票的最佳时机含手续费
2019-04-30
leetcode3 无重复字符的最长子串
2019-04-30
leetcode 76 最小覆盖子串
2019-04-30
leetcode 1143. 最长公共子序列
2019-04-30
leetcode 83. 删除排序链表中的重复元素
2019-04-30
智能体 Intelligent Agent
2019-04-30
Network Compression网络压缩(一)
2019-04-30
GAN系列(零)—— GAN的发展(两条路线)
2019-04-30
Conditional GAN (CGAN) 条件生成网络
2019-04-30
强化学习(三) —— Policy Gradient 策略梯度
2019-04-30
docker安装oracle(win10)
2019-04-30
Cloudera Quickstart & HUE
2019-04-30
HUE
2019-04-30
CDH
2019-04-30
行为树 BT
2019-04-30
Cassandra & CQL
2019-04-30
Oracle数据库
2019-04-30