HeavenStudioPlus/Assets/Scripts/Util/Sound.cs

73 lines
1.9 KiB
C#
Raw Normal View History

2021-12-21 01:10:49 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RhythmHeavenMania.Util
{
public class Sound : MonoBehaviour
{
public AudioClip clip;
2022-01-15 07:08:23 +00:00
public float pitch = 1;
2021-12-21 01:10:49 +00:00
private AudioSource audioSource;
2022-01-15 07:08:23 +00:00
private int pauseTimes = 0;
private float startTime;
public bool relyOnBeat = true;
2021-12-21 01:10:49 +00:00
private void Start()
{
audioSource = GetComponent<AudioSource>();
2022-01-15 07:08:23 +00:00
audioSource.clip = clip;
audioSource.pitch = pitch;
audioSource.PlayScheduled(Time.time);
startTime = Conductor.instance.songPosition;
if (!relyOnBeat)
{
StartCoroutine(NotRelyOnBeatSound());
}
2021-12-21 01:10:49 +00:00
}
2022-01-15 07:08:23 +00:00
private void Update()
2021-12-21 01:10:49 +00:00
{
if (relyOnBeat)
2022-01-15 07:08:23 +00:00
{
if (Conductor.instance.isPaused && !Conductor.instance.isPlaying && pauseTimes == 0)
{
audioSource.Pause();
pauseTimes = 1;
}
else if (Conductor.instance.isPlaying && !Conductor.instance.isPaused && pauseTimes == 1)
{
audioSource.Play();
pauseTimes = 0;
}
2022-01-15 07:08:23 +00:00
else if (!Conductor.instance.isPlaying && !Conductor.instance.isPaused)
{
Destroy(this.gameObject);
}
if (Conductor.instance.songPosition > startTime + clip.length)
{
Destroy(this.gameObject);
}
2022-01-20 01:48:52 +00:00
if (Conductor.instance.songPosition < startTime)
{
Destroy(this.gameObject);
}
2022-01-15 07:08:23 +00:00
}
}
2022-01-15 07:08:23 +00:00
IEnumerator NotRelyOnBeatSound()
{
yield return new WaitForSeconds(clip.length);
Destroy(this.gameObject);
2021-12-21 01:10:49 +00:00
}
}
}