You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.4 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class musicController : MonoBehaviour {
  4. public AudioClip startMusic;
  5. public AudioClip endMusic;
  6. private AudioSource source;
  7. private bool triggered = false;
  8. void Awake(){
  9. source = GetComponent<AudioSource> ();
  10. }
  11. // Use this for initialization
  12. void Start () {
  13. source.clip = startMusic;
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. if (Input.GetKeyDown (KeyCode.Equals)) {
  18. source.volume += 0.1f;
  19. }
  20. if (Input.GetKeyDown (KeyCode.Minus)) {
  21. source.volume -= 0.1f;
  22. }
  23. }
  24. IEnumerator fadeOut (float end, float steps){
  25. Debug.Log ("Volume: " + source.volume);
  26. float curVol = source.volume;
  27. while(source.volume > end + steps)
  28. {
  29. source.volume -= steps * Time.deltaTime;
  30. Debug.Log ("Volume: " + source.volume);
  31. yield return new WaitForSeconds(0.01f);
  32. }
  33. source.volume = end;
  34. source.clip = endMusic;
  35. source.Play();
  36. StartCoroutine (fadeIn (curVol, 0.2f));
  37. yield return null;
  38. }
  39. IEnumerator fadeIn (float end, float steps){
  40. while(source.volume <end - steps)
  41. {
  42. source.volume += steps * Time.deltaTime;
  43. yield return new WaitForSeconds(0.01f);
  44. }
  45. source.volume = end;
  46. yield return null;
  47. }
  48. void OnTriggerEnter(Collider other) {
  49. Debug.Log ("Music Change");
  50. if (triggered)
  51. return;
  52. if (other.transform.tag != "Player")
  53. return;
  54. StartCoroutine (fadeOut (0.0f, 0.2f));
  55. //fadeIn (1.0f, 0.01f);
  56. triggered = true;
  57. }
  58. }