2022-12-20 10:00:47 +01:00

31 lines
880 B
TypeScript

import { AudioClip, AudioSource, Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("AudioPlayer")
export class AudioPlayer extends Component {
@property(AudioSource) private soundSource: AudioSource;
@property(AudioSource) private musicSource: AudioSource;
public init(soundVolume: number, musicVolume: number): void {
this.setSoundVolume(soundVolume);
this.setMusicVolume(musicVolume);
}
public setSoundVolume(volume: number): void {
this.soundSource.volume = volume;
}
public setMusicVolume(volume: number): void {
this.musicSource.volume = volume;
}
public playSound(clip: AudioClip): void {
this.soundSource.playOneShot(clip);
}
public playMusic(clip: AudioClip): void {
this.musicSource.clip = clip;
this.musicSource.play();
}
}