菜鸟学习 - Unity中的热更新 - uLua官方 案例

1、HelloWorld

using LuaInterface;
public class HelloWorld : MonoBehaviour {
 void Start () {
        LuaState l = new LuaState();
  // 在C#下使用Lua
        l.DoString("print('hello world 世界')");
 }
}

2、CreateGameObject

using LuaInterface;
public class CreateGameObject : MonoBehaviour {
	//Lua脚本  Lua下使用c#
    private string script = @"
            luanet.load_assembly('UnityEngine')   //首先需要加载一个assembly包含指定类型
            GameObject = luanet.import_type('UnityEngine.GameObject')//来获得指定类型引用
			//使用引用
            local newGameObj = GameObject('NewObj')
            newGameObj:AddComponent('ParticleSystem')
        ";
	// 在C#下使用Lua
	void Start () {
        LuaState l = new LuaState();
        l.DoString(script);     //执行Lua脚本
	}
}

3、AccessingLuaVariables

using LuaInterface;
public class AccessingLuaVariables : MonoBehaviour {
    private string script = @"
		//Lua脚本  Lua下使用c#
            luanet.load_assembly('UnityEngine')			//首先需要加载一个assembly包含指定类型
            GameObject = luanet.import_type('UnityEngine.GameObject')  //来获得指定类型引用
            particles = {}
            for i = 1, Objs2Spawn, 1 do
                local newGameObj = GameObject('NewObj' .. tostring(i))
                local ps = newGameObj:AddComponent('ParticleSystem')
                ps:Stop()
                table.insert(particles, ps)
            end
            var2read = 42
        ";
	// 在C#下使用Lua
	void Start () {
        LuaState l = new LuaState();
        // Assign to global scope variables as if they're keys in a dictionary (they are really)
        l["Objs2Spawn"] = 5;
        l.DoString(script);
        // 从全局范围读取
        print("Read from lua: " + l["var2read"].ToString());
        // 得到lua table作为LuaTable对象
        LuaTable particles = (LuaTable)l["particles"];
        // 典型遍历 table中的值
        foreach( ParticleSystem ps in particles.Values )
        {
            ps.Play();
        }
	}
}

4、ScriptsFromFile

Lua的脚本文件04_ScriptsFromFile.lua:

--Lua脚本  Lua下使用c#
print("This is a script from a file 世界")

C#文件:

using LuaInterface;
public class ScriptsFromFile : MonoBehaviour {
    public TextAsset scriptFile;
	void Start () {
        LuaState l = new LuaState();
		//C# 运行Lua脚本
		l.DoString(scriptFile.text);
	}
}

5、CallLuaFunction

using LuaInterface;
public class CallLuaFunction : MonoBehaviour {
	//Lua脚本  Lua下使用c#
    private string script = @"
            function luaFunc(message)
                print(message)
                return 42
            end
        ";	
	void Start () {
        LuaState l = new LuaState();
        //C# 运行Lua脚本
        l.DoString(script);
        // 得到函数对象
        LuaFunction f = l.GetFunction("luaFunc");
		// 调用它
        object[] r = f.Call("I called a lua function!");
		// Lua 函数能够有 variable 变量返回值,因此我们将他们存储那些作为 C# 对象数组,和在这个案例的打印第一个元素 42
        print(r[0]);
	}
}

6、LuaCoroutines

using LuaInterface;
public class LuaCoroutines : MonoBehaviour {
	// 这个例子将打印 斐波那契 数列(当前数=前两个数的和) 1~10位,每次迭代是1秒间隔
	//Lua脚本  Lua下使用c#
    private string script = @"
            luanet.load_assembly('UnityEngine')   --首先需要加载一个assembly包含指定类型
            local Time = luanet.import_type('UnityEngine.Time')   --来获得指定类型引用
            -- 这yields在每一帧中,全局的游戏时间是仍然低于所需的戳的timestamp
            function waitSeconds(t)
                local timeStamp = Time.time + t
                while Time.time < timeStamp do
                    coroutine.yield()
                end
            end
            function fib(n)
                local a, b = 0, 1
                while n > 0 do
                    a, b = b, a + b
                    n = n - 1
                end

                return a
            end
            function myFunc()
                print('Coroutine started')
                local i = 0
                for i = 0, 10, 1 do
                    print(fib(i))
                    waitSeconds(1)
                end
                print('Coroutine ended')
            end
        ";
    private LuaThread co = null;     //Lua线程
	void Start () {
        LuaState l = new LuaState();
		// //C# 运行Lua脚本
        l.DoString(script);
        // 获得 函数对象
        LuaFunction f = l.GetFunction("myFunc");
		// 创建一个暂停的 lua coroutine协同对象从父状态和函数
        co = new LuaThread(l, f);
		//协程需要在每个帧恢复,因为它在每个帧yield,或者您可以只恢复它在 C# 中的条件相反
        co.Start();
	}
	void Update () {
        if( !co.IsDead() )
        {
            co.Resume();   //恢复
        }
        else
        {
            print("Coroutine has exited.");
			//为了摧毁协同 (但不是在 lua 的函数中,只是协同 stack 实例) 只允许 C# 来清理被包装的对象
            co = null;
        }
	}
}


 

 

 

 

你可能感兴趣的:(菜鸟学习 - Unity中的热更新 - uLua官方 案例)