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

AudioSettings.Reset

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们不能接受所有提交的内容,但我们确实会阅读用户提出的每一项修改建议,并在适用情况下进行更新。

关闭

提交失败

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

关闭

取消

切换到手册

声明

public static bool Reset(AudioConfiguration config);

参数

config 要使用的新的配置。

返回值

bool 如果所有设置都能成功应用,则为 True。

描述

更改设备配置并使用参数 deviceWasChanged=false 调用 AudioSettings.OnAudioConfigurationChanged 委托。不能保证使用指定的精确设置,但 Unity 会自动使用它支持的最接近的匹配项。注意:如果在异步加载对象时调用 AudioSettings.Reset,这可能会导致主线程停顿。

using UnityEngine;
using System.Collections;

public class TestAudioConfiguration : MonoBehaviour { void Start() { AudioSettings.OnAudioConfigurationChanged += OnAudioConfigurationChanged; }

void OnAudioConfigurationChanged(bool deviceWasChanged) { Debug.Log(deviceWasChanged ? "Device was changed" : "Reset was called"); if (deviceWasChanged) { AudioConfiguration config = AudioSettings.GetConfiguration(); config.dspBufferSize = 64; AudioSettings.Reset(config); } GetComponent<AudioSource>().Play(); }

static int[] validSpeakerModes = { (int)AudioSpeakerMode.Mono, (int)AudioSpeakerMode.Stereo, (int)AudioSpeakerMode.Quad, (int)AudioSpeakerMode.Surround, (int)AudioSpeakerMode.Mode5point1, (int)AudioSpeakerMode.Mode7point1 };

static int[] validDSPBufferSizes = { 32, 64, 128, 256, 340, 480, 512, 1024, 2048, 4096, 8192 };

static int[] validSampleRates = { 11025, 22050, 44100, 48000, 88200, 96000, };

static int[] validNumRealVoices = { 1, 2, 4, 8, 16, 32, 50, 64, 100, 128, 256, 512, };

static int[] validNumVirtualVoices = { 1, 2, 4, 8, 16, 32, 50, 64, 100, 128, 256, 512, };

int GUIRow(string name, int[] valid, int value, ref bool modified) { GUILayout.BeginHorizontal(); GUILayout.Button(name + "=" + value); for (int i = 0; i < valid.Length; i++) { string s = valid[i].ToString(); if (valid[i] == value) s = "[" + s + "]"; if (GUILayout.Button(s)) { value = valid[i]; modified = true; } } GUILayout.EndHorizontal(); return value; }

void OnGUI() { AudioSource source = GetComponent<AudioSource>(); bool modified = false;

AudioConfiguration config = AudioSettings.GetConfiguration();

config.speakerMode = (AudioSpeakerMode)GUIRow("speakerMode", validSpeakerModes, (int)config.speakerMode, ref modified); config.dspBufferSize = GUIRow("dspBufferSize", validDSPBufferSizes, config.dspBufferSize, ref modified); config.sampleRate = GUIRow("sampleRate", validSampleRates, config.sampleRate, ref modified); config.numRealVoices = GUIRow("RealVoices", validNumRealVoices, config.numRealVoices, ref modified); config.numVirtualVoices = GUIRow("numVirtualVoices", validNumVirtualVoices, config.numVirtualVoices, ref modified);

if (modified) AudioSettings.Reset(config);

if (GUILayout.Button("Start")) source.Play();

if (GUILayout.Button("Stop")) source.Stop(); } }