簡體   English   中英

unity c# 如何制作倒計時滴答聲?

[英]Unity c# How can i make countdown tick-tock sound?

我正在制作一個塔防游戲。 我想發出滴答聲倒計時的聲音,但是當我在代碼中調用“播放聲音”功能時,它只會無限循環。 我也使用了協程,但它不起作用。 請幫我。

================================================== ================

波生成器

public float timeBetweenWaves = 34f;
 private float countdown = 34f;
   
 void Update()
 {
     if (waveNumber <= 40)
     {
         SpawnCount();
     }
 }
 void SpawnCount()
 {
     GameObject[] enemyFind = GameObject.FindGameObjectsWithTag("Enemy");
     int enemyCount = enemyFind.Length;
     if (enemyCount == 0) // if enemy is 0, countdown goes 34 to 0
     {
         if (countdown <= 0f) // if countdown becomes 0, wave starts and countdown stops at 34
         {
             StartCoroutine(SpawnWave()); // wave starts here
             countdown = timeBetweenWaves; // countdown initializes
         }
         
         countdown -= Time.deltaTime; // countdown goes 34 to 0
         waveCountDownText.text = Mathf.Round(countdown).ToString();
         // I want to play countdown tick-tock sound here until countdown becomes 0
         //FindObjectOfType<SoundManager>().Play("Timer"); // this didn't work
     }
 }

聲音管理器

public Sound[] sounds;
 public static SoundManager instance;
 void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
     else
     {
         Destroy(gameObject);
         return;
     }
     DontDestroyOnLoad(gameObject);
     foreach (Sound s in sounds)
     {
         s.source = gameObject.AddComponent<AudioSource>();
         s.source.clip = s.clip;
         s.source.volume = s.volume;
         s.source.pitch = s.pitch;
         s.source.loop = s.loop;
     }
 }
 void Start()
 {
     Play("Theme");
 }
 public void Play (string name) // i used this function and sound name is "Timer"
 {
     Sound s = Array.Find(sounds, sound => sound.name == name);
     if (s == null)
     {
         Debug.LogWarning("Sound: " + name + "Not Found!");
         return;
     }
     s.source.Play();
 }

嘗試使用 AudioSource

public AudioSource[] audioSource;
public AudioClip[] clips;

audioSource [source].clip = clips [0];
audioSource[source].Play (); 

也請閱讀此內容,它可能對Unity有所幫助:音頻源、音頻偵聽器和音頻剪輯之間的區別

我會做這樣的事情。 如果您添加此代碼,這可能會更容易,您只需將滴答聲數組中的滴答聲和滴答聲更改為您想要的任何內容。

public class Clock : MonoBehaviour
{
    AudioSource audioS;
    public AudioClip[] ticking; 
    void Start()
    {
        audioS = gameObject.AddComponent<AudioSource>();
        StartCoroutine(TickingClock());
    }

    public bool tick = true; 
    IEnumerator TickingClock()
    {
        int x= 0; 
        while(tick)
        {
            x++;
            if(x % 2 == 0)
            {
                audioS.PlayOneShot(ticking[0], 1f);
            }
            else
            {
                audioS.PlayOneShot(ticking[1], 1f);
            }
        yield return new WaitForSeconds(0.5f);

        }
    }
}

要關閉滴答聲,您只需將 bool 設為 false :)。 如果你想調用 is ,你可能需要創建一個新的方法來觸發 is when 調用。

就像是。

// call this from a game Controler script :) 
public void CallClock(){
    StartCoroutine(TickingClock());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM