2016-08-18 15 views
0

私はドアスクリプトを作っていますが、それはうまくいきますが、今はドアが開いて閉じるときに別のサウンドを追加したいと思います。ドアにオーディオソースを追加して、ドアオープン音を加えました。どうすればdoorCloseサウンドを追加して、それをスクリプトで再生させることができますか? Audio Source2つのサウンドを追加してスクリプトで再生するには?

if (open) { 
    GetComponent<AudioSource>().Play(); 
} else { 
    GetComponent<AudioSource>().Play(); 
} 

答えて

0

チェックAudio & Sound Tutorial。ここにサンプルコードがあります:

using UnityEngine; 
using System.Collections; 

namespace Completed 
{ 
    public class SoundManager : MonoBehaviour 
    { 
     public AudioSource efxSource;     //Drag a reference to the audio source which will play the sound effects. 
     public AudioSource musicSource;     //Drag a reference to the audio source which will play the music. 
     public static SoundManager instance = null;  //Allows other scripts to call functions from SoundManager.    
     public float lowPitchRange = .95f;    //The lowest a sound effect will be randomly pitched. 
     public float highPitchRange = 1.05f;   //The highest a sound effect will be randomly pitched. 


     void Awake() 
     { 
      //Check if there is already an instance of SoundManager 
      if (instance == null) 
       //if not, set it to this. 
       instance = this; 
      //If instance already exists: 
      else if (instance != this) 
       //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager. 
       Destroy (gameObject); 

      //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene. 
      DontDestroyOnLoad (gameObject); 
     } 


     //Used to play single sound clips. 
     public void PlaySingle(AudioClip clip) 
     { 
      //Set the clip of our efxSource audio source to the clip passed in as a parameter. 
      efxSource.clip = clip; 

      //Play the clip. 
      efxSource.Play(); 
     } 


     //RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch. 
     public void RandomizeSfx (params AudioClip[] clips) 
     { 
      //Generate a random number between 0 and the length of our array of clips passed in. 
      int randomIndex = Random.Range(0, clips.Length); 
0

両方のオーディオファイルを参照してください。 次に、

if (open) { 
    GetComponent<AudioSource>().clip = _OpenClip; 
    GetComponent<AudioSource>().Play(); 
} else { 
    GetComponent<AudioSource>().clip = _CloseClip; 
    GetComponent<AudioSource>().Play(); 
} 
関連する問題