温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

一条非洲人--xlua整合fairyGui(一)

发布时间:2020-06-12 12:14:02 来源:网络 阅读:4874 作者:一条非洲人 栏目:游戏开发

最近公司在改造旧项目,经过讨论后决定用fairyGUI重做UI部分,lua插件用xlua,战斗部分用打补丁方式热更新。
以下是整合的过程
首先新建一个xlua工程,把fairyGUi_unitySDk的源码和luasupport拷过来

一条非洲人--xlua整合fairyGui(一)

这个时候luaUIHelper会报错,我们从这个类开始修改。
1.using luaInterface改为using Xlua

2.[NoToLuaAttribute]改为[BlackListAttribute]

3.获取luafunction:
peerTable.GetLuaFunction改为Get<LuaFunction>

4.执行lua的方法

_OnInit.BeginPCall();
_OnInit.Push(this);
_OnInit.PCall();
_OnInit.EndPCall();
改为
_OnInit.Action<LuaWindow>(this);(无gc调用)
其他几个函数如此类推

5.带返回值的lua方法调用

extendFunction.BeginPCall();
extendFunction.Push(gcom);
extendFunction.PCall();
_peerTable = extendFunction.CheckLuaTable();
extendFunction.EndPCall();
改为
_peerTable = extendFunction.Func<GComponent,LuaTable>(gcom);(无gc调用)

这个时候LuaUIHelper已经不报错了。

接下来我们要生成和FairyGui关联的wrap,直接贴代码:
using XLua;
using System.Collections.Generic;
using System;
using FairyGUI;

public static class FairyGuiGenConfig
{
//lua中要使用到C#库的配置,比如C#标准库,或者Unity API,第三方库等。
[LuaCallCSharp]
public static List<Type> LuaCallCSharp = new List<Type>() {
typeof(EventContext),
typeof(EventDispatcher),
typeof(EventListener),
typeof(InputEvent),
typeof(DisplayObject),
typeof(Container),
typeof(Stage),
typeof(FairyGUI.Controller),
typeof(GObject),
typeof(GGraph),
typeof(GGroup),
typeof(GImage),
typeof(GLoader),
typeof(GMovieClip),
typeof(TextFormat),
typeof(GTextField),
typeof(GRichTextField),
typeof(GTextInput),
typeof(GComponent),
typeof(GList),
typeof(GRoot),
typeof(GLabel),
typeof(GButton),
typeof(GComboBox),
typeof(GProgressBar),
typeof(GSlider),
typeof(PopupMenu),
typeof(ScrollPane),
typeof(Transition),
typeof(UIPackage),
typeof(Window),
typeof(GObjectPool),
typeof(Relations),
typeof(RelationType),
typeof(Timers),
typeof(GTween),
typeof(GTweener),
typeof(EaseType),
typeof(TweenValue),

    typeof(LuaUIHelper),
    typeof(GLuaComponent),
    typeof(GLuaLabel),
    typeof(GLuaButton),
    typeof(GLuaProgressBar),
    typeof(GLuaSlider),
    typeof(GLuaComboBox),
    typeof(LuaWindow)

typeof(GoWrapper)
};

//C#静态调用Lua的配置(包括事件的原型),仅可以配delegate,interface
[CSharpCallLua]
public static List<Type> CSharpCallLua = new List<Type>() {
    typeof(EventCallback0),
    typeof(EventCallback1)
};

}
用Generate Code生成一下

这个时候我们可以试着用lua生成一个界面了
首先我们先实现调用Main的功能,具体实现看教程。
我们先自己编辑一个UI文件,新建Main.lua和MainPanel.lua
在Main.lua引入FairyGUI.lua
执行一下,发现报错,因为xlua调用cs类的时候要加上CS.
所以我们加上FairyGUI = CS.FairyGUI
启动成功,开始写逻辑,此处我用FairyGuiDemo里面的Model例子来修改,写法是模仿官网上的例子
例子:
MainPanel = fgui.window_class()

local Resources = CS.UnityEngine.Resources
local Object = CS.UnityEngine.Object
local Vector3 = CS.UnityEngine.Vector3

--构建函数
function MainPanel:ctor()
UIPackage.AddPackage('UI/Model');
end

--可覆盖的函数(可选,不是说必须)
function MainPanel:OnInit()
self.contentPane = UIPackage.CreateObject('Model', 'Main')

local prefab = Resources.Load("Role/npc");
local  go = Object.Instantiate(prefab);
go.transform.localPosition = Vector3(61, -89, 1000);
go.transform.localScale = Vector3(180, 180, 180);
go.transform.localEulerAngles = Vector3(0, 100, 0);
self.contentPane:GetChild("holder").asGraph:SetNativeObject(GoWrapper(go));

end

function MainPanel:OnShown()
end

function MainPanel:OnHide()
end

调用:
local view = MainPanel.New();
view:Show();

运行一下,发现报错了
1.由于xlua没有bind New函数,所以我们new对象要将
FairyGUI.LuaWindow.New()改为FairyGUI.LuaWindow()

2.调用了tolua的函数tolua.setpeer,看了一下源码和百度,发现是用来继承cs的,然后在百度一下xlua对应的api,最后在作者的github找到了https://github.com/Tencent/xLua/issues/405
最后将tolua.setpeer(ins, t)改为xutil.state(ins, t)

3.xlua不能访问c#没有的属性,tolua会返回null,xlua就报错了,后来笔者把这个属性先放到lua这边
将ins.EventDelegates = {}改为t.EventDelegates

再运行一下,期待的画面出现了

一条非洲人--xlua整合fairyGui(一)
最基础的功能实现了,下次我会记录一下事件的修改。

项目Git:https://github.com/TaoOneOne/xLua_FairyGui_Demo
unity版本:5.6.5

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI