版本: Unity 6 (6000.0)
语言英语
  • C#

AudioSource.Stop

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每一个建议,并在适用时进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册

声明

public void Stop();

描述

停止播放 剪辑.

AudioSource.stop 函数会停止当前设置的音频剪辑的播放。下次播放音频剪辑时,它将从头开始播放。

其他资源: PlayPause 函数。

//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; } } }