Unity3D UniLua的示例

LuaScriptControllerusing System;using System.Collections;using UnityEngine;using UniLua;public class LuaScriptController : MonoBehaviour{public string LuaScriptFile = "framework/main.lua";private ILuaState Lua;private int AwakeRef;private int StartRef;private int UpdateRef;private int LateUpdateRef;private int FixedUpdateRef;void Awake(){Debug.Log("LuaScriptController Awake");if (Lua == null){Lua = LuaAPI.NewState();Lua.L_OpenLibs();var status = Lua.L_DoFile(LuaScriptFile);if (status != ThreadStatus.LUA_OK){throw new Exception(Lua.ToString(-1));}if (!Lua.IsTable(-1)){throw new Exception("framework main’s return value is not a table");}AwakeRef = StoreMethod("awake");StartRef = StoreMethod("start");UpdateRef = StoreMethod("update");LateUpdateRef = StoreMethod("late_update");FixedUpdateRef = StoreMethod("fixed_update");Lua.Pop(1);Debug.Log("Lua Init Done");}CallMethod(AwakeRef);}IEnumerator Start(){CallMethod(StartRef);// — sample code for loading binary Asset Bundles ——————–String s = "file:///" + Application.streamingAssetsPath + "/testx.unity3d";WWW www = new WWW(s);yield return www;if ( != null){TextAsset cc = (TextAsset);var status = Lua.L_LoadBytes(cc.bytes, "test");if (status != ThreadStatus.LUA_OK){throw new Exception(Lua.ToString(-1));}status = Lua.PCall(0, 0, 0);if (status != ThreadStatus.LUA_OK){throw new Exception(Lua.ToString(-1));}Debug.Log("—- call done —-");}}void Update(){CallMethod(UpdateRef);}void LateUpdate(){CallMethod(LateUpdateRef);}void FixedUpdate(){CallMethod(FixedUpdateRef);}private int StoreMethod(string name){Lua.GetField(-1, name);if (!Lua.IsFunction(-1)){throw new Exception(string.Format("method {0} not found!", name));}return Lua.L_Ref(LuaDef.LUA_REGISTRYINDEX);}private void CallMethod(int funcRef){Lua.RawGetI(LuaDef.LUA_REGISTRYINDEX, funcRef);// insert `traceback’ functionvar b = Lua.GetTop();Lua.PushCSharpFunction(Traceback);Lua.Insert(b);var status = Lua.PCall(0, 0, b);if (status != ThreadStatus.LUA_OK){Debug.LogError(Lua.ToString(-1));}// remove `traceback’ functionLua.Remove(b);}private static int Traceback(ILuaState lua){var msg = lua.ToString(1);if (msg != null){lua.L_Traceback(lua, msg, 1);}// is there an error object?else if (!lua.IsNoneOrNil(1)){// try its `tostring’ metamethodif (!lua.L_CallMeta(1, "__tostring")){lua.PushString("(no error message)");}}return 1;}}

main.lua

local InputControl = require "logic.input_control"local SceneMgr= require "logic.scene_mgr"local UnityEngine= require "lib.unity_engine"local GameObject= UnityEngine.GameObjectlocal MeshFilter= UnityEngine.MeshFilterlocal Resources= UnityEngine.Resourceslocal Mesh= UnityEngine.Meshlocal Vector3= UnityEngine.Vector3local MeshRenderer= UnityEngine.MeshRendererlocal Material= UnityEngine.Materiallocal function awake()print("—- awake —-")endlocal function start()print("—- start —-")SceneMgr.init_scene()endlocal function update()InputControl.update_input()endlocal function late_update()endlocal function fixed_update()endreturn {awake= awake,start= start,update= update,late_update= late_update,fixed_update = fixed_update,}input_control.lualocal SceneMgr= require "logic.scene_mgr"local UnityEngine = require "lib.unity_engine"local Input= UnityEngine.Inputlocal x = 0local y = 0local function update_input()– print( "vertical: ", Input.GetAxis("Vertical") )– print( "horizontal: ", Input.GetAxis("Horizontal") )local scene = SceneMgr.get_scene()x = x + Input.GetAxis("Horizontal") * 50y = y + Input.GetAxis("Vertical") * 50scene.hero:move( x, y )endlocal function start()endreturn {update_input = update_input,start = start,}scene_mgr.lualocal Hero = require "sprite.hero"local Scenelocal function create_hero()return Hero.create()endlocal function init_scene()Scene = {}Scene.hero = create_hero()return Sceneendlocal function get_scene()return Sceneendreturn {init_scene = init_scene,get_scene = get_scene,}hero.lualocal UnityEngine= require "lib.unity_engine"local GameObject= UnityEngine.GameObjectlocal MeshFilter= UnityEngine.MeshFilterlocal Resources= UnityEngine.Resourceslocal Mesh= UnityEngine.Meshlocal Vector3= UnityEngine.Vector3local MeshRenderer= UnityEngine.MeshRendererlocal Material= UnityEngine.Materiallocal function create()local Test = GameObject._New("Test")local componentlocal mesh = Resources.Load("Mesh/Quad1x1W1L1VC", Mesh._Type())local material = Resources.Load("Material/Sprite", Material._Type())local unity_obj = GameObject._New("HERO")component = unity_obj:AddComponent(MeshFilter._Type())local mesh_filter = MeshFilter._ConvertFrom(component)print("mesh_filter:", mesh_filter:ToString())print("mesh_filter.mesh:", mesh_filter.mesh)mesh_filter.sharedMesh = Mesh._ConvertFrom(mesh)unity_obj.transform.localScale = Vector3._New(128,128,128)component = unity_obj:AddComponent(MeshRenderer._Type())local mesh_renderer = MeshRenderer._ConvertFrom(component)mesh_renderer.castShadows = falsemesh_renderer.receiveShadows = falsemesh_renderer.material = Material._ConvertFrom(material)local mt = {__index = {move = function(self, x, y)local unity_obj = rawget(self, "__unity_obj")unity_obj.transform.localPosition = Vector3._New(x, y, 0)end},__newindex = function(self, key, value)end,}return setmetatable({__unity_obj = unity_obj,}, mt)endreturn {create = create,}项目地址https://github.com/xebecnan/UniLua

,启程了,人的智慧才得以发挥。

Unity3D UniLua的示例

相关文章:

你感兴趣的文章:

标签云: