TweenVolume.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(AudioSource))]
  4. [AddComponentMenu("NGUI/Tween/Tween Volume")]
  5. public class TweenVolume : UITweener
  6. {
  7. public AudioSource audioSource
  8. {
  9. get
  10. {
  11. if (this.mSource == null)
  12. {
  13. this.mSource = base.GetComponent<AudioSource>();
  14. if (this.mSource == null)
  15. {
  16. this.mSource = base.GetComponent<AudioSource>();
  17. if (this.mSource == null)
  18. {
  19. Debug.LogError("TweenVolume needs an AudioSource to work with", this);
  20. base.enabled = false;
  21. }
  22. }
  23. }
  24. return this.mSource;
  25. }
  26. }
  27. [Obsolete("Use 'value' instead")]
  28. public float volume
  29. {
  30. get
  31. {
  32. return this.value;
  33. }
  34. set
  35. {
  36. this.value = value;
  37. }
  38. }
  39. public float value
  40. {
  41. get
  42. {
  43. return (!(this.audioSource != null)) ? 0f : this.mSource.volume;
  44. }
  45. set
  46. {
  47. if (this.audioSource != null)
  48. {
  49. this.mSource.volume = value;
  50. }
  51. }
  52. }
  53. protected override void OnUpdate(float factor, bool isFinished)
  54. {
  55. this.value = this.from * (1f - factor) + this.to * factor;
  56. this.mSource.enabled = (this.mSource.volume > 0.01f);
  57. }
  58. public static TweenVolume Begin(GameObject go, float duration, float targetVolume)
  59. {
  60. TweenVolume tweenVolume = UITweener.Begin<TweenVolume>(go, duration);
  61. tweenVolume.from = tweenVolume.value;
  62. tweenVolume.to = targetVolume;
  63. return tweenVolume;
  64. }
  65. public override void SetStartToCurrentValue()
  66. {
  67. this.from = this.value;
  68. }
  69. public override void SetEndToCurrentValue()
  70. {
  71. this.to = this.value;
  72. }
  73. [Range(0f, 1f)]
  74. public float from = 1f;
  75. [Range(0f, 1f)]
  76. public float to = 1f;
  77. private AudioSource mSource;
  78. }