当前位置:   article > 正文

Unity2D学习笔记Day13:添加音效Audio_unity增加个吃金币的声音

unity增加个吃金币的声音

学习资源:B站 M_Studio《Unity教程2D入门》18

Unity Assets:Sunnyland

Day13

Audio Listener:耳朵,用来听声音的——一般放在main camera下
Audio Source:喇叭,音源,放在哪就从哪出声音
Audio Clips:声音片段

添加BGM

main camera默认有Audio Listener组件。
给player添加Audio Source。
在这里插入图片描述
小技巧:
注意到这边有条蓝色的边边,说明不是预制体中改变而是临时改变。
希望每个场景中有player都有这个声音的话,点击右上角
Overrides->Apply All 就可以把修改的效果自动添加到预制体中。
在这里插入图片描述
选中BGM,属性面板中可以试听。
在这里插入图片描述
选择Play on wake游戏一开始就播放,Loop循环播放,Volume调节音量。
在这里插入图片描述

添加敌人爆炸音效

为frog添加音效。因为不是bgm,这两个就不需要勾选。
在这里插入图片描述
要在不同敌人死亡时都播放该音频,因此不需要在每个敌人类下写,还记得上节课的内容吗,直接在enemy类里写。
当前脚本如下:

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

public class Enemy : MonoBehaviour
{
    protected Animator anim;
    protected AudioSource deathAudio;
    protected virtual void Start()
    {
        anim = GetComponent<Animator>();
        deathAudio = GetComponent<AudioSource>();
    }

    public void setDeath()
    {
        anim.SetTrigger("Death");
        deathAudio.Play();
    }
    public void DestroySelf()
    {
        Destroy(gameObject);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

添加人物角色跳跃音效

问题:那getcomponent的时候,如何区分jump和bgm的audio resource呢?

设置成public,然后拖入就好。

public AudioSource jumpAudio;
  • 1

在这里插入图片描述
添加受伤、吃金币等音效,同理。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/810974
推荐阅读
相关标签
  

闽ICP备14008679号