Unity 音效播放

目录

游戏开发所需音频文件

添加背景音乐

新建空对象

增加Audio Source组件

指定音频文件

循环播放、唤醒播放

添加音频播放脚本

给机器人加脚步声

设为3D空间混合

设置曲线

移除摄像机的监听器

在玩家上加一个监听器


游戏开发所需音频文件

背景音乐,走路,被打,击中敌人,完成任务等

添加背景音乐

新建空对象

增加Audio Source组件

指定音频文件

循环播放、唤醒播放

添加音频播放脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class AudioManager : MonoBehaviour
{
    public static AudioManager instance { get; private set; }
 
    private AudioSource audioS;
 
    // Start is called before the first frame update
    void Start()
    {
        instance = this;
        audioS = GetComponent<AudioSource>();
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
    public void AudioPlay(AudioClip clip)
    {
        audioS.PlayOneShot(clip);
    }
}

记得挂载这个脚本到空对象,不然报错

Object reference not set to an instance of an object Collect.OnTriggerEnter2D (UnityEngine.Collider2D other)

给机器人加脚步声

设为3D空间混合

设置曲线

移除摄像机的监听器

在玩家上加一个监听器

附完整教程:

Unity2d Rubys Adventure 课程设计报告

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/106500862
今日推荐