AudioSourcePool.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using Fort23.Core;
  3. using UnityEngine;
  4. namespace Core.Audio
  5. {
  6. public class AudioSourcePool : IDisposable
  7. {
  8. public object TimeLineAudioEventLogic;
  9. private AudioSource _audioSource;
  10. private TimerEntity _timerEntity;
  11. // private AssetHandle _assetHandle;
  12. public bool IsPlay;
  13. public System.Action OnFinish;
  14. private float maxTime;
  15. public string CurrPlayName
  16. {
  17. get { return _currPlayName; }
  18. }
  19. public string _currPlayName;
  20. public float AudioVolume
  21. {
  22. get { return _audioVolume; }
  23. }
  24. private float _audioVolume;
  25. public bool IsBgm;
  26. public void Init(Transform root)
  27. {
  28. GameObject gameObject = new GameObject();
  29. _audioSource = gameObject.AddComponent<AudioSource>();
  30. gameObject.transform.SetParent(root);
  31. }
  32. public void Play(string audioName, bool isBgm, AssetHandle audioClip, bool isLoop, float volume,
  33. float speed = 1)
  34. {
  35. if (audioClip == null)
  36. {
  37. return;
  38. }
  39. // if (_assetHandle != null)
  40. // {
  41. // _assetHandle.Release();
  42. // }
  43. this.IsBgm = isBgm;
  44. IsPlay = true;
  45. _currPlayName = audioName;
  46. // _assetHandle = audioClip;
  47. _audioSource.clip = audioClip.AssetObject<AudioClip>();
  48. _audioSource.loop = isLoop;
  49. _audioSource.pitch = speed;
  50. _audioVolume = volume;
  51. Setvolume();
  52. if (speed > 1)
  53. {
  54. _audioSource.outputAudioMixerGroup = AudioManager.Instance.AudioMixerGroup;
  55. _audioSource.outputAudioMixerGroup.audioMixer.SetFloat("path", 1f / speed);
  56. }
  57. else
  58. {
  59. _audioSource.outputAudioMixerGroup = null;
  60. }
  61. _audioSource.Play();
  62. maxTime = _audioSource.clip.length;
  63. if (!isLoop)
  64. {
  65. _timerEntity = TimerComponent.Instance.AddTimer((long)(maxTime * 1000L),
  66. delegate { Finish(); });
  67. }
  68. }
  69. public void Setvolume(float volume)
  70. {
  71. _audioVolume = volume;
  72. if (_audioVolume < 0)
  73. {
  74. return;
  75. }
  76. Setvolume();
  77. }
  78. public void Setvolume()
  79. {
  80. _audioSource.volume = _audioVolume *
  81. (IsBgm ? AudioManager.Instance.BgmVolume : AudioManager.Instance.AudionVolume);
  82. }
  83. public float Progress()
  84. {
  85. return _audioSource.time / maxTime;
  86. }
  87. public void Finish()
  88. {
  89. IsPlay = false;
  90. OnFinish?.Invoke();
  91. _audioSource.Stop();
  92. AudioManager.Instance.Recycle(this);
  93. TimerComponent.Instance.Remove(_timerEntity);
  94. _timerEntity = null;
  95. }
  96. public void Stop()
  97. {
  98. IsPlay = false;
  99. _audioSource.Stop();
  100. }
  101. public void Pause()
  102. {
  103. _audioSource.Pause();
  104. }
  105. public void UnPause()
  106. {
  107. _audioSource.UnPause();
  108. }
  109. public void Dispose()
  110. {
  111. }
  112. }
  113. }