AudioClip









How do I use Audio Clip
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: SoundManager.cs Copy
9     public static void LoadBgMusic(string fileName, bool isChangeMusic)
10     {
11         if (!isMusic) return;
12         if (isChangeMusic)
13         {
14             if (bgMusic != null)
15             {
16                 Object.Destroy(bgMusic);
17                 bgMusic = null;
18             }
19         }
20         if (bgMusic == null)
21         {
22             bgMusic = new GameObject("BgMusic");
23             AudioSource audio = bgMusic.AddComponent();
24             audio.clip = Resources.Load(fileName);
25             audio.loop = true;
26             audio.Play();
27             Object.DontDestroyOnLoad(bgMusic);
28         }
29     }
File name: SoundManager.cs Copy
31     public static void playSound(string fileName)
32     {
33         if (!isSound) return;
34         GameObject sound = new GameObject("Sound");
35         AudioSource audio = sound.AddComponent();
36         audio.clip = Resources.Load(fileName);
37         audio.Play();
38         Object.Destroy(sound, 1);
39     }
File name: SoundManager.cs Copy
41     public static void playSoundLong(string fileName)
42     {
43         if (!isSound) return;
44         GameObject sound = new GameObject("Sound");
45         AudioSource audio = sound.AddComponent();
46         audio.clip = Resources.Load(fileName);
47         audio.Play();
48     }
File name: SoundManager.cs Copy
50     public static void playSoundLong(string fileName, float duration)
51     {
52         if (!isSound) return;
53         GameObject sound = new GameObject("Sound");
54         AudioSource audio = sound.AddComponent();
55         audio.clip = Resources.Load(fileName);
56         audio.Play();
57         Object.Destroy(sound, duration);
58     }
File name: SoundManager.cs Copy
60     public static void playButtonSound()
61     {
62         //playSound("Sounds/click");
63         if (!isSound) return;
64         if (btSound != null)
65         {
66             btSound.GetComponent().Play();
67         }
68         else
69         {
70             btSound = new GameObject("BtSound");
71             AudioSource audio = btSound.AddComponent();
72             audio.clip = Resources.Load("Sounds/click");
73             audio.Play();
74             Object.DontDestroyOnLoad(btSound);
75         }
76     }
File name: SoundManager.cs Copy
32   public void PlaySingle(AudioClip clip)
33   {
34    //Set the clip of our efxSource audio source to the clip passed in as a parameter.
35    efxSource.clip = clip;
36
37    //Play the clip.
38    efxSource.Play ();
39   }
File name: SoundManager.cs Copy
43   public void RandomizeSfx (params AudioClip[] clips)
44   {
45    //Generate a random number between 0 and the length of our array of clips passed in.
46    int randomIndex = Random.Range(0, clips.Length);
47
48    //Choose a random pitch to play back our clip at between our high and low pitch ranges.
49    float randomPitch = Random.Range(lowPitchRange, highPitchRange);
50
51    //Set the pitch of the audio source to the randomly chosen pitch.
52    efxSource.pitch = randomPitch;
53
54    //Set the clip to the clip at our randomly chosen index.
55    efxSource.clip = clips[randomIndex];
56
57    //Play the clip.
58    efxSource.Play();
59   }

AudioClip 96 lượt xem

Gõ tìm kiếm nhanh...