找回密码
 立即注册
查看: 431|回复: 0

Unity教程2D入门:18 音效Audio

[复制链接]
发表于 2022-1-11 09:10 | 显示全部楼层 |阅读模式
https://www.bilibili.com/video/BV1R4411C7FD
在对预制体添加组件时,组件的左边框会有蓝色条,代表该组件只作用于该预制体,


如果想所有预制体都作用,点击下面Overrides》Apply All即可


最方便的更新方式是点击预制体右侧的箭头在里面的gameobject添加音效




Audio Listener声音接收器
Audio Source扬声器,音源
Audio Clips声音片段
Player添加Audio Source组件,音乐拖进Audio Clips里面


这里先给enemy死亡一个声音
在Enemy脚本添加一个函数,并且在JumpOn()中调用(不需要拖拽调用)
  1. public class Enemy : MonoBehaviour
  2. {
  3.     protected Animator anim;
  4.     protected AudioSource deathAudio;//<===================================
  5.     protected virtual void Start()
  6.     {
  7.         anim = GetComponent<Animator>();
  8.         deathAudio = GetComponent<AudioSource>();
  9.     }
  10.     public void Death()
  11.     {
  12.         Destroy(gameObject);
  13.     }
  14.     public void JumpOn()
  15.     {
  16.         anim.SetTrigger("Death");
  17.         deathAudio.Play();//<===================================
  18.     }
  19. }
复制代码
偷懒技巧:在Frog复制组件,在Eagle随便选一个组件粘贴,轻松复制死亡音效



为Player添加跳跃音效,① 在player添加代码如下
  1.     public AudioSource jumpAuido;//<==================================
  2.     void Start()
  3.     {
  4.         jumpAuido = GetComponent<AudioSource>();//<==================================
  5.     }
  6.     void Movement()//移动代码
  7.     {
  8.         if (Input.GetKeyDown(KeyCode.U))
  9.         {
  10.             jumpAuido.Play();//<==================================
  11.         }
  12.     }
复制代码
记得AudioSource要添加在预制体内的Player,否则会与BGM冲突




后期添加吃樱桃,受伤害等方法和跳跃音效相同。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-14 18:55 , Processed in 0.149528 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表