UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
AudioSource.Stop
# [AudioSource](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.html).Stop public void **Stop** (); ## 描述 停止播放 [clip](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-clip.html)。 AudioSource.stop 函数停止播放当前设置的音频剪辑。当您下次播放该音频剪辑时,将从头开始播放。 另请参阅:[Play](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Play.html)、[Pause](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Pause.html) 函数。 ``` //This script allows you to toggle music to play and stop. //Assign an AudioSource to a GameObject and attach an Audio Clip in the Audio Source. Attach this script to the GameObject. using UnityEngine; public class Example : MonoBehaviour { AudioSource m_MyAudioSource; //Play the music bool m_Play; //Detect when you use the toggle, ensures music isn’t played multiple times bool m_ToggleChange; void Start() { //Fetch the AudioSource from the GameObject m_MyAudioSource = GetComponent<AudioSource>(); //Ensure the toggle is set to true for the music to play at start-up m_Play = true; } void Update() { //Check to see if you just set the toggle to positive if (m_Play == true && m_ToggleChange == true) { //Play the audio you attach to the AudioSource component m_MyAudioSource.Play(); //Ensure audio doesn’t play more than once m_ToggleChange = false; } //Check if you just set the toggle to false if (m_Play == false && m_ToggleChange == true) { //Stop the audio m_MyAudioSource.Stop(); //Ensure audio doesn’t play more than once m_ToggleChange = false; } } void OnGUI() { //Switch this toggle to activate and deactivate the parent GameObject m_Play = GUI.Toggle(new Rect(10, 10, 100, 30), m_Play, "Play Music"); //Detect if there is a change with the toggle if (GUI.changed) { //Change to true to show that there was just a change in the toggle state m_ToggleChange = true; } } } ```
da
2022年5月24日 10:08
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码