Unity中使用sLua的 超丶简单基础教程(三)

614 阅读1分钟
原文链接: lafree317.github.io

前言

Unity中使用sLua的 超丶简单基础教程(一)

Unity中使用sLua的 超丶简单基础教程(二)

这一篇展示一个用SLua这个库 在Lua中执行Unity的生命周期方法,直接上代码:

正文

在创建一个新的场景,一个新的CreateEmpty把C#脚本挂上去代码如下:

LuaManager.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using SLua;

public class LuaManager : MonoBehaviour {

    private LuaFunction _luaStart = null;
    private LuaFunction _luaUpdate = null;
    private LuaFunction _luaLateUpdate = null;
    private LuaFunction _luaFixedUpdate = null;
    private LuaFunction _luaAwake = null;
    private LuaFunction _luaOnDisable = null;
    private LuaFunction _luaOnDestroy = null;


    private void Awake()
    {
        LuaSvr svr = new LuaSvr();// 如果不先进行某个LuaSvr的初始化的话,下面的mianState会爆一个为null的错误..
        LuaSvr.mainState.loaderDelegate += LuaReourcesFileLoader;
		svr.init(null, () => // 如果不用init方法初始化的话,在Lua中是不能import的
		{
            svr.start("Main");
            _luaAwake = LuaSvr.mainState.getFunction("Awake");
            _luaStart = LuaSvr.mainState.getFunction("Start");
            _luaFixedUpdate = LuaSvr.mainState.getFunction("FixedUpdate");
            _luaUpdate = LuaSvr.mainState.getFunction("Update");
            _luaLateUpdate = LuaSvr.mainState.getFunction("LateUpdate");
            _luaOnDisable = LuaSvr.mainState.getFunction("OnDisable");
            _luaOnDestroy = LuaSvr.mainState.getFunction("OnDestroy");
		});
        if(_luaAwake != null){
            _luaAwake.call();
        }
    }

	private void Start ()
    {

        if(_luaStart != null)
        {
            _luaStart.call();
        }
	}

    // SLua Loader代理方法
    private static byte[] LuaReourcesFileLoader(string strFile)
    {
        // 这里为了测试就不先判断为空,开发的时候再加上
        string filename = Application.dataPath + "/Scripts/Lua/" + strFile.Replace('.', '/') + ".txt";
        return File.ReadAllBytes(filename);
    }

    void FixedUpdate()
    {
        if(_luaFixedUpdate != null)
        {
            _luaFixedUpdate.call();
        }
    }
    void Update()
    {
        if(_luaUpdate != null)
        {
            _luaUpdate.call();
        }
    }
    void LateUpdate()
    {
        if(_luaLateUpdate != null)
        {
            _luaLateUpdate.call();
        }
    }
    void OnDisable()
    {
        if(_luaOnDisable != null)
        {
            _luaOnDisable.call();
        }
    }
    void OnDestroy()
    {
        if(_luaOnDestroy == null)
        {
            _luaOnDestroy.call();
        }
    }
}

Main.txt

import "UnityEngine"

-- main方法,入口函数
function main()
    print("Lua创建了一个Cube")
    local cube = GameObject.CreatePrimitive(PrimitiveType.Cube)
end

function Awake()
    print("Awake")
end

function Start()
    print("Start")
end

function FixedUpdate()
    print("FixedUpdate")
end

function Update()
    print("luaUpdate")
end

function LateUpdate()
    print("LateUpdate")
end

function OnDisable()
    print("OnDisable")
end

function OnDestroy()
    print("OnDestroy")
end

效果图

其他

之后总结几篇SLua中调用Prebab/UI等…

本篇教程很基础,如果有精力会将之后学习到的知识都整理成博客分享给大家~