Unity3D 的 PlayOnShot() Play() PlayClipAtPoint()

一.参考资料:

  https://blog.csdn.net/winner_2012/article/details/46558435

  https://blog.csdn.net/winner_2012/article/details/46558435

  https://blog.csdn.net/winner_2012/article/details/46558589

二.介绍目录

  Class    AudioSource

  1.基本常识

  2.常见使用方法介绍

    Public Methed

      (1)  Play()

      (2)  PlayOnShot()

    Static Methed

      (1) PlayClipAtPoint()

-----------------------------官方API 指路-------------------------------------

AudioSource

Description

A representation of audio sources in 3D.

An AudioSource is attached to a GameObject for playing back sounds in a 3D environment. In order to play 3D sounds you also need to have a AudioListener. The audio listener is normally attached to the camera you want to use. Whether sounds are played in 3D or 2D is determined by AudioImporter settings.

You can play a single audio clip using PlayPause and Stop. You can also adjust its volume while playing using the volume property, or seek using time. Multiple sounds can be played on one AudioSource using PlayOneShot. You can play a clip at a static position in 3D space using PlayClipAtPoint.

See Also: AudioListenerAudioClipAudioSource component.

 

-----------------------------------------------------------------------------

三.总结:

   1. AudioSource 是 声源(即声音发出的来源)

   那么不管是2D音效还是3D音效都需要一个  AudioListener 来接受音效

   

    2. AudioSource 组件 使用了 AudioClip.

 即 AudioSource 中包含 AudioClip   ,AudioClip 存放需要播放的音乐(.ogg .mp3 等)

 

    

     AudioSource 可以用于2D 或者 3D环境,对应的 Inspector 窗口中可以看见

    

 (1) 如果我们只需要播放一种音乐.

  就可以用 Play() Pause() Stop() 进行控制。

  而使用Play() 这种方法播放,播放音效的属性取决于 Inspector窗口中设置。

       如图,用Play() 播放音效的音量为0.469

       

  

  值得注意的一点是,每个 AudioSource 用 Play() , 只能播放一种音效.

  即 AudioSource 调用 Play()后, 会先将 这个 AudioSource 正在播放音效停止,然后再播放 AudioClip 中的音效。

   

public class SoundPlayer : MonoBehaviour {
    private static AudioClip audioClip;
        private static AudioSource audioSource;
    void Awake(){
        //获取当前GameObject的AudioSource组件的AduioPlayer
        audioClip = gameObject.GetComponent<AudioSource>().clip;
        //获取当前GameObject的AudioSource组件
        audioSource = this.gameObject.GetComponent<AudioSource>();
        audioSource.Play();
        StartCoroutine(WaitAndPrint(4.0F));//4秒后再次调用Play()
    }
}

 

(2) 如果我们需要在同一时间播放多种音乐。 我们就需要用到 PlayClipAtPoint() 或者 PlayOnShot()

  PlayOnShot()  与 PlayClipAtPoint() 区别在于

       1. PlayClipAtPoint() 是 Static Method 不需要实例化对象就可以使用.

  2. PlayClipAtPoint() 播放的是 3D 音效,因为PlayOnShot() 与位置有关

    , PlayOnShot() 播放的是 2D 音效

  每个

  

猜你喜欢

转载自www.cnblogs.com/--zz/p/9378845.html