UI / UIScript
UIScript Class
UI Driver script base class for
- UIScript How does it work?
When you want to use the UI functionality of an editor, there is a high chance that you will need to come into contact with UIScript, unless you customize the interface using UserWidget.
The script inherited from UIScript is different from that inherited from Script. The script inherited from Script is mounted in the object manager, and the editor will automatically call the life cycle function for you. But scripts inherited from UIScript are placed in the object manager, and the editor will not automatically call lifecycle functions for you.
He needs to rely on UIService or UIPrefab.
- What are the lifecycles enjoyed by inheriting from UIScript scripts?
🌵 Here is a detailed description
- When will the editor help you call the UI life cycle?
There are two ways:
Use UIService to manage this script for you. When UIService. create and show, the lifecycle of the script will be initiated.
Use UIPrefab. The script is mounted on UIPrefab and placed in the object manager.
Hierarchy
Table of contents
Accessors
canUpdate(): boolean other |
---|
Obtain whether the onUpdate lifecycle function in the UI can be triggered |
fullScreen(inFull : boolean ): void other |
Setting up full screen adaptation with the parent node will verify the size of the parent node to ensure that it follows the size of the parent node |
layer(): number other |
Get the layer layer of UI |
rootCanvas(): Canvas other |
Get the root Canvas node of the UI |
uiObject(): Widget |
Get UI top level widget object |
uiWidgetBase(): UserWidget other |
Get UI top level widget object |
visible(): boolean other |
Get whether the UI is displayed |
Methods
destroy(): void other |
---|
Destroy UI objects |
detectDrag(dragKey : Keys ): EventReply other |
Trigger detection of DragDrop event |
detectDragIfPressed(inPointEvent : PointerEvent , dragKey : Keys ): EventReply other |
Event detection passed, triggering a reply to the DragDrop event. |
newDragDrop(inVisualWidget : Widget , inTag? : string , inPayLoad? : any , inPivot? : DragPivot , inOffset? : Vector2 ): DragDropOperation other |
Create DragDrop event |
remove(): void other |
Remove UI object |
setVisible(inVisible : boolean SlateVisibility , ...params : any []): void other |
Set whether the UI is displayed |
addBehavior(key : string , value : any ): void other |
Add a global behavior |
clearBehavior(): void other |
Clear a global action |
getBehavior(key : string ): any other |
Execute a global action |
removeBehavior(key : string ): void other |
Remove a global behavior |
Accessors
canUpdate
• | • | ||||
---|---|---|---|---|---|
Obtain whether the onUpdate lifecycle function in the UI can be triggered Default is false Returns
| Set whether the onUpdate lifecycle function in the UI can be triggered Parameters
|
fullScreen
• | ||
---|---|---|
Setting up full screen adaptation with the parent node will verify the size of the parent node to ensure that it follows the size of the parent node Parameters
|
layer
• | • | ||||
---|---|---|---|---|---|
Get the layer layer of UI Usage example: Generally speaking, you can use built-in defined ones or customize extended layer ts
Returns
| Set the layer layer where the UI is located The display may affect the zOrder. When UIService is used to display the UI, the zOrder will be dynamically set according to the layer layer. Each call will recalculate the new zOrder of the current layer to ensure that the UI is at the top of the current layer. Parameters
|
rootCanvas
• | ||
---|---|---|
Get the root Canvas node of the UI Example usage: Create a script called NewScript, place it in the object bar, open the script, modify the original content to the following, save and run the game. In this example, this.rootCanvas.name is empty because Canvas has not been added to this interface. ts
Returns
|
uiObject
• | ||
---|---|---|
Get UI top level widget object Example usage: Create a script called NewScript, place it in the object bar, open the script, modify the original content to the following, save and run the game, the editor will dynamically generate a UserWidget, and you can see the name of the UserWidget control. ts
Returns
|
uiWidgetBase
• | ||
---|---|---|
Get UI top level widget object Example usage: Create a script called NewScript, place it in the object bar, open the script, modify the original content to the following, save and run the game, the editor will dynamically generate a UserWidget, and you can see the name of the UserWidget control. ts
Returns
|
visible
• | • | ||||
---|---|---|---|---|---|
Get whether the UI is displayed Returns
| Set whether the UI is displayed The OnShow/OnHide event in the life cycle of the bound script will be triggered. If you need to pass parameters, you can use the setVisible method. Parameters
|
Methods
destroy
• destroy(): void
other
Destroy UI objects
detectDrag
• detectDrag(dragKey
): EventReply
other
Trigger detection of DragDrop event
Parameters
dragKey Keys | Usage: Trigger button default: mw.Keys |
---|
Returns
EventReply | Return the triggered event reply |
---|
detectDragIfPressed
• detectDragIfPressed(inPointEvent
, dragKey
): EventReply
other
Event detection passed, triggering a reply to the DragDrop event.
Parameters
inPointEvent PointerEvent | Usage: Transmitting triggered event information |
---|---|
dragKey Keys | Usage: Trigger button |
Returns
EventReply | Return the triggered event reply |
---|
newDragDrop
• newDragDrop(inVisualWidget
, inTag?
, inPayLoad?
, inPivot?
, inOffset?
): DragDropOperation
other
Create DragDrop event
Parameters
inVisualWidget Widget | Usage: drag the displayed UI widget |
---|---|
inTag? string | Usage: tag text default: "" range: unlimited |
inPayLoad? any | Usage: Drag and drop event data information default: null |
inPivot? DragPivot | Usage: Drag and drop to display UI anchor point default: UIType DragPivot.TopLeft |
inOffset? Vector2 | Usage: Drag to display the percentage of offset of UI relative to anchor default: vector2 (0,0) |
Returns
DragDropOperation | Return the triggered event reply |
---|
remove
• remove(): void
other
Remove UI object
setVisible
• setVisible(inVisible
, ...params
): void
other
Set whether the UI is displayed
Parameters
inVisible boolean SlateVisibility | Set whether it is visible. If it is a boolean type, set it to SelfHitTestInvisible, and if it is invisible, set it to Collapsed, Otherwise, set the specific display type according to the enumeration. |
---|---|
...params any [] | Parameters passed to onShow |
The OnShow/OnSide event that triggers the bound script can pass parameters.
addBehavior
• Static
addBehavior(key
, value
): void
other
Add a global behavior
Parameters
key string | Behavior tag range: The string length is not limited, as long as it is reasonable |
---|---|
value any | Behavioral value |
UI A more convenient way of event communication.
Example usage: Create a script called NewScript, place it in the object bar, open the script, modify the original content to the following, save and run the game, a screen UI button will be generated in the scene, press the P key, and the button text will change.
@Component
export default class NewScript extends Script {
protected onStart(): void {
if(SystemUtil.isClient()){
//UIService.create(newUI);
UIService.show(newUI);
UIScript.addBehavior("UI",(ui:StaleButton)=>{
ui.text = "change";
});
}
}
}
class newUI extends UIScript{
button:StaleButton;
protected onStart() {
//Set whether onUpdate can be triggered every frame
this.canUpdate = false;
this.layer = UILayerMiddle;
this.button = StaleButton.newObject(this.rootCanvas);
this.button.text = "Press to turn red";
this.button.transitionEnable = true;
this.button.pressedImagColor = LinearColor.red;
this.button.visibility = SlateVisibility.Visible;
this.button.onClicked.add(() => {
console.log("click");
})
InputUtil.onKeyDown(Keys.P,()=>{
let ui = UIScript.getBehavior("UI");
ui(this.button);
});
}
}
@Component
export default class NewScript extends Script {
protected onStart(): void {
if(SystemUtil.isClient()){
//UIService.create(newUI);
UIService.show(newUI);
UIScript.addBehavior("UI",(ui:StaleButton)=>{
ui.text = "change";
});
}
}
}
class newUI extends UIScript{
button:StaleButton;
protected onStart() {
//Set whether onUpdate can be triggered every frame
this.canUpdate = false;
this.layer = UILayerMiddle;
this.button = StaleButton.newObject(this.rootCanvas);
this.button.text = "Press to turn red";
this.button.transitionEnable = true;
this.button.pressedImagColor = LinearColor.red;
this.button.visibility = SlateVisibility.Visible;
this.button.onClicked.add(() => {
console.log("click");
})
InputUtil.onKeyDown(Keys.P,()=>{
let ui = UIScript.getBehavior("UI");
ui(this.button);
});
}
}
clearBehavior
• Static
clearBehavior(): void
other
Clear a global action
getBehavior
• Static
getBehavior(key
): any
other
Execute a global action
Parameters
key string | Behavior tag range: The string length is not limited, as long as it is reasonable |
---|
Returns
any | Return an action |
---|
Example usage: Create a script called NewScript, place it in the object bar, open the script, modify the original content to the following, save and run the game, a screen UI button will be generated in the scene, press the P key, and the button text will change.
@Component
export default class NewScript extends Script {
protected onStart(): void {
if(SystemUtil.isClient()){
//UIService.create(newUI);
UIService.show(newUI);
UIScript.addBehavior("UI",(ui:StaleButton)=>{
ui.text = "change";
});
}
}
}
class newUI extends UIScript{
button:StaleButton;
protected onStart() {
//Set whether onUpdate can be triggered every frame
this.canUpdate = false;
this.layer = UILayerMiddle;
this.button = StaleButton.newObject(this.rootCanvas);
this.button.text = "Press to turn red";
this.button.transitionEnable = true;
this.button.pressedImagColor = LinearColor.red;
this.button.visibility = SlateVisibility.Visible;
this.button.onClicked.add(() => {
console.log("click");
})
InputUtil.onKeyDown(Keys.P,()=>{
let ui = UIScript.getBehavior("UI");
ui(this.button);
});
}
}
@Component
export default class NewScript extends Script {
protected onStart(): void {
if(SystemUtil.isClient()){
//UIService.create(newUI);
UIService.show(newUI);
UIScript.addBehavior("UI",(ui:StaleButton)=>{
ui.text = "change";
});
}
}
}
class newUI extends UIScript{
button:StaleButton;
protected onStart() {
//Set whether onUpdate can be triggered every frame
this.canUpdate = false;
this.layer = UILayerMiddle;
this.button = StaleButton.newObject(this.rootCanvas);
this.button.text = "Press to turn red";
this.button.transitionEnable = true;
this.button.pressedImagColor = LinearColor.red;
this.button.visibility = SlateVisibility.Visible;
this.button.onClicked.add(() => {
console.log("click");
})
InputUtil.onKeyDown(Keys.P,()=>{
let ui = UIScript.getBehavior("UI");
ui(this.button);
});
}
}
removeBehavior
• Static
removeBehavior(key
): void
other
Remove a global behavior
Parameters
key string | Behavior tag range: The string length is not limited, as long as it is reasonable |
---|