Skip to content
SelectionUtil

Utils / SelectionUtil

SelectionUtil Class

Stroke drawing tool

Table of contents

Methods

drawGameObjectSelectionBox(StartPoint: Vector2, EndPoint: Vector2, Color: LinearColor, DurationTime?: number): void other
Draw an object selection box
getGameObjectBySelectionBox(StartPoint: Vector2, EndPoint: Vector2, IsIncludeNonCollidingObjects?: boolean, IsUseObjectsBoundingBox?: boolean): HitResult[] other
Retrieve the object at the selected screen position
setGlobalOutlineParams(Width?: number, CoveredAlpha?: number, CoveredEdgeAlpha?: number, NotCoveredAlpha?: number, NotCoveredEdgeAlpha?: number): void other
Set global outline parameters

Methods

drawGameObjectSelectionBox

Static drawGameObjectSelectionBox(StartPoint, EndPoint, Color, DurationTime?): void other

Draw an object selection box

Parameters

StartPoint Vector2Usage: mouse start position
EndPoint Vector2Usage: mouse end position
Color LinearColorUsage: Select box color
DurationTime? numberUsage: Display time default: 0.1 range: No restrictions type: Floating point number

Usage example: Create a script called SelectionExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, left click the mouse to select the object, and a selection box will be drawn. (Note that entering the game box selection may be affected by the UI and difficult to select. Please open the Default UI and check whether the TouchPadDesigner object property is controlled by the mouse to select false.)

ts
@Component
export default class SelectionExample extends Script {

    touchIndexesStart: Map<number, Vector2> = new Map<number, Vector2>();
    selectedGoes: Array<HitResult> = [];
    touch: TouchInput;

    async onStart() {
        this.touch = new TouchInput();
        await Player.asyncGetLocalPlayer();
        this.touch.onTouch.add((index, location, type) => {
            console.log("ontouch", index, location, type);
            if (type == TouchInputType.TouchBegin) {
                this.onTouchBegin(index, location);
            }
            else if (type == TouchInputType.TouchMove) {
                this.onTouchMove(index, location);
            }
        })
    }

    // Start touching the screen and record the initial position
    onTouchBegin(index: number, location: Vector2) {
        this.touchIndexesStart.set(index, location);
    }

    // Touch and move, draw a selection box
    onTouchMove(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        SelectionUtil.drawGameObjectSelectionBox(start, location, LinearColor.red, 0.03);
    }
}
@Component
export default class SelectionExample extends Script {

    touchIndexesStart: Map<number, Vector2> = new Map<number, Vector2>();
    selectedGoes: Array<HitResult> = [];
    touch: TouchInput;

    async onStart() {
        this.touch = new TouchInput();
        await Player.asyncGetLocalPlayer();
        this.touch.onTouch.add((index, location, type) => {
            console.log("ontouch", index, location, type);
            if (type == TouchInputType.TouchBegin) {
                this.onTouchBegin(index, location);
            }
            else if (type == TouchInputType.TouchMove) {
                this.onTouchMove(index, location);
            }
        })
    }

    // Start touching the screen and record the initial position
    onTouchBegin(index: number, location: Vector2) {
        this.touchIndexesStart.set(index, location);
    }

    // Touch and move, draw a selection box
    onTouchMove(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        SelectionUtil.drawGameObjectSelectionBox(start, location, LinearColor.red, 0.03);
    }
}

getGameObjectBySelectionBox

Static getGameObjectBySelectionBox(StartPoint, EndPoint, IsIncludeNonCollidingObjects?, IsUseObjectsBoundingBox?): HitResult[] other

Retrieve the object at the selected screen position

Parameters

StartPoint Vector2Usage: mouse start position
EndPoint Vector2Usage: mouse end position
IsIncludeNonCollidingObjects? booleanUsage: whether to include non collision component of objects default: false
IsUseObjectsBoundingBox? booleanUsage: whether to use the object bounding box default: false

Returns

HitResult[]Selected objects

Usage example

ts
@Component
export default class SelectionExample extends Script {

    touchIndexesStart: Map<number, Vector2> = new Map<number, Vector2>();
    selectedGoes: Array<HitResult> = [];
    touch: TouchInput;

    async onStart() {
        this.creatObjs();
        this.touch = new TouchInput();
        await Player.asyncGetLocalPlayer();
        this.touch.onTouch.add((index, location, type) => {
            console.log("ontouch", index, location, type);
            if (type == TouchInputType.TouchBegin) {
                this.onTouchBegin(index, location);
            } else if (type == TouchInputType.TouchMove) {
                this.onTouchMove(index, location);
            } else if (type == TouchInputType.TouchEnd) {
                this.onTouchEnd(index, location);
            }
        })
    }

