Version: 2021.2
public float volume ;

描述

音频源的音量(0.0 到 1.0)。

AudioSource 的音量属性控制来自 AudioClip 的声音级别。最高音量等级为 1,最低音量为 0(此时听不到声音)。

using UnityEngine;

public class Example : MonoBehaviour { AudioSource m_MyAudioSource; //Value from the slider, and it converts to volume level float m_MySliderValue;

void Start() { //Initiate the Slider value to half way m_MySliderValue = 0.5f; //Fetch the AudioSource from the GameObject m_MyAudioSource = GetComponent<AudioSource>(); //Play the AudioClip attached to the AudioSource on startup m_MyAudioSource.Play(); }

void OnGUI() { //Create a horizontal Slider that controls volume levels. Its highest value is 1 and lowest is 0 m_MySliderValue = GUI.HorizontalSlider(new Rect(25, 25, 200, 60), m_MySliderValue, 0.0F, 1.0F); //Makes the volume of the Audio match the Slider value m_MyAudioSource.volume = m_MySliderValue; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961