Version: 2023.1
public float pitch ;

描述

音频源的音高。

音高是一种让旋律更高或更低的品质。例如,请想象一下正在播放一个音高设置为 1 的音频剪辑。在播放该剪辑时,增加音高会使剪辑听起来更悠扬。类似地,将音高减小到 1 以下会使剪辑声音变得低沉。

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

// A script that plays your chosen song. The pitch starts at 1.0. // You can increase and decrease the pitch and hear the change // that is made.

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