tolua調用C#中的靜態類


一、創建unity中引用lua虛擬機接口的庫

using UnityEngine;
using System.Collections;
using LuaInterface;
using WCALibs;

public class LUA_Test : MonoBehaviour
{
//聲明lua對象,以及lua函數對象
    private LuaState lua = null;
    private LuaFunction luaFunc = null;
    void Start()
    {
        lua = new LuaState();
        lua.Start();
        string fullPath = Application.dataPath + "\\Lua";
        lua.AddSearchPath(fullPath);
        LuaBinder.Bind(lua);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            lua.DoFile("Funclua.lua");
            luaFunc = lua.GetFunction("testFunc");
            if (luaFunc != null)
            {
                luaFunc.Call();
            }
        }
    }
    void OnApplicationQuit()
    {
        lua.Dispose();
        lua = null;
    }
}

2//創建靜態函數

using System;
using UnityEngine;
using System.Collections.Generic;

public class LuaSDK
{
    public static void Test()
    {
        Debug.Log("測試靜態方法");
    }

    public static void Test1()
    {
        Debug.Log("測試靜態方法1"); 
    }

    public static void Test2()
    {
        Debug.Log("測試靜態方法2");
    }
}

3、寫lua腳本

--創建lua腳本Funclua.lua
function testFunc()
     print("Hello World")
     LuaSDK.test()
end

切記每次寫完新的代碼,tolua都需要生成代碼,在編輯器模式下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM