Skip to content
Sound

Scene / Sound

Sound Class

Sound effects

Example usage: Create a script called "SoundExample", place it in the object bar, open the script, enter the following code to save, run the game, and you can hear the sound effects. The code is as follows:

ts
@Component
export default class SoundExample extends Script {

    private readonly sound = {
        assetID: "14929",
        object: null as mw.Sound,
    };

    protected onStart(): void {
        this.createSound();
    }

    @mw.RemoteFunction(mw.Client)
    public async createSound(): Promise<void> {
        const success = await AssetUtil.asyncDownloadAsset(this.sound.assetID);
            if (success) {
                // Download completed, create sound effects
                this.sound.object = await GameObject.asyncSpawn<mw.Sound>(this.sound.assetID);

                // Set sound effect transform
                const transform = new Transform(new Vector(0, 0, 0), new Rotation(0, 0, 0), new Vector(1, 1, 1));
                this.sound.object.worldTransform = transform;

                // Set the sound effect to spatial sound effect
                this.sound.object.isUISound = false;
                this.sound.object.isSpatialization = true;
                // Set the UI sound effect shape to spherical
                this.sound.object.attenuationShape = AttenuationShape.Sphere;
                // Set sound effect range to 100
                this.sound.object.attenuationShapeExtents = new Vector(100,0,0);
                // Set the sound effect attenuation distance to 200
                this.sound.object.falloffDistance = 200;
                // Set sound effect volume
                this.sound.object.volume = 1;
                // Activate sound loop
                this.sound.object.isLoop = true;
                // Play sound effects
                this.sound.object.play();
            }
    }
}
@Component
export default class SoundExample extends Script {

    private readonly sound = {
        assetID: "14929",
        object: null as mw.Sound,
    };

    protected onStart(): void {
        this.createSound();
    }

    @mw.RemoteFunction(mw.Client)
    public async createSound(): Promise<void> {
        const success = await AssetUtil.asyncDownloadAsset(this.sound.assetID);
            if (success) {
                // Download completed, create sound effects
                this.sound.object = await GameObject.asyncSpawn<mw.Sound>(this.sound.assetID);

                // Set sound effect transform
                const transform = new Transform(new Vector(0, 0, 0), new Rotation(0, 0, 0), new Vector(1, 1, 1));
                this.sound.object.worldTransform = transform;

                // Set the sound effect to spatial sound effect
                this.sound.object.isUISound = false;
                this.sound.object.isSpatialization = true;
                // Set the UI sound effect shape to spherical
                this.sound.object.attenuationShape = AttenuationShape.Sphere;
                // Set sound effect range to 100
                this.sound.object.attenuationShapeExtents = new Vector(100,0,0);
                // Set the sound effect attenuation distance to 200
                this.sound.object.falloffDistance = 200;
                // Set sound effect volume
                this.sound.object.volume = 1;
                // Activate sound loop
                this.sound.object.isLoop = true;
                // Play sound effects
                this.sound.object.play();
            }
    }
}

Hierarchy

Table of contents

Properties

onFinish: MulticastDelegate<() => void>
Sound End Event
onPause: MulticastDelegate<() => void>
Sound pause event
onPlay: MulticastDelegate<() => void>
Sound Start Event
click

Properties

onBeforeDestroyDelegate: MulticastDelegate<() => void> other
Event callback before object destruction
onCustomPropertyChange: Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other
Monitor custom attribute synchronization events
onDestroyDelegate: MulticastDelegate<() => void> other
Event callback after object destruction
onPropertyChange: Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>>
Monitor system replicated events

Accessors

attenuationDistanceModel(): AttenuationDistanceModel other
Attenuation method for obtaining sound effect
attenuationShape(): AttenuationShape other
Obtain the shape of the sound effect
attenuationShapeExtents(): Vector other
Obtain the range of sound effect shapes
falloffDistance(): number other
Get attenuation distance
isLoop(): boolean other
Get whether to loop play or not
isSpatialization(): boolean other
Get sound effects spatialization
isUISound(): boolean other
Get whether it is a UI sound effect
playState(): SoundPlayState other
Get sound effect playback status
timeLength(): number other
Duration of obtaining sound effect
timePosition(): number other
Get the current played duration
volume(): number other
Get volume ratio
click

Accessors

