// Use this for initialization void Start() { //创建lua虚拟机 luaState = new LuaState(); DelegateFactory.Init(); luaState.Start(); //在lua虚拟机(全局)中注册自定义函数 luaState.BeginModule(null); CSFunctionTestWrap.Register(luaState); luaState.EndModule(); //加载lua脚本 this.luaState.DoFile(Application.streamingAssetsPath + "/MyLua2.lua"); //加载完lua以后,获取lua中的函数,并执行。(此lua脚本回调了C#中的方法) LuaFunction luaFunction = luaState.GetFunction("LuaFunction"); luaFunction.BeginPCall(); luaFunction.Push("张三"); luaFunction.PCall(); luaFunction.EndPCall(); luaFunction = luaState.GetFunction("LuaAdd"); luaFunction.BeginPCall(); luaFunction.Push(100); luaFunction.PCall(); luaFunction.EndPCall(); luaFunction.Dispose(); luaState.Dispose(); luaFunction = null; luaState = null; }
public static void Bind(LuaState L) { float t = Time.realtimeSinceStartup; L.BeginModule(null); CSFunctionTestWrap.Register(L); LuaInterface_DebuggerWrap.Register(L); L.BeginModule("UnityEngine"); UnityEngine_ComponentWrap.Register(L); UnityEngine_TransformWrap.Register(L); UnityEngine_MaterialWrap.Register(L); UnityEngine_LightWrap.Register(L); UnityEngine_CameraWrap.Register(L); UnityEngine_AudioSourceWrap.Register(L); UnityEngine_BehaviourWrap.Register(L); UnityEngine_MonoBehaviourWrap.Register(L); UnityEngine_GameObjectWrap.Register(L); UnityEngine_TrackedReferenceWrap.Register(L); UnityEngine_ApplicationWrap.Register(L); UnityEngine_PhysicsWrap.Register(L); UnityEngine_ColliderWrap.Register(L); UnityEngine_TimeWrap.Register(L); UnityEngine_TextureWrap.Register(L); UnityEngine_Texture2DWrap.Register(L); UnityEngine_ShaderWrap.Register(L); UnityEngine_RendererWrap.Register(L); UnityEngine_WWWWrap.Register(L); UnityEngine_ScreenWrap.Register(L); UnityEngine_CameraClearFlagsWrap.Register(L); UnityEngine_AudioClipWrap.Register(L); UnityEngine_AssetBundleWrap.Register(L); UnityEngine_ParticleSystemWrap.Register(L); UnityEngine_AsyncOperationWrap.Register(L); UnityEngine_LightTypeWrap.Register(L); UnityEngine_SleepTimeoutWrap.Register(L); UnityEngine_AnimatorWrap.Register(L); UnityEngine_InputWrap.Register(L); UnityEngine_KeyCodeWrap.Register(L); UnityEngine_SkinnedMeshRendererWrap.Register(L); UnityEngine_SpaceWrap.Register(L); UnityEngine_AnimationBlendModeWrap.Register(L); UnityEngine_QueueModeWrap.Register(L); UnityEngine_PlayModeWrap.Register(L); UnityEngine_WrapModeWrap.Register(L); UnityEngine_QualitySettingsWrap.Register(L); UnityEngine_RenderSettingsWrap.Register(L); UnityEngine_ResourcesWrap.Register(L); L.BeginModule("Experimental"); L.BeginModule("Director"); UnityEngine_Experimental_Director_DirectorPlayerWrap.Register(L); L.EndModule(); L.EndModule(); L.BeginModule("Events"); L.RegFunction("UnityAction", UnityEngine_Events_UnityAction); L.EndModule(); L.BeginModule("Camera"); L.RegFunction("CameraCallback", UnityEngine_Camera_CameraCallback); L.EndModule(); L.BeginModule("Application"); L.RegFunction("AdvertisingIdentifierCallback", UnityEngine_Application_AdvertisingIdentifierCallback); L.RegFunction("LogCallback", UnityEngine_Application_LogCallback); L.EndModule(); L.BeginModule("AudioClip"); L.RegFunction("PCMReaderCallback", UnityEngine_AudioClip_PCMReaderCallback); L.RegFunction("PCMSetPositionCallback", UnityEngine_AudioClip_PCMSetPositionCallback); L.EndModule(); L.EndModule(); L.BeginModule("System"); L.RegFunction("Action", System_Action); L.RegFunction("Predicate_int", System_Predicate_int); L.RegFunction("Action_int", System_Action_int); L.RegFunction("Comparison_int", System_Comparison_int); L.RegFunction("Func_int_int", System_Func_int_int); L.EndModule(); L.EndModule(); L.BeginPreLoad(); L.AddPreLoad("UnityEngine.MeshRenderer", LuaOpen_UnityEngine_MeshRenderer, typeof(UnityEngine.MeshRenderer)); L.AddPreLoad("UnityEngine.BoxCollider", LuaOpen_UnityEngine_BoxCollider, typeof(UnityEngine.BoxCollider)); L.AddPreLoad("UnityEngine.MeshCollider", LuaOpen_UnityEngine_MeshCollider, typeof(UnityEngine.MeshCollider)); L.AddPreLoad("UnityEngine.SphereCollider", LuaOpen_UnityEngine_SphereCollider, typeof(UnityEngine.SphereCollider)); L.AddPreLoad("UnityEngine.CharacterController", LuaOpen_UnityEngine_CharacterController, typeof(UnityEngine.CharacterController)); L.AddPreLoad("UnityEngine.CapsuleCollider", LuaOpen_UnityEngine_CapsuleCollider, typeof(UnityEngine.CapsuleCollider)); L.AddPreLoad("UnityEngine.Animation", LuaOpen_UnityEngine_Animation, typeof(UnityEngine.Animation)); L.AddPreLoad("UnityEngine.AnimationClip", LuaOpen_UnityEngine_AnimationClip, typeof(UnityEngine.AnimationClip)); L.AddPreLoad("UnityEngine.AnimationState", LuaOpen_UnityEngine_AnimationState, typeof(UnityEngine.AnimationState)); L.AddPreLoad("UnityEngine.BlendWeights", LuaOpen_UnityEngine_BlendWeights, typeof(UnityEngine.BlendWeights)); L.AddPreLoad("UnityEngine.RenderTexture", LuaOpen_UnityEngine_RenderTexture, typeof(UnityEngine.RenderTexture)); L.AddPreLoad("UnityEngine.Rigidbody", LuaOpen_UnityEngine_Rigidbody, typeof(UnityEngine.Rigidbody)); L.EndPreLoad(); Debugger.Log("Register lua type cost time: {0}", Time.realtimeSinceStartup - t); }