@@ -7,19 +7,72 @@ public class LuaManager : Manager {
77 private LuaState lua ;
88 private LuaLoader loader ;
99
10+ private LuaFunction updateFunc = null ;
11+ private LuaFunction lateUpdateFunc = null ;
12+ private LuaFunction fixedUpdateFunc = null ;
13+
14+ public LuaEvent UpdateEvent {
15+ get ;
16+ private set ;
17+ }
18+
19+ public LuaEvent LateUpdateEvent {
20+ get ;
21+ private set ;
22+ }
23+
24+ public LuaEvent FixedUpdateEvent {
25+ get ;
26+ private set ;
27+ }
28+
1029 // Use this for initialization
1130 void Awake ( ) {
1231 loader = new LuaLoader ( ) ;
1332 lua = new LuaState ( ) ;
33+ this . InitLuaLibrary ( ) ;
1434 LuaBinder . Bind ( lua ) ;
35+ LuaCoroutine . Register ( lua , this ) ;
1536 }
1637
1738 public void InitStart ( ) {
1839 InitLuaPath ( ) ;
1940 InitLuaBundle ( ) ;
2041 this . lua . Start ( ) ; //启动LUAVM
42+
43+ lua . DoFile ( "Main.lua" ) ;
44+
45+ updateFunc = lua . GetFunction ( "Update" ) ;
46+ lateUpdateFunc = lua . GetFunction ( "LateUpdate" ) ;
47+ fixedUpdateFunc = lua . GetFunction ( "FixedUpdate" ) ;
48+
49+ LuaFunction main = lua . GetFunction ( "Main" ) ;
50+ main . Call ( ) ;
51+ main . Dispose ( ) ;
52+ main = null ;
53+
54+ UpdateEvent = GetEvent ( "UpdateBeat" ) ;
55+ LateUpdateEvent = GetEvent ( "LateUpdateBeat" ) ;
56+ FixedUpdateEvent = GetEvent ( "FixedUpdateBeat" ) ;
57+ }
58+
59+ /// <summary>
60+ /// 初始化加载第三方库
61+ /// </summary>
62+ void InitLuaLibrary ( ) {
63+ lua . OpenLibs ( LuaDLL . luaopen_pb ) ;
64+ lua . OpenLibs ( LuaDLL . luaopen_sproto_core ) ;
65+ lua . OpenLibs ( LuaDLL . luaopen_protobuf_c ) ;
66+ lua . OpenLibs ( LuaDLL . luaopen_lpeg ) ;
67+ lua . OpenLibs ( LuaDLL . luaopen_cjson ) ;
68+ lua . OpenLibs ( LuaDLL . luaopen_cjson_safe ) ;
69+ lua . OpenLibs ( LuaDLL . luaopen_bit ) ;
70+ lua . OpenLibs ( LuaDLL . luaopen_socket_core ) ;
2171 }
2272
73+ /// <summary>
74+ /// 初始化Lua代码加载路径
75+ /// </summary>
2376 void InitLuaPath ( ) {
2477 if ( AppConst . DebugMode ) {
2578 string rootPath = AppConst . FrameworkRoot ;
@@ -47,6 +100,52 @@ void InitLuaBundle() {
47100 }
48101 }
49102
103+ void Update ( ) {
104+ if ( updateFunc != null ) {
105+ updateFunc . BeginPCall ( TracePCall . Ignore ) ;
106+ updateFunc . Push ( Time . deltaTime ) ;
107+ updateFunc . Push ( Time . unscaledDeltaTime ) ;
108+ updateFunc . PCall ( ) ;
109+ updateFunc . EndPCall ( ) ;
110+ }
111+ lua . Collect ( ) ;
112+ #if UNITY_EDITOR
113+ lua . CheckTop ( ) ;
114+ #endif
115+ }
116+
117+ void LateUpdate ( ) {
118+ if ( lateUpdateFunc != null ) {
119+ lateUpdateFunc . BeginPCall ( TracePCall . Ignore ) ;
120+ lateUpdateFunc . PCall ( ) ;
121+ lateUpdateFunc . EndPCall ( ) ;
122+ }
123+ }
124+
125+ void FixedUpdate ( ) {
126+ if ( fixedUpdateFunc != null ) {
127+ fixedUpdateFunc . BeginPCall ( TracePCall . Ignore ) ;
128+ fixedUpdateFunc . Push ( Time . fixedDeltaTime ) ;
129+ fixedUpdateFunc . PCall ( ) ;
130+ fixedUpdateFunc . EndPCall ( ) ;
131+ }
132+ }
133+
134+ LuaEvent GetEvent ( string name ) {
135+ LuaTable table = lua . GetTable ( name ) ;
136+ LuaEvent e = new LuaEvent ( table ) ;
137+ table . Dispose ( ) ;
138+ table = null ;
139+ return e ;
140+ }
141+
142+ void SafeRelease ( ref LuaFunction luaRef ) {
143+ if ( luaRef != null ) {
144+ luaRef . Dispose ( ) ;
145+ luaRef = null ;
146+ }
147+ }
148+
50149 public object [ ] DoFile ( string filename ) {
51150 return lua . DoFile ( filename ) ;
52151 }
@@ -65,7 +164,26 @@ public void LuaGC() {
65164 }
66165
67166 public void Close ( ) {
167+ SafeRelease ( ref updateFunc ) ;
168+ SafeRelease ( ref lateUpdateFunc ) ;
169+ SafeRelease ( ref fixedUpdateFunc ) ;
170+
171+ if ( UpdateEvent != null ) {
172+ UpdateEvent . Dispose ( ) ;
173+ UpdateEvent = null ;
174+ }
175+
176+ if ( LateUpdateEvent != null ) {
177+ LateUpdateEvent . Dispose ( ) ;
178+ LateUpdateEvent = null ;
179+ }
180+
181+ if ( FixedUpdateEvent != null ) {
182+ FixedUpdateEvent . Dispose ( ) ;
183+ FixedUpdateEvent = null ;
184+ }
68185 lua . Dispose ( ) ;
186+ lua = null ;
69187 loader = null ;
70188 }
71189 }
0 commit comments