C# 移动滑块时如何播放音频剪辑?

C# 移动滑块时如何播放音频剪辑?,c#,unity3d,audio,C#,Unity3d,Audio,在我的另一个问题中,我在游戏场景和主菜单场景中使用了一个音频混音器和两个组 在主菜单场景中,我有音乐的音频源和sfx效果的音频源。 当游戏在主菜单中开始时,音乐会在唤醒状态下自动播放 sfx没有播放,我希望当我更改sfx滑块时,它会在短时间内发出sfx效果,因此每次更改滑块值时,它都会播放sfx以听到音量 滑块在游戏中的样子: 设置音频音量时的设置脚本: using System.Collections; using System.Collections.Generic; using Uni

在我的另一个问题中,我在游戏场景和主菜单场景中使用了一个音频混音器和两个组

在主菜单场景中,我有音乐的音频源和sfx效果的音频源。 当游戏在主菜单中开始时,音乐会在唤醒状态下自动播放

sfx没有播放,我希望当我更改sfx滑块时,它会在短时间内发出sfx效果,因此每次更改滑块值时,它都会播放sfx以听到音量

滑块在游戏中的样子:

设置音频音量时的设置脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using TMPro;
using System;
using UnityEngine.Events;
using System.Linq;

public class Settings : MonoBehaviour
{
    public AudioMixer audioMixer;
    public TMP_Dropdown resolutionDropdown;
    public TMP_Dropdown qualityDropdown;
    public Text musicText;
    public Text sfxText;
    public Slider[] audioSliders;
    public Toggle fullScreenToggle;

    private Resolution[] resolutions;

    private void Awake()
    {
        resolutionDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("resolutionvalue", resolutionDropdown.value);
            PlayerPrefs.Save();

        }));

        qualityDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("qualityvalue", qualityDropdown.value);
            PlayerPrefs.Save();

        }));

        fullScreenToggle.onValueChanged.AddListener(new UnityAction<bool>(index =>
        {
            PlayerPrefs.SetInt("fullscreen", boolToInt(fullScreenToggle.isOn));
            PlayerPrefs.Save();

        }));
    }

    private void Start()
    {
        qualityDropdown.value = PlayerPrefs.GetInt("qualityvalue");

        var resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == 60).ToArray();
        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolutionIndex = 0;
        for(int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if(resolutions[i].width == Screen.currentResolution.width &&
                resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }
        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = PlayerPrefs.GetInt("resolutionvalue", currentResolutionIndex);
        resolutionDropdown.RefreshShownValue();

        float musicvolume = PlayerPrefs.GetFloat("musicvolume");
        float sfxvolume = PlayerPrefs.GetFloat("sfxvolume");

        musicText.text = musicvolume.ToString();
        sfxText.text = sfxvolume.ToString();
        audioSliders[0].value = musicvolume / 100f;
        audioSliders[1].value = sfxvolume / 100f;

        fullScreenToggle.isOn = intToBool(PlayerPrefs.GetInt("fullscreen", 0));
        
    }

    public void SetResolution(int resolutionIndex)
    {
        if (resolutions != null)
        {
            Resolution resolution = resolutions[resolutionIndex];
            Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
        }
    }

    public void SetMusicVolume(float volume)
    {
        audioMixer.SetFloat("musicvol", Mathf.Log10(volume) * 20);
        musicText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("musicvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));
    }

    public void SetSfxVolume(float volume)
    {
        audioMixer.SetFloat("sfxvol", Mathf.Log10(volume) * 20);
        sfxText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("sfxvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));
    }

    public void SetQuality(int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullscreen(bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;        
    }

    int boolToInt(bool val)
    {
        if (val)
            return 1;
        else
            return 0;
    }

    bool intToBool(int val)
    {
        if (val != 0)
            return true;
        else
            return false;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Audio;
使用UnityEngine.UI;
使用TMPro;
使用制度;
使用UnityEngine.Events;
使用System.Linq;
公共课堂设置:单一行为
{
公共混音器;
公共TMP_下拉列表决议下拉列表;
公共TMP_下拉列表质量下拉列表;
公共文本音乐文本;
公共文本sfxText;
公共滑块[]音频滑块;
公共切换全屏切换;
非公开决议[]决议;
私人空间
{
resolutionDropdown.onValueChanged.AddListener(新UnityAction(索引=>
{
PlayerPrefs.SetInt(“resolutionvalue”,resolutionDropdown.value);
PlayerPrefs.Save();
}));
qualityDropdown.onValueChanged.AddListener(新UnityAction)(索引=>
{
PlayerPrefs.SetInt(“qualityvalue”,qualityDropdown.value);
PlayerPrefs.Save();
}));
fullScreenToggle.onValueChanged.AddListener(新的UnityAction(索引=>
{
PlayerPrefs.SetInt(“全屏”,boolToInt(fullscreenttoggle.isOn));
PlayerPrefs.Save();
}));
}
私有void Start()
{
qualityDropdown.value=PlayerPrefs.GetInt(“qualityvalue”);
var resolutions=Screen.resolutions.Where(resolution=>resolution.refreshRate==60.ToArray();
resolutionDropdown.ClearOptions();
列表选项=新列表();
int currentResolutionIndex=0;
for(int i=0;i

也许我的整个逻辑都是错误的,但我想让播放器选择更改音乐和sfx音量,以决定加载的内容和减少的内容

[SerializeField] private AudioSource SfxSource;

public void SetSfxVolume(float volume)
{
    audioMixer.SetFloat("sfxvol", Mathf.Log10(volume) * 20);
    sfxText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

    PlayerPrefs.SetFloat("sfxvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));

    if(!SfxSource.isPlaying) SfxSource.Play();
}

或者使用。

我在这一行中使用了一个断点:if(!SfxSource.isPlaying),当移动sfx卷的滑块时,它始终为true。这从来都不是假的。我设法让它起作用了。因为两个音频源在同一个游戏对象上,所以我稍微更改了脚本。在顶部,我使用了数组:[SerializeField]private audiosource[]audioSources,而不是single audiosource;然后在sfx的函数中:if(!audioSources[1].isplay)audioSources[1].Play();现在我面临另一个问题。如果我移动滑块太快,当我停止滑块时,它将再次开始播放sfx。因此,如果我在值7中,并将滑块快速移动到23,它将在值23上再次播放。但我希望在缓慢或快速移动滑块时,在滑块的每个值上播放。有没有办法解决这个问题?在这种情况下,您可以跳过检查,直接使用
SfxSource.Play()