private string luaString = @" function testFunc(num) return num*2 end t = {} t.testFunc = testFunc "; private LuaFunction luaFunc; void Start () { LuaState lua = new LuaState(); lua.Start (); lua.DoString (luaString); luaFunc = lua.GetFunction ("testFunc"); DelegateFactory.Init ();//委托工厂初始化 Func<int,int> funcDel = luaFunc.ToDelegate<Func<int,int>> (); int s3 = funcDel(8888888); Debug.Log (s3); //释放资源 }
lua方法转成c#委托,要先调用DelegateFactory.Init()方法初始化,没有调用时会报错
使用LuaFunction的ToDelegate方法转换,泛型参数表示要转换的委托类型
这里委托类型用的是内置的Func有返回值,lua代码中的参数返回的是数值类型,我说了可以用string来接,但是这里不行,就算是把lua方法的返回值改成string类型,同样不行,就是说不能用Func<...,string>接收委托,不明所以
private string luaString = @" function testFunc(num) return num*2 end t = {} t.testFunc = testFunc "; private LuaFunction luaFunc; void Start () { LuaState lua = new LuaState(); lua.Start (); lua.DoString (luaString); luaFunc = lua.GetFunction ("testFunc"); luaFunc.BeginPCall (); luaFunc.Push (2000);//参数的推入 luaFunc.PCall ();//调用 int s4 = (int)luaFunc.CheckNumber ();//获取返回值 luaFunc.EndPCall ();//结束释放资源 Debug.Log (s4); //清理 }
据说这种调用方法是不会产生内存垃圾的,但是不方便使用
private string luaString = @" function testFunc(num) return num*2 end t = {} t.testFunc = testFunc "; private LuaFunction luaFunc; void Start () { LuaState lua = new LuaState(); lua.Start (); lua.DoString (luaString); //luaFunc = lua.GetFunction ("testFunc"); //string s2 = lua.Invoke<int,string> ("t.testFunc", 666, true);一样的 string s2 = lua.Invoke<int,string>("testFunc",666,true); Debug.Log (s2); }
这里不需要获取到LuaFunction,直接用lua的Invoke方法,同样的泛型参数一是参数类型,参数二是返回类型。
参数一是lua的方法名,参数..,最后一个参数是不知道
private string luaString = @" function testFunc(num) return num*2 end t = {} t.testFunc = testFunc "; private LuaFunction luaFunc; void Start () { LuaState lua = new LuaState(); lua.Start (); lua.DoString (luaString); luaFunc = lua.GetFunction ("testFunc"); //luaFunc = lua.GetFunction ("t.testFunc");一样的 string s = luaFunc.Invoke<int,string> (33648); Debug.Log (s); //LuaFunc和LuaState的释放 }
LuaFunc的Invok方法,泛型的第一个参数表示传入参数的类型,第二个参数表示返回的参数类型
这里lua方法是返回的是数值类型的,我这里用string类型来接受是可以的
using LuaInterface; public class LuaFile : MonoBehaviour { private string path; LuaState lua; void Start () { this.path = Application.dataPath + "/Lua"; lua = new LuaState (); lua.Start (); lua.AddSearchPath (this.path); //添加搜索路径 } void Update () { if (Input.GetKey (KeyCode.D)) { lua.DoFile ("a.lua");//可以重复调用,完整文件名 } if (Input.GetKey (KeyCode.R)) { lua.Require ("a");//只加载文件一次,再次触发文件Lua代码也不执行 } } void Destroy(){ lua.CheckTop (); lua.Dispose (); } }
通过AddSearchPath添加寻找lua文件的路径,后面调用直接输入文件名(后缀),ToLua会在添加了的搜索路径下查找这些文件再执行。
这个路径可以添加多个,ToLua按添加的顺序去搜索文件,找到之后就不在往下搜索了
只有添加了路径下的lua文件,才可以被加载
using LuaInterface; public class HelloLua : MonoBehaviour { private string script = @" print('hello toLua');--这个是lua代码字符串 "; // Use this for initialization void Start () { LuaState lua = new LuaState ();//创建lua虚拟机,主要用于和C#交互 lua.Start (); //启动,初始化 lua.DoString (script); //调用字符串形式的lua代码 lua.CheckTop (); //清空lua虚拟机栈 lua.Dispose (); //析构lua lua = null; //赋空,让GC回收 } }
LuaState类在LuaInterface命名空间中
官方下载Tolua的源文件,将Assets目录下的文件拉进自己项目中,如果是Unity5.x的,再将Unity5.x下面的Plugins文件夹拉进项目中,替换覆盖原来的文件。再点击Lua/Generate All注册c#方法到lua中等,这样示例就可以跑起来了,当然,你也可以开发了。
using UnityEngine.Networking; IEnumerator LoadForWebRequest(){ string fileUrl = "file://D:/Documents/Study/Assets/14/AssetBundle/ppp.assetbundle"; using (UnityWebRequest request = UnityWebRequest.GetAssetBundle (fileUrl, 0)) { yield return request.SendWebRequest(); //发送等待回应 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent (request); Object player = bundle.LoadAsset ("Player"); Instantiate (player); bundle.Unload (false); } }
据说WWW要被抛弃了,没错就是UnityWebRequest替代了
使用UnityWebRequest.GetAssetBundle()发起加载AssetBundle请求
通过DownloadHandlerAssetBundle.GetContent()获取到AssetBundle资源
IEnumerator LoadForWWW(){ string fileUrl = "file://D:/Documents/Study/Assets/14/AssetBundle/ppp.assetbundle"; using(WWW www = new WWW(fileUrl)){ //www资源用完自动释放 yield return www; //等待加载完成 AssetBundle bundle = www.assetBundle; //通过assetBundle属性获取 Object player = bundle.LoadAsset ("Player"); Instantiate (player); bundle.Unload (false); } }
使用www来加载资源,如果加载本地的需要加上file:://协议表示本地
所有需要加载服务器上的资源时,用http://,https://等
void LoadForAssetBundle(){ string filePath = "D:/Documents/Study/Assets/14/AssetBundle/ppp.assetbundle"; AssetBundle bundle = AssetBundle.LoadFromFile (filePath); Object player = bundle.LoadAsset ("Player"); Instantiate (player); bundle.Unload (false); }
不需要等待,直接使用AssetBundle的静态方法LoadFromFile直接得到AssetBundle
通过LoadAsset("资源名字"),加载AssetBundle里面的资源
Unload()方法传递一个bool值,true表示释放全部,false表示释放没有用到的