Home
  • English
  • 中文
Home
  • English
  • 中文
  • Get Started

    • Install
    • Hello LGUI
    • Custom Button
    • Use custom font
    • Prefab workflow
    • Interaction
    • Button from scratch
  • Sample Projects

    • Sample Projects
  • Prefab Workflow

    • LGUI Prefab
    • PrefabEditor
    • LGUILifeCycleBehaviour
    • LGUIPrefabInterface
  • Rendering

    • LGUICanvas
    • Screen Space Overlay
    • World Space - UE Renderer
    • World Space - LGUI Renderer
    • LGUI render flow
    • UICanvasGroup
  • Visual Components

    • UIText
    • UIEffect
    • TextAnimation
    • UIBackgroundBlur
    • UIBackgroundPixelate
  • Events

    • LGUIEventDelegate
    • LGUIEventSystem
  • Interaction Components

    • UISelectable component
  • Layout

    • Basic Layout
    • Auto Layout
  • Tween Animation

    • LTween
    • Use LTween for UI element
  • Performance

    • LGUI Stat
  • ReleaseNotes

    • LGUI 3.x
    • LGUI 2.x
  • FAQ

    • Why use prefab?
    • Update project from LGUI2 to LGUI3
    • Project 3d world position to LGUI's screen 2d position
    • Deproject LGUI's screen position to world
    • Use custom material for UI elements
    • Disable interaction component
    • Find Child by DisplayName
    • Get actor or component inside prefab
    • Anti-aliasing

Custom Button

In this section, we will learn how to customize our button using UISprite. Before this, you should follow HelloWorld section to create a default button. The default button is a little ugly, we can use a nicer looking texture instead.

Prepare a sprite texture.

Use photoshop or any other app to create a image, or use this one:
step1

Drag the image to engine content folder, name it "Button":
step2

Create UISpriteData.

Select "Button" asset, right click on it and choose "LGUISprite"->"Create Sprite":
step3

Then a LGUISpriteData asset named "Button_Sprite" will be created:
step4

Double click on "Button_Sprite" and open a UISpriteDataEditor:
step4_1

Change "Border Left" and "Border Top" to 14, "Border Right" and "Border Bottom" to 15:
step4_2 Close the UISpriteDataEditor.

Prepare a LGUI ScreenSpaceUI.

Follow the HelloWorld section to create a base ScreenSpaceUI:
step5

Create UISprite and UIText.

Select "ScreenSpaceUIRoot" actor, right click on the arrow button and choose "Create UI Element"->"UISprite", this will create a UISprite actor:
step6

Rename the created UISprite actor to "MyButton", and change "PosZ" to -120:
step7

Select "MyButton" actor, drag "Button_Sprite" asset to "Sprite" property, see the change in viewport:
step8

Select "MyButton" actor, right click on the arrow and choose "Create UI Element"->"UIText", this will create a UIText actor:
step9

Rename the created UIText actor to "MyButtonText", and change "Color" to black, "Text" to "I'm a button", "Size" to 24, "Font Style" to "Bold":
step10

Adjust button size.

Select "MyButton" actor, change "Width" to 240, "Height" to 60:
step11

Select "MyButtonText" actor, click anchor buttn and a AnchorPreset will popup:
step12

Hold "Alt" button on keyboard and click the right bottom button in AnchorPreset, see change in viewport:
step13

Add UIButton component.

Select "MyButton" actor, click "+Add Component" and add a "UIButton" component:
step14

Select "UIButton" component you just created, change the "Transition" to "ColorTint":
step15

Click on the left arrow of "Transition", this will expand the transition area:
step16

Drag "MyButton" actor to "Transition Actor":
step17

Enable RaycastTarget.

Select "MyButton" actor, select "UISprite" component, in LGUI-Raycast category check "Raycast Target" property:
step19

Add button event:

Select "MyButton" actor, select "UIButton" component, find "On Click()" property and click the "+" button:
step20

Set the event like this:
step21

Now hit play and move your mouse over the button, then click the button, see the change in viewport:
step22

Last Updated:
Contributors: lf2007hf
Prev
Hello LGUI
Next
Use custom font