| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import { AudioClip, AudioSource, Component, director, System, SystemEventType, _decorator } from "cc";
- import { SoundSystem } from "./SoundSystem";
- const { ccclass, property } = _decorator;
- /**
- * 声音组件
- * @author 袁浩
- */
- @ccclass("Sound")
- export class Sound extends Component {
- @property({ tooltip: "声音名称,如果不填,则使用uuid" })
- public soundName: string = "";
- @property({ tooltip: "音源", type: [AudioClip] })
- public clips: AudioClip[] = [];
- @property({ visible: false })
- private _volume: number = 1;
- @property({ tooltip: "音量", min: 0, max: 1, step: 0.1 })
- public get volume(): number {
- return this._volume;
- }
- public set volume(value: number) {
- this.audioSource.volume = value;
- this._volume = value;
- }
- @property({ tooltip: "是否在声音组件运行时自动播放" })
- public playOnLoad: boolean = true;
- @property({ tooltip: "是否在触摸节点时播放" })
- public playOnTouch: boolean = false;
- @property({ tooltip: "播放时是否不覆盖前一个声音" })
- public oneShot: boolean = true;
- @property({
- tooltip: "是否循环播放", visible() {
- return !this.oneShot;
- }
- })
- public loop: boolean = false;
- private get audioSource(): AudioSource {
- let audioSource = this.node.getComponent(AudioSource) || this.node.addComponent(AudioSource);
- audioSource.playOnAwake = false;
- return audioSource;
- }
- onLoad() {
- this.initSystem();
- }
- start() {
- this.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
- this.playOnLoad && this.play();
- }
- initSystem() {
- this.soundName = this.soundName || this.uuid;
- let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
- if (!soundSystem) {
- soundSystem = new SoundSystem();
- director.registerSystem(SoundSystem.ID, soundSystem, 0);
- }
- soundSystem.regedit(this);
- }
- onDestroy() {
- let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
- soundSystem.unregedit(this);
- }
- private onTouchEnd() {
- this.playOnTouch && this.play();
- }
- public play(index: number = 0) {
- let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
- // this.clips[index].setLoop(this.loop);
- this.audioSource.loop = this.loop;
- if (this.oneShot) {
- // this.clips[index].playOneShot(this.volume * soundSystem.effectVolumeScale);
- this.audioSource.playOneShot(this.clips[index], this.volume * soundSystem.effectVolumeScale);
- }
- else {
- this.audioSource.stop();
- // this.clips[index].setVolume(this.volume * soundSystem.musicVolumeScale);
- // this.clips[index].play();
- this.audioSource.volume = this.volume * soundSystem.musicVolumeScale;
- this.audioSource.clip = this.clips[index];
- this.audioSource.play();
- }
- }
- public stop(index: number = 0) {
- // this.clips[index].stop();
- this.audioSource.stop();
- }
- public stopAll() {
- this.audioSource.stop();
- }
- public setVolumeAll() {
- let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
- this.audioSource.volume = this.volume * soundSystem.musicVolumeScale;
- }
- }
|