    //Randomly generate some objects in the scene for frame selection
    private creatObjs() {
        const cubeAssetId = "197386";
        for (let i = 0;
i < 50;
i++) {
            GameObject.asyncSpawn<Model>(cubeAssetId).then(obj => {
                obj.worldTransform.position = new Vector(MathUtil.randomInt(-500, 500), MathUtil.randomInt(-500, 500), MathUtil.randomInt(-500, 500));
            })
        }
    }

    // Start touching the screen and record the initial position
    private onTouchBegin(index: number, location: Vector2) {
        this.touchIndexesStart.set(index, location);
    }

    // Touch and move, draw a selection box
    private onTouchMove(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        SelectionUtil.drawGameObjectSelectionBox(start, location, LinearColor.red, 0.03);
    }

    // Touch completed, box selected object
    private onTouchEnd(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        // Cancel the outline of the last frame selected object
        this.selectedGoes.forEach(result => {
            let mesh = result.gameObject as Model;
            mesh.setPostProcessOutline(false,LinearColor.green, 1);
        })
        // Box selection object
        this.selectedGoes = SelectionUtil.getGameObjectBySelectionBox(start, location, false, false).filter(result => (result.gameObject instanceof StaticMesh));
        // Add strokes to unselected objects
        this.selectedGoes.forEach(result => {
            let mesh = result.gameObject as Model;
            mesh.setPostProcessOutline(true,LinearColor.red, 1);
        })
        SelectionUtil.setGlobalOutlineParams(4, 0, 0, 0, 1);
    }

}
@Component
export default class SelectionExample extends Script {

    touchIndexesStart: Map<number, Vector2> = new Map<number, Vector2>();
    selectedGoes: Array<HitResult> = [];
    touch: TouchInput;

    async onStart() {
        this.creatObjs();
        this.touch = new TouchInput();
        await Player.asyncGetLocalPlayer();
        this.touch.onTouch.add((index, location, type) => {
            console.log("ontouch", index, location, type);
            if (type == TouchInputType.TouchBegin) {
                this.onTouchBegin(index, location);
            } else if (type == TouchInputType.TouchMove) {
                this.onTouchMove(index, location);
            } else if (type == TouchInputType.TouchEnd) {
                this.onTouchEnd(index, location);
            }
        })
    }

    //Randomly generate some objects in the scene for frame selection
    private creatObjs() {
        const cubeAssetId = "197386";
        for (let i = 0;
i < 50;
i++) {
            GameObject.asyncSpawn<Model>(cubeAssetId).then(obj => {
                obj.worldTransform.position = new Vector(MathUtil.randomInt(-500, 500), MathUtil.randomInt(-500, 500), MathUtil.randomInt(-500, 500));
            })
        }
    }

    // Start touching the screen and record the initial position
    private onTouchBegin(index: number, location: Vector2) {
        this.touchIndexesStart.set(index, location);
    }

    // Touch and move, draw a selection box
    private onTouchMove(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        SelectionUtil.drawGameObjectSelectionBox(start, location, LinearColor.red, 0.03);
    }

    // Touch completed, box selected object
    private onTouchEnd(index: number, location: Vector2) {
        let start = this.touchIndexesStart.get(index);
        if (!start) { return;
}
        // Cancel the outline of the last frame selected object
        this.selectedGoes.forEach(result => {
            let mesh = result.gameObject as Model;
            mesh.setPostProcessOutline(false,LinearColor.green, 1);
        })
        // Box selection object
        this.selectedGoes = SelectionUtil.getGameObjectBySelectionBox(start, location, false, false).filter(result => (result.gameObject instanceof StaticMesh));
        // Add strokes to unselected objects
        this.selectedGoes.forEach(result => {
            let mesh = result.gameObject as Model;
            mesh.setPostProcessOutline(true,LinearColor.red, 1);
        })
        SelectionUtil.setGlobalOutlineParams(4, 0, 0, 0, 1);
    }

}

setGlobalOutlineParams

Static setGlobalOutlineParams(Width?, CoveredAlpha?, CoveredEdgeAlpha?, NotCoveredAlpha?, NotCoveredEdgeAlpha?): void other

Set global outline parameters

Parameters

Width? numberUsage: stroke width default: 2 range: [0,4] The larger the value, the larger the stroke width. Type: floating point number
CoveredAlpha? numberUsage: Illuminated parts highlight transparency default: 0 range: [0,1] The larger the value, the more opaque it is. Type: Floating point number
CoveredEdgeAlpha? numberUsage: outline transparency of the occluded part default: 1 range: [0, 1] The greater the value, the more opaque type: floating point
NotCoveredAlpha? numberUsage: Unobstructed parts highlight transparency default: 0 range: [0,1] The larger the value, the more opaque it is. Type: Floating point number
NotCoveredEdgeAlpha? numberUsage: Unobstructed part of stroke transparency default: 1 range: [0,1] The larger the value, the more opaque it is. Type: Floating point number