actorFlagValue(): number other
Get object tags
actorLevel(): number other
Obtain Actor Level
assetId(): string other
Retrieve the UID of the resource currently being used by the object
gameObjectId(): string other
Get the unique identification of an object (a string that uniquely identifies an object).
isDestroyed(): boolean other
Has the current object been destroyed
isReady(): boolean other
Current object status
localTransform(): Transform other
Local transformation of the current object
name(): string other
Return the current object name
netStatus(): NetStatus other
Get the current object synchronization status
parent(): GameObject other
Retrieve the current parent object
tag(): string other
Get the tag of the current object
worldTransform(): Transform other
Current object world transformation

Methods

pause(bPause?: boolean): void other
Pause playing sound effects
play(startTime?: number, onSuccess?: () => void): void other
Play sound effects
setSoundAsset(assetGUID: string): void other
Set sound effects through UUID
stop(): void other
Stop playing sound effects
click

Methods

addComponent<T: extends Script<T>>(constructor: (...args: unknown[]) => T: extends Script<T>, bInReplicates?: boolean): T: extends Script<T> other
Add a script component
asyncGetChildByName(name: string): Promise<GameObject> other
Asynchronous search for sub objects based on their names
asyncReady(): Promise<GameObject> other
Return after the object is ready
clone(gameObjectInfo?: GameObjectInfo): GameObject other
Copy Objects
destroy(): void other
delete object
getBoundingBox(nonColliding?: boolean, includeFromChild?: boolean, outer?: Vector): Vector other
Get the size of the object's bounding box
getBounds(onlyCollidingComponents: boolean, originOuter: Vector, boxExtentOuter: Vector, includeFromChild?: boolean): void other
Obtain object boundaries
getChildByGameObjectId(gameObjectId: string): GameObject other
Search for sub objects based on gameObjectid
getChildByName(name: string): GameObject other
Search for sub objects by name
getChildByPath(path: string): GameObject other
Find sub objects according to path
getChildren(): GameObject[] other
Obtain sub objects
getChildrenBoundingBoxCenter(outer?: Vector): Vector other
Get the center point of all child objects' bounding box (not including the parent object, if the parent object is unavailable, return [0,0,0])
getChildrenByName(name: string): GameObject[] other
Search for all sub objects by name
getComponent<T: extends Script<T>>(constructor?: (...args: unknown[]) => T: extends Script<T>): T: extends Script<T> other
Get the component of the specified type
getComponentPropertys<T: extends Script<T>>(constructor: (...args: unknown[]) => T: extends Script<T>): Map<string, IPropertyOptions> other
Get property of script component
getComponents<T: extends Script<T>>(constructor?: (...args: unknown[]) => T: extends Script<T>): T: extends Script<T>[] other
Get all component of the specified type
getCustomProperties(): string[] other
Get all customize property
getCustomProperty<T: extends CustomPropertyType>(propertyName: string): T: extends CustomPropertyType other
Get customize property
getCustomPropertyChangeDelegate(property): Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other
Event proxy triggered when a given object property is modified
getPropertyChangeDelegate(property): Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other
Event proxy triggered when a given object property is modified
getVisibility(): boolean other
Obtain whether the object is displayed
isPrefabActor(): boolean other
Return whether the current object is a prefabricated body
moveBy(velocity: Vector, isLocal?: boolean): void other
Smoothly move an object over time according to a given velocity vector
moveTo(targetPosition: Vector, time: number, isLocal?: boolean, onComplete?: () => void): void other
Smoothly move from the current position to the target position within the specified time
rotateBy(rotation: Quaternion Rotation, multiplier: number, isLocal?: boolean): void other
Rotate the object smoothly over time according to the given rotation amount
rotateTo(targetRotation: Quaternion Rotation, time: number, isLocal?: boolean, onComplete?: () => void): void other
Smoothly changes from the current rotation to the target rotation within the specified time
scaleBy(scale: Vector, isLocal?: boolean): void other
Smoothly scale objects over time using a given scaling vector per second
scaleTo(targetScale: Vector, time: number, isLocal?: boolean, onComplete?: () => void): void other
Smoothly changes from the current scale to the target scale within the specified time
setAbsolute(absolutePosition?: boolean, absoluteRotation?: boolean, absoluteScale?: boolean): void other
Set whether the object localTransform is relative to the parent object or the world
setCustomProperty(propertyName: string, value: undefined CustomPropertyType): void other
Set custom attributes
setVisibility(status: boolean PropertyStatus, propagateToChildren?: boolean): void other
Set whether the object is displayed
stopMove(): void other
Interrupt further movement of moveTo() and moveBy()
stopRotate(): void other
Interrupt further rotation from rotateTo() or rotateBy()
stopScale(): void other
Interrupt further scale from ScaleTo() or ScaleBy()
asyncFindGameObjectById(gameObjectId: string): Promise<GameObject> other
Asynchronous search for GameObject through gameObjectid
asyncGetGameObjectByPath(path: string): Promise<GameObject> other
Asynchronously find objects through path
asyncSpawn<T: extends GameObject<T>>(assetId: string, gameObjectInfo?: GameObjectInfo): Promise<T: extends GameObject<T>> other
Asynchronous construction of an object
bulkPivotTo(gameObjects: GameObject[], transforms: Transform[]): void other
Batch set position
findGameObjectById(gameObjectId: string): GameObject other
Search for objects through gameObjectid
findGameObjectByName(name: string): GameObject other
Search for objects by name
findGameObjectsByName(name: string): GameObject[] other
Search for objects by name
findGameObjectsByTag(tag: string): GameObject[] other
Get objects through customize tag
getGameObjectByPath(path: string): GameObject other
Search for objects through paths
spawn<T: extends GameObject<T>>(assetId: string, gameObjectInfo?: GameObjectInfo): T: extends GameObject<T> other
Construct an object

