AudioSource.Stop

Unity · Версия 2020.3 LTS · Scripting · AudioSource

AudioSource.Stop

Объявление

public void Stop();

Описание

Остановка воспроизведения клипа.

Функция AudioSource.stop останавливает воспроизведение текущего установленного аудиоклипа. Аудиоклип воспроизводится с начала при следующем воспроизведении.

Смотрите так же: Play, Pause functions.

//Этот скрипт позволяет включать и выключать музыку.
//Назначаем AudioSource GameObject и присоединяем Audio Clip к Audio Source. Прикрепите этот скрипт к 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;
        }
    }
}

Обновлено 17 ноября 2026 г.