您的位置:首页 > 编程语言 > Lua

LuaFramework_UGUI_学习笔记 <1>

2018-01-22 14:26 302 查看
游戏的热更新一直是比较头疼的地方。unity3D自带 Assetbundle 只能做到 资源的热更新。虽然C#可以用反射做到热更新 代码。但是在IOS 品台是被 禁止的!

所以 Lua就来了。Lua 设计之初 就是放于 宿主 被执行的。 其基础支持 当需要自己学习。

    下载最新的
LuaFramework_UGUI 工程

    这里先讲解两个小例子。给以后的内容做一下铺垫!

    在Unity3D里面 C#和Lua的相互调用


首先在这里新建一个 文件夹 Mine;

在这里申明下: 在之前的版本里面 Lua 和 C# 相互调用可以这么写:

这个是C#脚本

using UnityEngine;
using LuaInterface;

public class CSharpLuaTest : MonoBehaviour {

private LuaState luaState = new LuaState();
public TextAsset textAsset;

// Use this for initialization
void Start ()
{
this.luaState.RegisterFunction("CSharpMethod", this, this.GetType().GetMethod("CSharpMethod"));

this.luaState.DoString(textAsset.text);

object[] objs = luaState.GetFunction("LuaMethod").Call(999);

Debug.Log( string.Format("Lua Resoult: {0}, :{1}", objs[0], objs[1] ));

}

public string CSharpMethod(int num)
{
return string.Format("Hello World {0}", num + 1);
}
}

Lua脚本这么写
print("LuaText StartUp!");

function LuaMethod(i)
local s = CSharpMethod(i);
return i,s;
end

这样就可以实现 相互调用了。 但是在新班的 LuaFramework_UGUI 中这么写 会提示
"luaState.RegisterFunction" 找不到这个方法 。 后来对比代码发现 LuaState 没有这个方法了!

我们就用新版的调用方法吧; 我们先定义一个自己的类型,将他继承 LuaClient

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
using LuaFramework;
using System;

namespace Mine
{
public class TestLua : LuaClient
{

public TextAsset textAsset;

// Use this for initialization
void Start()
{

}

protected override LuaFileUtils InitLoader()
{
return new LuaFileUtils();
}
protected override void CallMain()
{
LuaFunction func = luaState.GetFunction("CallFromCSharp");
string resoult = func.Invoke<string, string>("CSharp Is Good");
Debug.Log("====>:" + resoult);
}

protected override void StartMain()
{
luaState.DoString(textAsset.text);
CallMain();
}

public static void CallFromLua(string content)
{
Debug.Log("这是来自 Lua 脚本的:" + content);
}

public string CallNoStatic()
{
return "Hello Lua This From CSharp";
}
}
}

Lua代码 这么写

function CallFromCSharp( str )

UpdateBeat:Add(Update,self)

local  s = "Call form CSharp ===============>"..str;
print(s);
DoCallCSharp();
return s;
end

function DoCallCSharp()
local  s = "Call form Lua ===============>";
print(s);

--Mine.TestLua:CallNoStatic();

LuaFramework.Util.Log("===================>LuaFramework.Util.Log");

Mine.TestLua.CallFromLua(s);

--Mine.TestLua.CallNoStatic();

end

function Update()
LuaFramework.Util.Log("UpdateBeat");
end

 

这样简单的 相互调用就算完成了

然后在 CustomSettings.cs 里面注册给 Lua 调用



重新生成下



这样 就能自动 完成 lua 注册了

但是我发现 吊用不了 非 静态 函数 不知道是为什么..还是哪里写的不对

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: