前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SLua-在Lua中实现Unity中的生命周期

SLua-在Lua中实现Unity中的生命周期

作者头像
祝你万事顺利
发布2019-07-26 14:58:55
1.4K0
发布2019-07-26 14:58:55
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

Enumerable.Cast(IEnumerable) Method : Casts the elements of an IEnumerable to the specified type. 将一个IEnumerable的元素转换为一个具体的类型。

实现Unity中的Unity生命周期 1.获取Lua的表 2.获取表中的Update方法 3.使用强制类型转换将LuaFunction转换成一个委托方法,此委托方法可以传入LuaTable自身 4.在C#中调用转换成委托的方法并传入LuaTable

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;

using UnityEngine;
using SLua;
using System.IO;
using System;

public class OpenLuaFile : MonoBehaviour {

    private LuaSvr lua_svr;
    private LuaTable self;
    private LuaFunction update;

    [CustomLuaClass]
    public delegate void UpdateDelegate(object self);

    UpdateDelegate ud;

    void Start () {
        lua_svr = new LuaSvr();
        lua_svr.init(null, InitComplete,LuaSvrFlag.LSF_EXTLIB);
    }

    private void InitComplete()
    {
        //自定义Lua文件
        LuaState.main.loaderDelegate += LuaLoader;
        self = (LuaTable)lua_svr.start("HelloLua");
        update = (LuaFunction)self["Update"];
        ud = update.cast<UpdateDelegate>();
    }
    private void Update()
    {
        if (ud != null)
        {
            ud(self);
        }
    }

    [DoNotToLua]
    private byte[] LuaLoader(string fn, ref string absoluteFn)
    {
        string newFn = fn.Replace(".", "/");
        string path = Application.dataPath + "/Scripts/Lua/" + newFn + ".lua";
        byte[] data = null;
        if (File.Exists(path))
        {
            data = File.ReadAllBytes(path);
        }
        else
        {
            TextAsset txt = Resources.Load<TextAsset>(newFn);
        }
        return data;
    }
}

Lua中的表中定义的Update方法

代码语言:javascript
复制
function main()
    maxItemCount = 40;

    scrollRect = GameObject.Find("Canvas/Backpack"):GetComponent(UI.ScrollRect);
    
    content = scrollRect.content;
    contentGridLayoutGroup = content:GetComponent(UI.GridLayoutGroup);

    columnCount = math.floor((content.rect.width - contentGridLayoutGroup.spacing.x)/contentGridLayoutGroup.cellSize.x);
    if columnCount ~= 0 then
        rowCount = math.ceil( content.childCount/columnCount );
        curBottomIndex = (rowCount - 1)*columnCount;
        initializedBottomIndex = curBottomIndex;
    end

    oneBlockHeight = contentGridLayoutGroup.cellSize.y + contentGridLayoutGroup.spacing.y;
    local contentSizeDeltaY = math.floor( oneBlockHeight * (math.ceil(maxItemCount / columnCount))) ;
    content.sizeDelta = UnityEngine.Vector2(content.sizeDelta.x,contentSizeDeltaY);

    for i=1,maxItemCount do
        table.insert(itemIDs,i);
    end

    scrollRect.onValueChanged:AddListener(ListenerMethod);
    FreshBackpack();

    return class;
end
function class : Update()
    if UnityEngine.Input.GetKeyDown(KeyCode.Space) then
        JumpToTargetID(12);
    end
    if UnityEngine.Input.GetKeyDown(KeyCode.A) then
        if #itemIDs < maxItemCount then
            table.insert( itemIDs,math.random(0,100) );
            FreshBackpack();
        else
            print("BP Full!");
        end
    end
end
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.07.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档