Utils / ScreenUtil
ScreenUtil Class
Screen viewport Tools
Table of contents
Methods
checkWidgetAt(screenPosition : Vector2 ): boolean other |
---|
Obtain whether there is a UI at the screen coordinate point. Note that this method has poor performance and should not be called frequently |
getGameObjectByScreenPosition(sceneX : number , sceneY : number , distance? : number , multiTrace? : boolean , onRay? : boolean ): HitResult [] other |
Obtain the object at the corresponding position of the viewport |
getSightBeadPosition(): Vector other |
Obtain the world position aim by the camera center point |
getWidgetAt(screenPosition : Vector2 ): Widget other |
Get the UI at the screen coordinate point. Note that the performance of this method is too poor. Do not call it frequently |
projectWorldPositionToWidgetPosition(player : Player , worldLocation : Vector , outScreenPosition : Vector2 , isPlayerViewportRelative : boolean ): boolean other |
Get the screen position of the projected world to the player, and then convert it to the widget position, taking into account any quality scale. |
Methods
checkWidgetAt
• Static
checkWidgetAt(screenPosition
): boolean
other
Obtain whether there is a UI at the screen coordinate point. Note that this method has poor performance and should not be called frequently
Parameters
screenPosition Vector2 | Usage: screen coordinate point |
---|
Returns
boolean | Is there a UI at this location |
---|
getGameObjectByScreenPosition
• Static
getGameObjectByScreenPosition(sceneX
, sceneY
, distance?
, multiTrace?
, onRay?
): HitResult
[] other
Obtain the object at the corresponding position of the viewport
Parameters
sceneX number | Usage: viewport coordinates X range: no more than screen coordinates. For details of screen coordinates, see class Vector2 type: floating point |
---|---|
sceneY number | Usage: viewport coordinate Y range: no more than screen coordinate. For details of screen coordinates, see class Vector2 type: floating point |
distance? number | Usage: detect distance default: 100000 range: unlimited, type: floating point |
multiTrace? boolean | Usage: Get multiple GameObjects default: false |
onRay? boolean | Usage: Enable ray display effect default: false |
Returns
HitResult [] | Click on the object at the location |
---|
Usage example: create a script named ScreenExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and obtain the world object corresponding to the position of the touch
@Component
export default class ScreenExample extends Script {
touch: TouchInput;
async onStart() {
this.touch = new TouchInput();
this.touch.onTouch.add((index, location, type) => {
console.log("ontouch", index, location, type);
if (type == TouchInputType.TouchBegin) {
Console.log ("The name of the GameObject touched is:"+this. onTouchBegin (index, location). name);
}
})
}
// Start touching the screen from this position
private onTouchBegin(index: number, location: Vector2): mw.GameObject{
return ScreenUtil.getGameObjectByScreenPosition(location.x, location.y)[0].gameObject;
}
}
@Component
export default class ScreenExample extends Script {
touch: TouchInput;
async onStart() {
this.touch = new TouchInput();
this.touch.onTouch.add((index, location, type) => {
console.log("ontouch", index, location, type);
if (type == TouchInputType.TouchBegin) {
Console.log ("The name of the GameObject touched is:"+this. onTouchBegin (index, location). name);
}
})
}
// Start touching the screen from this position
private onTouchBegin(index: number, location: Vector2): mw.GameObject{
return ScreenUtil.getGameObjectByScreenPosition(location.x, location.y)[0].gameObject;
}
}
getSightBeadPosition
• Static
getSightBeadPosition(): Vector
other
Obtain the world position aim by the camera center point
Returns
Vector | Target point world position |
---|
Usage example: create a script named ScreenExample, place it in the object bar, open the script, modify the original content to the following content, save and run the game, and get the world position corresponding to the viewport center point when your finger moves
@Component
export default class ScreenExample extends Script {
touch: TouchInput;
async onStart() {
this.touch = new TouchInput();
this.touch.onTouchMove.add((index, location, type) => {
console.log("ontouch", index, location, type);
if (type == TouchInputType.TouchMove) {
Console.log ("The world position corresponding to the viewport center point is:"+getSightBeadPosition());
}
})
}
___
### getWidgetAt <Score text="getWidgetAt" />
• `Static` **getWidgetAt**(`screenPosition`): [`Widget`](mw.Widget.md) <Badge type="tip" text="other" />
Get the UI at the screen coordinate point. Note that the performance of this method is too poor. Do not call it frequently
#### Parameters
| `screenPosition` [`Vector2`](mw.Vector2.md) | Usage: screen coordinate point |
| :------ | :------ |
#### Returns
| [`Widget`](mw.Widget.md) | Return the UI of this point, if it does not return empty |
| :------ | :------ |
___
### projectWorldPositionToWidgetPosition <Score text="projectWorldPositionToWidgetPosition" />
• `Static` **projectWorldPositionToWidgetPosition**(`player`, `worldLocation`, `outScreenPosition`, `isPlayerViewportRelative`): `boolean` <Badge type="tip" text="other" />
Get the screen position of the projected world to the player, and then convert it to the widget position, taking into account any quality scale.
#### Parameters
| `player` [`Player`](mw.Player.md) | Usage: Player's position in the world of the game is projected to the controller on the screen |
| :------ | :------ |
| `worldLocation` [`Vector`](mw.Vector.md) | Usage: World position to be projected |
| `outScreenPosition` [`Vector2`](mw.Vector2.md) | Usage: position in viewport |
| `isPlayerViewportRelative` `boolean` | Usage: Whether it is related to the Player's viewport sub area (useful when using Player's additional widgets in split screen or when the width ratio is limited) If the position is projected onto the screen, return true |
#### Returns
| `boolean` | boolean |
| :------ | :------ |
@Component
export default class ScreenExample extends Script {
touch: TouchInput;
async onStart() {
this.touch = new TouchInput();
this.touch.onTouchMove.add((index, location, type) => {
console.log("ontouch", index, location, type);
if (type == TouchInputType.TouchMove) {
Console.log ("The world position corresponding to the viewport center point is:"+getSightBeadPosition());
}
})
}
___
### getWidgetAt <Score text="getWidgetAt" />
• `Static` **getWidgetAt**(`screenPosition`): [`Widget`](mw.Widget.md) <Badge type="tip" text="other" />
Get the UI at the screen coordinate point. Note that the performance of this method is too poor. Do not call it frequently
#### Parameters
| `screenPosition` [`Vector2`](mw.Vector2.md) | Usage: screen coordinate point |
| :------ | :------ |
#### Returns
| [`Widget`](mw.Widget.md) | Return the UI of this point, if it does not return empty |
| :------ | :------ |
___
### projectWorldPositionToWidgetPosition <Score text="projectWorldPositionToWidgetPosition" />
• `Static` **projectWorldPositionToWidgetPosition**(`player`, `worldLocation`, `outScreenPosition`, `isPlayerViewportRelative`): `boolean` <Badge type="tip" text="other" />
Get the screen position of the projected world to the player, and then convert it to the widget position, taking into account any quality scale.
#### Parameters
| `player` [`Player`](mw.Player.md) | Usage: Player's position in the world of the game is projected to the controller on the screen |
| :------ | :------ |
| `worldLocation` [`Vector`](mw.Vector.md) | Usage: World position to be projected |
| `outScreenPosition` [`Vector2`](mw.Vector2.md) | Usage: position in viewport |
| `isPlayerViewportRelative` `boolean` | Usage: Whether it is related to the Player's viewport sub area (useful when using Player's additional widgets in split screen or when the width ratio is limited) If the position is projected onto the screen, return true |
#### Returns
| `boolean` | boolean |
| :------ | :------ |