Unity connects to Xlua interaction

Table of contents

file location

C#

Delete

Effect demo​


file needs comment

file location

It is important to remember the location of these files

 C#

using System;
using System.IO;
using UnityEngine;
using XLua;
public class LuaLine : MonoBehaviour
{
    private LuaEnv _luaEnv = new LuaEnv();

    private Action OnStartEvent;

    private Action OnUpdateEvent;

    void Start()
    {
        _luaEnv.AddLoader(GetLuaFromFile);

        _luaEnv.DoString("require 'StartLua'");

        OnStartEvent = _luaEnv.Global.Get<Action>("Start");

        OnUpdateEvent = _luaEnv.Global.Get<Action>("UpDate");

        OnStartEvent?.Invoke();
    }

    private byte[] GetLuaFromFile(ref string fileName)
    {
        fileName = fileName.Replace('.', '/');
        var path = Application.dataPath + "/XluaScript/" + fileName + ".lua";
        if (!File.Exists(path))
        {
            return null;
        }
        var datas = File.ReadAllBytes(path);
        return datas;
    }

    void Update()
    {
        OnUpdateEvent?.Invoke();
    }
    private void OnDestroy()
    {
        OnUpdateEvent = null;
    }
}

Delete

require("Lipus/BaseClass")
require("Lipus/head")

function Start()
    print("我是xlua的start")
end

function UpDate()
    print("我是xlua的UpDate")
end

 Effect demonstration

Guess you like

Origin blog.csdn.net/q1295006114/article/details/131411845