AudioSource.pitch

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

AudioSource.pitch

public float pitch;

Описание

Высота звука источника звука.

Высота тона – это качество, благодаря которому мелодия становится выше или ниже. В качестве примера представьте себе воспроизведение аудиоклипа с шагом, равным единице. Увеличение высоты тона во время воспроизведения клипа заставит его звучать так, как будто он выше. Точно так же уменьшение высоты тона меньше единицы делает звук клипа тише.

//Attach this script to a GameObject.
//Attach an AudioSource to your GameObject (Click Add Component and go to Audio>Audio Source). Choose an audio clip in the AudioClip field.
//This script sets the pitch of the audio at the start, and then gradually turns it down to 0 as time passes.

using UnityEngine;

//Make sure there is an Audio Source component on the GameObject
[RequireComponent(typeof(AudioSource))]

public class ExampleScript : MonoBehaviour
{
    public int startingPitch = 4;
    public int timeToDecrease = 5;
    AudioSource audioSource;

    void Start()
    {
        //Fetch the AudioSource from the GameObject
        audioSource = GetComponent<AudioSource>();

        //Initialize the pitch
        audioSource.pitch = startingPitch;
    }

    void Update()
    {
        //While the pitch is over 0, decrease it as time passes.
        if (audioSource.pitch > 0)
        {
            audioSource.pitch -= Time.deltaTime * startingPitch / timeToDecrease;
        }
    }
}

Другой пример:

using UnityEngine;

// Скрипт, который воспроизводит выбранную вами песню. Шаг начинается с 1.0.
// Вы можете увеличивать и уменьшать высоту тона и слышать изменение
// что сделано.

public class AudioExample : MonoBehaviour
{
    public float pitchValue = 1.0f;
    public AudioClip mySong;

    private AudioSource audioSource;
    private float low = 0.75f;
    private float high = 1.25f;

    void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.clip = mySong;
        audioSource.loop = true;
    }

    void OnGUI()
    {
        pitchValue = GUI.HorizontalSlider(new Rect(25, 75, 100, 30), pitchValue, low, high);
        audioSource.pitch = pitchValue;
    }
}

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