How Unity controls the volume of sound (including mute function)

One: UGUI production

1. First, create the Slider component in the UI under [Hierarchy]. Set its corresponding width and height.

b7a97e0944c746369ad0f9ad0640492e.png

 2. Adjust the fill color of the Slider slider. The general sound color is yellow, so we also tune it to yellow.

a6ac7a900f6d479a8c98f417ea134add.png

We try to slide the value inside the Slider.

9c6a87d481064bb3affbfe3bae2bb0df.png

a. Before sliding.

ac1bba4e9b034fffb638a7908c844310.png

b. Slide halfway.

 46a46cad5ab94f9c9607018048fe7c1d.png

c. Finish sliding.

 0b27484fa4db4a6582cfa33a97104396.png

 From the above sliding value, we can know that the filling effect is controlled by the value. Making the blood bar is similar, depending on what it is used for, we make a sliding control volume bar, so we need to get the sound first.

Three: Create an empty object to mount the sound

We create an empty object named BGM, then add the AudioSource sound source component , and add the size of the sound that needs to be controlled in the component.

046c3c6373a04cf3b517fa4b7756b947.png

At this point we have mounted the sound that needs to be controlled, and then we create a script to realize that the volume of the sound changes with the value of the Slider component .

 Four: Code implementation to control the volume of the sound

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BGM : MonoBehaviour
{
    // Start is called before the first frame update

    //声音控制器
    public Slider AudioSlider;
    //声音控制器文本
    public Text AudioText;
    //音乐
    public AudioSource BGMsource;
    void Start()
    {
        //默认一开始声音为0.6
        AudioSlider.value = 0.6f;
    }
    //控制声音大小方法
    public void AudioCtrl()
    {
        //把value的值赋值给 BGMsource.volume 
        BGMsource.volume = AudioSlider.value;

        //文本显示当前声音大小
        AudioText.text = AudioSlider.value.ToString();
    }

}

Five: Back to the Unity engine

1. In order to be able to see it clearly, we have done more detailed above, adding a text to display the sound level.

So we need to create a Text text in Unity and set the size of the Text text.

71b4dfcb62934049858b0d8b166def3a.png

 2. Mount components and objects

613aeb8693864ea7bfd86cbafd8f1ec5.png

 8e3f5456041547079dd954a8e19752ef.png

3. run

a. The default sound is 0.6 

d3842fac90634f7c8aeb2f0fb0777a60.png

b. Slide the Slider component in the game scene, and the corresponding sound volume value will change accordingly.

16d5ae9c30c546498c00cfa82ec611af.png

 To make a sound controller, we should also have a button, check it to have a sound, disable it and the sound will be gone. Next, let's implement it.

Six: Sound controller switch

1. Create the Toggle component

b33dd35b5b934c5f8c15e43b6f15ef03.png

 2. Add the corresponding components to the code

After improvement: the following is the overall code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BGM : MonoBehaviour
{
    // Start is called before the first frame update

    //声音控制器
    public Slider AudioSlider;
    //声音控制器文本
    public Text AudioText;
    //声音开关
    public Toggle AudioSwitchToggle;
    //音乐
    public AudioSource BGMsource;
    void Start()
    {
        //默认一开始声音为0.6
        AudioSlider.value = 0.6f;
    }
    //控制声音大小方法
    public void AudioCtrl()
    {
        //把value的值赋值给 BGMsource.volume 
        BGMsource.volume = AudioSlider.value;

        //文本显示当前声音大小
        AudioText.text = AudioSlider.value.ToString();
    }
    //控制声音开关
    public void AudioSwitchToggleCtrl()
    {
        //如果声音控制按钮勾选了,那么我们就有声音
        if (AudioSwitchToggle.isOn)
        {
            //激活声音对象为自动播放
            BGMsource.gameObject.SetActive(true);
            //调用移动滑块控制声音大小
            AudioCtrl();
        }
        else
        {
            //关闭声音对象
            BGMsource.gameObject.SetActive(false);
        }
    }
}

3. Mount tgoole object and add method

9e50fd48feb34546b6139bb3fc89cdfa.png

3fa3cb43b6d14787b548f0867bb9855f.png

Seven: Operation effect diagram

1: runtime

3aeb4fc0b6d14a18a635e84b51575fdd.png

 2: After clicking the toggle button

aa466dcfc7a0463eb0c7edea1303b1fe.png

Here we run the exploit so that we can see that the BGM object is directly disabled , and no sound can be heard at runtime. 

Therefore, it has been realized to check to turn on the sound, and to disable the effect of unchecking the sound.

One more thing to add:   If we need to keep the sound playing, we can check the Loop on the AudioSource component to play in a loop. After checking, the sound can be played in a loop. Generally, sound is essential in the production process of us using Unity.


at last

The above steps are the whole process of making the sound switch controller and the sound slider to control the volume of the sound.

Friends who have seen it, click three times with one button. Your support makes me more motivated to create and share. I hope it will always bring you surprises and gains.

2413988ad82470fe49997fa97ee1adf8.png
Three in a row with one click! ! !

Guess you like

Origin blog.csdn.net/weixin_57813136/article/details/132327906