Unity热更新专题(八)uLua示例Demo简析及SimpleFramework工作流程
我们在进行热更新的完整案例之前。有必要分析一下uLua的实例代码,了解一下SimpleFramework工作流程,在我们制作实例时便于理解。
1、LuaState
using UnityEngine;using System.Collections;using LuaInterface;public class HelloWorld : MonoBehaviour {// Use this for initializationvoid Start () {LuaState l = new LuaState();string str = "print('hello world 世界')";l.DoString(str);}// Update is called once per framevoid Update () {}}这个示例代码中就使用了LuaState。
2、LuaScriptMgr
using UnityEngine;using System.Collections;using LuaInterface;public class CreateGameObject02 : MonoBehaviour {private string script = @"luanet.load_assembly('UnityEngine')GameObject = UnityEngine.GameObjectParticleSystem = UnityEngine.ParticleSystemlocal newGameObj = GameObject('NewObj')newGameObj:AddComponent(ParticleSystem.GetClassType())";//非反射调用void Start () {LuaScriptMgr lua = new LuaScriptMgr();lua.Start();lua.DoString(script);}// Update is called once per framevoid Update () {}}这个示例代码中就使用了LuaScriptMgr。
上面的两个示例中,要注意一下的代码:
private string script = @"luanet.load_assembly('UnityEngine')GameObject = UnityEngine.GameObjectParticleSystem = UnityEngine.ParticleSystemlocal newGameObj = GameObject('NewObj')newGameObj:AddComponent(ParticleSystem.GetClassType())";大概工作流程就是先加载UnityEngine,然后创建一个GameObject变量,再调用AddComponent方法。其实就和Unity里面使用的差不多。很简单。不多说。
3、在Unity中访问Lua中的变量
using UnityEngine;using System.Collections;using LuaInterface;public class AccessingLuaVariables01 : MonoBehaviour {private string script = @"luanet.load_assembly('UnityEngine')GameObject = luanet.import_type('UnityEngine.GameObject')ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')particles = {}for i = 1, Objs2Spawn, 1 dolocal newGameObj = GameObject('NewObj')local ps = newGameObj:AddComponent(luanet.ctype(ParticleSystem))ps:Stop()table.insert(particles, ps)endvar2read = 42";void Start () {LuaState l = new LuaState();l["Objs2Spawn"] = 5;//就是这里l.DoString(script);print("Read from lua: " + l["var2read"].ToString());LuaTable particles = (LuaTable)l["particles"];foreach( ParticleSystem ps in particles.Values ){ps.Play();}}void Update () {}}4、执行Lua脚本文件,调用Lua方法,在Lua中使用协程
using UnityEngine;using System.Collections;using LuaInterface;public class ScriptsFromFile_01 : MonoBehaviour{public TextAsset scriptFile;// Use this for initializationvoid Start(){LuaState l = new LuaState();l.DoString(scriptFile.text);}// Update is called once per framevoid Update(){}}而我们的File文件就是在同目录下的与类同名的txt文件。
注意,我们这里定义的File文件,是TextAsset类型。
public TextAsset scriptFile;接下来就是调用Lua的方法。也很简单。
using UnityEngine;using System.Collections;using LuaInterface;public class CallLuaFunction_01 : MonoBehaviour {private string script = @"function luaFunc(message)print(message)return 42end";void Start () {LuaState l = new LuaState();l.DoString(script);LuaFunction f = l.GetFunction("luaFunc");object[] r = f.Call("I called a lua function!");print(r[0]);}void Update () {}}协程的使用:
using UnityEngine;using System.Collections;using LuaInterface;public class LuaCoroutines : MonoBehaviour {private string script = @"function fib(n)local a, b = 0, 1while n > 0 doa, b = b, a + bn = n – 1endreturn aendfunction CoFunc()print('Coroutine started')local i = 0for i = 0, 10, 1 doprint(fib(i))coroutine.wait(1)endprint('Coroutine ended')endfunction myFunc()coroutine.start(CoFunc)end";private LuaScriptMgr lua = null;void Awake (){lua = new LuaScriptMgr();lua.Start();lua.DoString(script);LuaFunction f = lua.GetLuaFunction("myFunc");f.Call();f.Release();}void Update (){lua.Update();}void LateUpdate(){lua.LateUpate();}void FixedUpdate(){lua.FixedUpdate();}}好了。uLua的示例代码我们就分析到这里了。接下来的可以自己琢磨,对于我们的案例来说已经够了。下面我们来分析一下自带热更新的案例的工作流程。也就是我们的SimpleFramework的工作流程。
5、框架启动GlobalGenerator,生成appview和gamemanager
在这里我们首先要看一下框架的目录。
整个SimpleFramework采用的是PureMVC的框架。看下图。
要做一个积极勇敢乐观的追梦人,永远不说消极的话,