Sound.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { AudioClip, AudioSource, Component, director, System, SystemEventType, _decorator } from "cc";
  2. import { SoundSystem } from "./SoundSystem";
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 声音组件
  6. * @author 袁浩
  7. */
  8. @ccclass("Sound")
  9. export class Sound extends Component {
  10. @property({ tooltip: "声音名称,如果不填,则使用uuid" })
  11. public soundName: string = "";
  12. @property({ tooltip: "音源", type: [AudioClip] })
  13. public clips: AudioClip[] = [];
  14. @property({ visible: false })
  15. private _volume: number = 1;
  16. @property({ tooltip: "音量", min: 0, max: 1, step: 0.1 })
  17. public get volume(): number {
  18. return this._volume;
  19. }
  20. public set volume(value: number) {
  21. this.audioSource.volume = value;
  22. this._volume = value;
  23. }
  24. @property({ tooltip: "是否在声音组件运行时自动播放" })
  25. public playOnLoad: boolean = true;
  26. @property({ tooltip: "是否在触摸节点时播放" })
  27. public playOnTouch: boolean = false;
  28. @property({ tooltip: "播放时是否不覆盖前一个声音" })
  29. public oneShot: boolean = true;
  30. @property({
  31. tooltip: "是否循环播放", visible() {
  32. return !this.oneShot;
  33. }
  34. })
  35. public loop: boolean = false;
  36. private get audioSource(): AudioSource {
  37. let audioSource = this.node.getComponent(AudioSource) || this.node.addComponent(AudioSource);
  38. audioSource.playOnAwake = false;
  39. return audioSource;
  40. }
  41. onLoad() {
  42. this.initSystem();
  43. }
  44. start() {
  45. this.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  46. this.playOnLoad && this.play();
  47. }
  48. initSystem() {
  49. this.soundName = this.soundName || this.uuid;
  50. let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
  51. if (!soundSystem) {
  52. soundSystem = new SoundSystem();
  53. director.registerSystem(SoundSystem.ID, soundSystem, 0);
  54. }
  55. soundSystem.regedit(this);
  56. }
  57. onDestroy() {
  58. let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
  59. soundSystem.unregedit(this);
  60. }
  61. private onTouchEnd() {
  62. this.playOnTouch && this.play();
  63. }
  64. public play(index: number = 0) {
  65. let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
  66. // this.clips[index].setLoop(this.loop);
  67. this.audioSource.loop = this.loop;
  68. if (this.oneShot) {
  69. // this.clips[index].playOneShot(this.volume * soundSystem.effectVolumeScale);
  70. this.audioSource.playOneShot(this.clips[index], this.volume * soundSystem.effectVolumeScale);
  71. }
  72. else {
  73. this.audioSource.stop();
  74. // this.clips[index].setVolume(this.volume * soundSystem.musicVolumeScale);
  75. // this.clips[index].play();
  76. this.audioSource.volume = this.volume * soundSystem.musicVolumeScale;
  77. this.audioSource.clip = this.clips[index];
  78. this.audioSource.play();
  79. }
  80. }
  81. public stop(index: number = 0) {
  82. // this.clips[index].stop();
  83. this.audioSource.stop();
  84. }
  85. public stopAll() {
  86. this.audioSource.stop();
  87. }
  88. public setVolumeAll() {
  89. let soundSystem = director.getSystem(SoundSystem.ID) as SoundSystem;
  90. this.audioSource.volume = this.volume * soundSystem.musicVolumeScale;
  91. }
  92. }