Unity 声音

摄像机默认自带声音插件,因此如果有多个摄像机的时候会报错,把另一个摄像机的声音插件删掉保留一个即可

将声音绑定到对象中:添加声音插件 Audio Source,将音乐直接拖入

唤醒时播放:运行起来是否播放

3D Sound Settings

①:物体上有个喇叭状图形,进行拖拽可以调节最小距离(声音)

②:滑动滚轮会发现有个最大球形,这个球形控制着最大距离(声音)

脚本控制

添加AudioTest脚本

    //写下该对象,脚本文件中会出现下面该选项,将音乐拖拽进去后,该对象不为空
    public AudioClip music;
    public AudioClip se;

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

public class AudioTest : MonoBehaviour
{
    //AudioClip
    public AudioClip music;
    public AudioClip se;

    //播放器组件
    private AudioSource player;


    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<AudioSource>();
        //设定播放的音频片段
        player.clip = music;
        //循环播放
        player.loop = true;
        //音量
        player.volume = 0.5f;
        //播放
        player.Play();
    }

    // Update is called once per frame
    void Update()
    {
        //按空格切换声音的播放和暂停
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (player.isPlaying)
            {
                //暂停 对应关系:1 正常暂停
                //player.Pause();

                //停止 对应关系:2 每次暂停都重新开始
                player.Stop();
            }
            else
            {
                //继续 对应关系:1
                //player.UnPause();

                //开始播放 对应关系:2
                player.Play();
            }
        }

        //按鼠标左键播放声音
        if (Input.GetMouseButton(0))
        {
            //只播放一声
            player.PlayOneShot(se);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ssl267422/article/details/128808519
今日推荐