UIButton
UIButton is a subclass of UIInteractive, which implements basic button functionality. The button component encapsulates UIImage
for displaying background images, inherits from the interactive component UIInteractive
, and uses the square area described by the built-in UITransform
as the sensing hot zone.
Component Texture
The component has multiple states UIInteractiveStyle, each state corresponds to a texture
normalSprite
- Texture by defaultdownSprite
- Texture when the mouse is pressedoverSprite
- Texture when the mouse is overdisableSprite
- Texture when the component is disabled
We can set the corresponding background image of the component in various states to obtain the effect of the button changing style with the state switching
let button = new Object3D()
let guiButton: UIButton = button.addComponent(UIButton)
// Load a group of sprite textures
await Engine3D.res.loadAtlas('https://cdn.orillusion.com/atlas/UI_atlas.json')
// Set the corresponding state texture of the button
guiButton.normalSprite = Engine3D.res.getGUISprite('button-up')
guiButton.downSprite = Engine3D.res.getGUISprite('button-down')
guiButton.overSprite = Engine3D.res.getGUISprite('button-over')
guiButton.disableSprite = Engine3D.res.getGUISprite('button-disable')
2
3
4
5
6
7
8
9
Enable / Disable
To set whether the component is valid:
true
- Button will respond to mouse eventsfalse
-Does not respond to mouse events, and presents the texture ofdisableSprite
let button = new Object3D()
let guiButton: UIButton = button.addComponent(UIButton)
guiButton.enable = true; //false
2
3
Component Interaction
Same as the normal Object3D
, we can use the event listener to listen to the GUI
series events in PickGUIEvent3D
, and we can get the user operation response function callback:
PickGUIEvent3D.PICK_CLICK_GUI
: Mouse clickPickGUIEvent3D.PICK_OVER_GUI
: Mouse hoverPickGUIEvent3D.PICK_OUT_GUI
: Mouse outPickGUIEvent3D.PICK_UP_GUI
: Mouse upPickGUIEvent3D.PICK_DOWN_GUI
: Mouse down
TIP
Since v0.8.4
, the GUI component's mouse events have deprecated the original PointerEvent3D
event in favor of using PickGUIEvent3D
as the default mouse event handler. Apart from the name change, other parameters remain unchanged
let button = new Object3D()
let guiButton: UIButton = button.addComponent(UIButton)
button.addEventListener(PickGUIEvent3D.PICK_CLICK_GUI, this.onClick, this)
button.addEventListener(PickGUIEvent3D.PICK_OUT_GUI, this.onOut, this)
button.addEventListener(PickGUIEvent3D.PICK_OVER_GUI, this.onOver, this)
button.addEventListener(PickGUIEvent3D.PICK_DOWN_GUI, this.onDown, this)
2
3
4
5
6
7
Button Text
A component can add multiple GUI components, such as adding UIButton
and UITextField
at the same time
let button = new Object3D()
let guiButton: UIButton = button.addComponent(UIButton)
...
// Load font resources
await Engine3D.res.loadFont('https://cdn.orillusion.com/fnt/0.fnt')
// Add GUI text
let buttonLabel = button.addComponent(UITextField);
buttonLabel.text = 'Click me';
2
3
4
5
6
7
8
import { Engine3D, Scene3D, Object3D, Camera3D, ViewPanel, UIButton, HoverCameraController, PointerEvent3D, View3D, AtmosphericComponent, UITextField, Color, TextAnchor, WorldPanel, UIPanel, GPUCullMode, PickGUIEvent3D } from '@orillusion/core';
class Sample_button {
async run() {
// initializa engine
await Engine3D.init();
// create new scene as root node
let scene3D: Scene3D = new Scene3D();
scene3D.addComponent(AtmosphericComponent);
// create camera
let cameraObj: Object3D = new Object3D();
let camera = cameraObj.addComponent(Camera3D);
// adjust camera view
camera.perspective(60, Engine3D.aspect, 1, 5000.0);
// set camera controller
let controller = cameraObj.addComponent(HoverCameraController);
controller.setCamera(0, -20, 30);
// add camera node
scene3D.addChild(cameraObj);
let view = new View3D();
view.scene = scene3D;
view.camera = camera;
Engine3D.startRenderView(view);
// create panel root
let panelRoot: Object3D = new Object3D();
let panel: UIPanel = panelRoot.addComponent(WorldPanel);
panel.cullMode = GPUCullMode.none;
panelRoot.localScale.set(0.1, 0.1, 0.1);
let canvas = view.enableUICanvas();
canvas.addChild(panelRoot);
// create button node
let buttonQuad = new Object3D();
panelRoot.addChild(buttonQuad);
// create button component
let button: UIButton = buttonQuad.addComponent(UIButton);
// set button size
button.uiTransform.resize(180, 60);
// set button status
// load sprite resource
await Engine3D.res.loadAtlas('https://cdn.orillusion.com/atlas/UI_atlas.json');
button.normalSprite = Engine3D.res.getGUISprite('button-up');
button.downSprite = Engine3D.res.getGUISprite('button-down');
button.overSprite = Engine3D.res.getGUISprite('button-over');
button.disableSprite = Engine3D.res.getGUISprite('button-disable');
// add button text
// load font resource
await Engine3D.res.loadFont('https://cdn.orillusion.com/fnt/0.fnt');
let buttonLabel = buttonQuad.addComponent(UITextField);
buttonLabel.text = 'Click me';
buttonLabel.fontSize = 24;
buttonLabel.color = new Color(1, 0.8, 0.4);
buttonLabel.alignment = TextAnchor.MiddleCenter;
// add listener
buttonQuad.addEventListener(PickGUIEvent3D.PICK_CLICK_GUI, this.onClick, this);
buttonQuad.addEventListener(PickGUIEvent3D.PICK_OUT_GUI, this.onOut, this);
buttonQuad.addEventListener(PickGUIEvent3D.PICK_OVER_GUI, this.onOver, this);
buttonQuad.addEventListener(PickGUIEvent3D.PICK_DOWN_GUI, this.onDown, this);
}
private onClick() {}
private onOut() {}
private onOver() {}
private onDown() {
alert('clicked me!');
}
}
new Sample_button().run();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76