Ready to work

  1. If you are using ToLua, add Scripting Define Symbols FAIRYGUI_TOLUA。 XLUA is not required.
  2. In the case of ToLua, add the following statement to the appropriate place in CustomSettings.cs and then regenerate the binding file.
  1. Put FairyGUI.lua into your lua file directory.

Listening for events

  1. Common method listening and deleting listening
require 'FairyGUI'
function OnClick()
print('you click')
function OnClick(context)
print('you click'..context.sender)
local view = UIPackage.CreateObject('Demo', 'DemoMain')
  1. ToLua supports callback with self
function TestClass:OnClick()
print('you click')
function TestClass:OnClick(context)
print('you click'..context.sender)
self.view.onClick:Add(TestClass.OnClick, self)
self.view.onClick:Remove(TestClass.OnClick, self)

Using the Window class

The Window class provided by FairyGUI generally needs to be extended by developers, such as covering OnShown, OnHide, etc. In Lua, the method for writing Window extensions is:

WindowBase = fgui.window_class ()
    --Build function
    function WindowBase: ctor ()
    --Overridable functions (optional, not necessarily required)
    function WindowBase: OnInit ()
        self.contentPane = UIPackage.CreateObject ("Basics", "WindowA");
    function WindowBase: OnShown ()
    function WindowBase: OnHide ()
    function WindowBase: DoShowAnimation ()
        self: OnShown ();
    function WindowBase: DoHideAnimation ()
        self: HideImmediately ();
    --Create and display window
    local win = WindowBase.New ();
    win: Show ();
    You can also continue to inherit the Window class obtained above, for example:
    MyWindow = fgui.window_class (WindowBase)
    Calling the parent method in the inherited class:
    function MyWindow: OnInit ()
    WindowBase.OnInit (self)

Custom extension

FairyGUI is available in C #UIObjectFactory.SetPackageItemExtensionMake custom extensions. In Lua, you can do the same. Methods as below:

  1. Define extension classes. Pay attention to the basic types, don’t get it wrong. For example, the button is GButton, and the general component is GComponent.
MyButton = fgui.extension_class (GButton)
  -Note that this is not a constructor, it is called when the component has been built
  function MyButton: ctor ()
  print (self: GetChild ('n1'))
  -Add custom methods and fields
  function MyButton: Test ()
  print ('test')
  local get = tolua.initget (MyButton)
  local set = tolua.initset (MyButton)
  get.myProp = function (self)
  return self._myProp
  set.myProp = function (self, value)
  self._myProp = value
  self: GetChild ('n1'). text = value
  1. Register the extension class. Before registering any objects.
fgui.register_extension ("ui://package name/my button", MyButton)
  1. After completing the above two steps, any object created by the resource “My Button” can be accessed using MyButton. E.g:
local myButton = someComponent: GetChild ("myButton") --The resource of this myButton is "My Button"
  myButton: Test ()
  myButton.myProp = 'hello'
  local myButton2 = UIPackage.CreateObject ("package name", "my button")
  myButton2: Test ()
  myButton2.myProp = 'world'