Unity热更新(七)SimpleFramework

发表于2017-10-25
评论0 1.8k浏览


我们在上一篇中已经导入了SimpleFramework_UGUI-master,我们首先看官方bbs上的一点介绍。


/**********************************************************************************************************************************/

拿到框架与插件,有几个点需要知道:
(0)SimpleFramework NGUI/UGUI各版本框架都已经自带集成了最新版的ulua,用户无需导入ulua插件,否则就是多此一举,出现错误。

(1)ulua插件最新版1.2.1,没有集成框架的自动wrap功能,也就是你添加了一个自定义的c#,生成wrap以后,需要手动的在lua/system/wrap.lua里面添加一行,再运行,下个版本会集成自动wrap功能。

(2)拿到框架,有两个步骤一个是生成wrap,另一个是build资源,一前一后,执行顺序不能颠倒,因为生成wrap的时候会修改项目里的wrap.lua,然后build资源的时候把它一起打包出去,才能生效。

(3)视频,ULUA提供的视频,没有提供声音,因为我没麦,它使用的版本是Unity4.6.2跟ulua_v1.16,如果还没搞懂前,必须严格照用同版本,直接用后面版本可能会有报错。

(4)关于Luastate跟LuaScriptMgr读取的lua文件路径问题:如果你用Luastate l = new Luastate(); l.DoFile("绝对路径"),如果用LuaScriptMgr mgr; mgr.DoFile("相对路径"),至于相对于谁?在Util类的LuaPath函数内设定。

(5)关于使用32位版的用户,把Plugins/x86.zip解压出来,因为Unity5不允许有两个相同的ulua.dll存在,所以提供压缩包,用户自解压。

(6)关于框架、插件的部分例子直接运行报错的原因,如果例子中涉及到LuaScriptMgr的类,它是wrap去反射模式,也就是说,需要把自带的lua目录下面的脚本解压到指定目录,才能争取读取,那正确的姿势是:(1)打开login测试场景,运行成功后,再运行例子,原因是login场景会把自己包内的lua脚本解压到指定的数据目录,Windows默认是c://simpleframework下面,而例子里面没有任何释放lua脚本资源的代码,因此在LuaScripgMgr.Start()的时候,找不到lua类。(2)或者,在AppConst.cs里面将DebugMode设置位true,让它自己读取工程目录下的lua类。

(7)ulua_v1.03 (支持Unity 4.6.1及其老版本) 
        SimpleFramework NGUI/UGUI 和
        ulua_v1.03_with_iOS64_x86到最新版 (支持Unity 4.6.2以上版本)

(8)ulua/SimpleFramework NGUI/UGUI版本跟用户自编译的底层库有个很重要的区别,现在我们用的底层库在MAC/iOS平台因为用的是lua原始代码,为了目前应对苹果的arm64的问题,自带的底层库是统一字节码的(用自带的luac编码一次lua文件即可,加载时也不需要区分32bit/64bit),而且优化了luac编码出来的尺寸大小,甚至还带了自加密功能,不区分平台。但是,用户自编译的底层库需要用户区分32bit/64bit的luac编译出来的字节码,没有优化字节码及其加密功能,或者用户有能力可以自己做这部分。原因我们也不是故意不开源,因为目前盗掘猖獗,我们为了保护大家的知识劳动成功别被第三次无耻偷取,所以临时闭源,择时公开。所以是选择自己编译底层库还是直接选用我们自带的功能,需要提前考虑清楚。

(9)如果你在Bindlua.cs里面更改、删除了某些不需要注册进lua的类,在Clear完了,ulua会编译一些引用脚本,一定要等编译的菊花转完,再重新生成,否则会有更改无效的情况出现,切记等菊花转完。 

(链接:http://blog.ulua.org/article/faq/uluasimpleframeworkshiyongqianxuzhi.html)

/**********************************************************************************************************************************/

挑几段好看的代码看看:

1、HelloWorld

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using LuaInterface;  
  4.   
  5. public class HelloWorld : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.         LuaState l = new LuaState();  
  10.         string str = "print('hello world 世界')";  
  11.         l.DoString(str);  
  12.     }  
  13.       
  14.     // Update is called once per frame  
  15.     void Update () {  
  16.       
  17.     }  
  18. }  

2、CreateGameObject

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using LuaInterface;  
  4.   
  5. public class CreateGameObject01 : MonoBehaviour {  
  6.   
  7.     private string script = @"  
  8.             luanet.load_assembly('UnityEngine')  
  9.             GameObject = luanet.import_type('UnityEngine.GameObject')          
  10.         ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')              
  11.             local newGameObj = GameObject('NewObj')  
  12.             newGameObj:AddComponent(luanet.ctype(ParticleSystem))  
  13.         ";  
  14.   
  15.     //反射调用  
  16.     void Start () {  
  17.         LuaState lua = new LuaState();  
  18.         lua.DoString(script);  
  19.     }  
  20.       
  21.     // Update is called once per frame  
  22.     void Update () {  
  23.       
  24.     }  
  25. }  
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using LuaInterface;  
  4.   
  5. public class CreateGameObject02 : MonoBehaviour {  
  6.   
  7.     private string script = @"  
  8.             luanet.load_assembly('UnityEngine')  
  9.             GameObject = UnityEngine.GameObject  
  10.             ParticleSystem = UnityEngine.ParticleSystem  
  11.             local newGameObj = GameObject('NewObj')  
  12.             newGameObj:AddComponent(ParticleSystem.GetClassType())  
  13.         ";  
  14.   
  15.     //非反射调用  
  16.     void Start () {  
  17.         LuaScriptMgr lua = new LuaScriptMgr();  
  18.         lua.Start();  
  19.         lua.DoString(script);  
  20.     }  
  21.       
  22.     // Update is called once per frame  
  23.     void Update () {  
  24.       
  25.     }  
  26. }  

3、CallLuaFunction
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using LuaInterface;  
  4.   
  5. public class CallLuaFunction_01 : MonoBehaviour {  
  6.   
  7.     private string script = @"  
  8.             function luaFunc(message)  
  9.                 print(message)  
  10.                 return 42  
  11.             end  
  12.         ";  
  13.   
  14.     // Use this for initialization  
  15.     void Start () {  
  16.         LuaState l = new LuaState();  
  17.   
  18.         // First run the script so the function is created  
  19.         l.DoString(script);  
  20.   
  21.         // Get the function object  
  22.         LuaFunction f = l.GetFunction("luaFunc");  
  23.   
  24.         // Call it, takes a variable number of object parameters and attempts to interpet them appropriately  
  25.         object[] r = f.Call("I called a lua function!");  
  26.   
  27.         // Lua functions can have variable returns, so we again store those as a C# object array, and in this case print the first one  
  28.         print(r[0]);  
  29.     }  
  30.       
  31.     // Update is called once per frame  
  32.     void Update () {  
  33.       
  34.     }  
  35. }  

4、LuaClass
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class LuaClass : MonoBehaviour {  
  5.     const string source = @"  
  6.         Account = { balance = 0 };  
  7.           
  8.         function Account:new(o)      
  9.             o = o or {};  
  10.             setmetatable(o, { __index = self });       
  11.             return o;    
  12.         end    
  13.     
  14.         function Account.deposit(self, v)    
  15.             self.balance = self.balance   v;    
  16.         end    
  17.     
  18.         function Account:withdraw(v)    
  19.             if (v) > self.balance then error 'insufficient funds'; end    
  20.             self.balance = self.balance - v;    
  21.         end   
  22.   
  23.         SpecialAccount = Account:new();  
  24.   
  25.         function SpecialAccount:withdraw(v)    
  26.             if v - self.balance >= self:getLimit() then    
  27.                 error 'insufficient funds';    
  28.             end    
  29.             self.balance = self.balance - v;    
  30.         end    
  31.     
  32.         function SpecialAccount.getLimit(self)    
  33.             return self.limit or 0;    
  34.         end    
  35.   
  36.         s = SpecialAccount:new{ limit = 1000 };  
  37.         print(s.balance);    
  38.         s:deposit(100.00);  
  39.   
  40.         print (s.limit);    
  41.         print (s.getLimit(s))    
  42.         print (s.balance)    
  43.     ";  
  44.   
  45.     // Use this for initialization  
  46.     void Start () {  
  47.         LuaScriptMgr mgr = new LuaScriptMgr();  
  48.         mgr.Start();  
  49.         mgr.lua.DoString(source);  
  50.     }  
  51.       
  52.     // Update is called once per frame  
  53.     void Update () {  
  54.       
  55.     }  
  56. }  

具体点Demo可以自己慢慢看。

就不多介绍了。


我们下一节就自己动手来做一个UGUI的实例吧。


如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引