Properties


onFinish

onFinish: MulticastDelegate<() => void>

Sound End Event


onPause

onPause: MulticastDelegate<() => void>

Sound pause event


onPlay

onPlay: MulticastDelegate<() => void>

Sound Start Event

Accessors


attenuationDistanceModel

get attenuationDistanceModel(): AttenuationDistanceModel other

set attenuationDistanceModel(model): void other

Attenuation method for obtaining sound effect

Returns

AttenuationDistanceModelAttenuation method

Set the attenuation method for sound effects

Parameters

modelAttenuationDistanceModel

attenuationShape

get attenuationShape(): AttenuationShape other

set attenuationShape(shape): void other

Obtain the shape of the sound effect

Returns

AttenuationShapeshape

Set the shape of sound effects

Parameters

shapeAttenuationShape

attenuationShapeExtents

get attenuationShapeExtents(): Vector other

set attenuationShapeExtents(ShapeExtents): void other

Obtain the range of sound effect shapes

Returns

VectorSound effect range

Set the sound effect shape range to use for the size of the attenuation shape. The value of each shape is interpreted differently.

Sphere X is the radius of the sphere, Y and Z are not used

Capsule X is half height, Y is radius, Z is not used

X, Y, and Z are the dimensions of a rectangular prism

Cone X is the radius of the cone, Y is the angle of the cone, and Z is the attenuation angle of the cone

Parameters

ShapeExtents VectorUsage: Set shape range

falloffDistance

get falloffDistance(): number other

set falloffDistance(falloffDistance): void other

Get attenuation distance

Returns

numberdistance

Set attenuation Distance

Parameters

falloffDistance numberUsage: Distance

isLoop

get isLoop(): boolean other

set isLoop(Loop): void other

Get whether to loop play or not

Returns

booleanIs it a loop

Set loop playback

Parameters

Loop booleanUsage: Set whether to enable loop

isSpatialization

get isSpatialization(): boolean other

set isSpatialization(spatialization): void other

Get sound effects spatialization

Returns

booleanWhether to enable spatialization of sound effect

Set the sound effect spatialization. If yes, turn on the spatial propagation attenuation

Parameters

spatialization booleanUsage: set to enable spatialization of sound effect

isUISound

get isUISound(): boolean other

set isUISound(isUISound): void other

Get whether it is a UI sound effect

Returns

booleanboolean

Set whether it is UI sound effect, note: UI sound effect does not depend on game logic

Parameters

isUISound booleanUsage: Is it used for UI

playState

get playState(): SoundPlayState other

Get sound effect playback status

Returns

SoundPlayStateIs it playing

timeLength

get timeLength(): number other

Duration of obtaining sound effect

Returns

numberSound effect duration (ms)

timePosition

get timePosition(): number other

Get the current played duration

Returns

numberDuration Played

volume

get volume(): number other

set volume(volume): void other

Get volume ratio

Returns

numberVolume ratio

Set volume to 0~1

Parameters

volume numberUsage: volume ratio

Methods


pause

pause(bPause?): void other

Pause playing sound effects

Parameters

bPause? booleanUsage: Set pause status default: true

play

play(startTime?, onSuccess?): void other

Play sound effects

Parameters

startTime? numberUsage: set the start playback time default: 0 range: not greater than the duration of the sound effect type: floating point number
onSuccess? () => voidUsage: callback after playback completion default: null

setSoundAsset

setSoundAsset(assetGUID): void other

Set sound effects through UUID

Parameters

assetGUID stringUsage: Sound resource ID range: Depending on the length of the ID

stop

stop(): void other

Stop playing sound effects