赞
踩
简单介绍一下unity audio,auido包括三个东西,分别是Aduio Listener,Audio Source,Audio Clips。Audio Listener就相当于耳朵,是用来听声音的,Audio Source就相当于播放器,一个音源,可以播放声音,具体播放什么声音就靠Audio Clips
通常Audio Listener只有一个,默认情况下Audio Listener就在Main Camera中,是固定的
接下来就要在游戏场景中摆放各种声音的内容,然后播放让Audio Listener来听
所以我们要导入音频资源
导入之后我们再导入一下bgm资源
然后我们就要播放声音了
我们在Hierarchy栏创建一个空项目命名为SoundManager,我们用这个项目来管理我们游戏的所有音效
然后我们要写一下代码来控制声音,所以创建一个C#脚本命名为SoundManager,然后我们将这个脚本挂到空项目SoundManager上
我们还需要给这SoundManager项目添加一个Audio Source来播放音效
我们让SoundManager项目可以挂载所有我们需要的声音效果,每当我使用某个音效的时候,我们将Audio Clip放到Audio Source里面然后将它播放,每切换到不同的声音时我们放进去不同的Audio Clip
然后我们写一下代码,我们要先获得自身的Audio Source的引用,然后我们需要很多的Audio Clip来存储我们的音效,这里有跳跃的声音,受伤的声音,吃樱桃的声音,还有敌人死亡时的声音,敌人死亡的音效我们后面在单独做,我们这里主要针对人物来设置音效
我们回到unity设置一下这几个引用
有很多音效可以自己挑选
接下来我们创建不同的函数来将不同的音效输入到Audio Source播放
当我们人物跳跃或者吃到樱桃就会播放SoundManager脚本中的函数
怎么从playercontroller访问SoundManager的函数呢,前面我们消灭敌人那里我们是生成了一个Enemy的实例,然后获得自身的组件调用死亡函数。这里我们不这么做,因为这么做我们需要在三个地方执行这样的操作,这样不是很方便。
这里我们使用静态类的方法生成一个实例,然后在其他的代码就可以直接调用这个类中的函数了
我们先回到SoundManager类,我们要先将SoundManager这个类原地进行实例化,这样我们就可以用这个实体访问这个类中的所有函数
我们这里创建了一个SoundManager静态实例的对象,然后我们在Awake函数里面将自身赋值给instance,这样instance就是自身的静态实例,可以访问SoundManager类的所有对象。
Awake函数与start函数类似,不过Awake函数会在start函数前调用,Awake函数是脚本对象实例化后被调用的,而start函数是在对象的第一帧调用的,这里就不细说了
然后我们在playercontroller设置一下播放时的代码
跳跃时音效
吃到樱桃时的音效
受到伤害时的音效
这样就设置好了
还有背景音乐还没有设置,我们在SoundManager项目那里再创建一个Audio Source组件,我们把它的Audio Clip设置我们想要的bgm就行,循环播放记得勾选loop
之后这个Audio Source我们就不用管了
下面我们把敌人死亡时的音效给添加上去,我们给Enemy_Frog添加一个AudioSource,选择合适的死亡音效添加到它的Audio Clip,记得把play on awake取消勾选,不然游戏一运行就会播放这个音效
然后我们在Enemy脚本里面在死亡的函数里面播放这个音效即可
这样就ok了,我们回到游戏里实验一下就行了
最后贴一下SoundManager,Enemy还有playercontroller的代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class SoundManager : MonoBehaviour
- {
- public static SoundManager instance;//生成静态实例
- public AudioSource audioSource;//播放器
- public AudioClip hurtAudio, jumpAudio, cherryAudio;//不同的音效资源
-
- private void Awake()
- {
- instance = this;
- }
-
- //不同音效的函数
- public void JumpAudio()
- {
- audioSource.clip = jumpAudio;
- audioSource.Play();
- }
- public void HurtAudio()
- {
- audioSource.clip = hurtAudio;
- audioSource.Play();
- }
- public void CherryAudio()
- {
- audioSource.clip = cherryAudio;
- audioSource.Play();
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Enemy : MonoBehaviour
- {
- protected Animator anim;
- private AudioSource deathSource;
-
- protected virtual void Start()
- {
- anim = GetComponent<Animator>();
- deathSource = GetComponent<AudioSource>();
- }
-
- //播放完死亡动画销毁敌人
- public void DeathFall()
- {
- Destroy(gameObject);
- }
-
- //关闭碰撞器
- public void Death()
- {
- //先把敌人的碰撞器关闭,这样敌人就可以掉下去了
- GetComponent<Collider2D>().enabled = false;
- //播放死亡动画
- anim.SetTrigger("death");
- //播放死亡音效
- deathSource.Play();
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class playercontroller : MonoBehaviour
- {
- private Rigidbody2D rb;//获得Rigidbody2D组件
- private Collider2D coll;//获得Collider2D组件
- private Animator anim;//获得动画组件
- public float speed, jumpForce;//公开,设置速度和跳跃力
- public LayerMask ground;//获得地面图层
- public Transform groundCheck;//检测角色是否碰到地面
- private bool isJump, isGround;//判断是否按下空格键,判断是否在地面
- private int jumpCount;//用来设置角色是几段跳
- private int cherries;//计数变量
- public Text cherryText;//樱桃计数的UI组件
- private bool isHurt;//受击判断
-
- //初始化
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- coll = GetComponent<Collider2D>();
- anim = GetComponent<Animator>();
- }
-
- void Update()
- {
- //如果按下空格键并且在地面上
- if (Input.GetKeyDown(KeyCode.Space) && jumpCount > 0)
- {
- isJump = true;
- }
- }
- private void FixedUpdate()
- {
- isGround = Physics2D.OverlapCircle(groundCheck.position, 0.1f, ground);
- if(!isHurt)
- {
- Movement();
- }
- Jump();
- SwitchAnim();
- }
-
- //基础移动
- void Movement()
- {
- float horizontal = Input.GetAxisRaw("Horizontal");
- rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);//设置x轴的移动
- //设置角色的转向问题
- if (horizontal != 0)
- {
- transform.localScale = new Vector3(horizontal, 1, 1);
- anim.SetBool("run", true);
- }
- else
- anim.SetBool("run", false);
- }
-
- //跳跃
- void Jump()
- {
- //如果在地面设置二段跳
- if (isGround)
- {
- jumpCount = 2;
- }
- //按下跳跃键且在地面上
- if (isJump && isGround)
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- SoundManager.instance.JumpAudio();
- jumpCount--;
- isJump = false;
- }
- //按下跳跃键且不在地面上且jumpCount大于0
- else if (isJump && !isGround && jumpCount > 0)
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- SoundManager.instance.JumpAudio();
- jumpCount--;
- isJump = false;
- }
- }
-
- //切换动画
- void SwitchAnim()
- {
- //如果在下落状态
- if (rb.velocity.y < 0 && !isGround)
- {
- anim.SetBool("fall", true);
- anim.SetBool("jump", false);
- }
- //如果在跳跃状态
- if (!isGround && rb.velocity.y > 0)
- {
- anim.SetBool("jump", true);
- }
- //如果人物受击
- else if (isHurt)
- {
- anim.SetBool("hurt", true);
- if (Mathf.Abs(rb.velocity.x) < 0.1f)
- {
- anim.SetBool("hurt", false);
- isHurt = false;
- }
- }
- //如果在地面上
- else if (coll.IsTouchingLayers(ground))
- {
- anim.SetBool("fall", false);
- }
- }
-
- //判断是否碰到物品
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if(collision.tag=="Collection")
- {
- Destroy(collision.gameObject);
- cherries++;
- cherryText.text = "樱桃:" + cherries;
- SoundManager.instance.CherryAudio();
- }
- }
-
- //消灭敌人
- private void OnCollisionEnter2D(Collision2D collision)
- {
- //判断是否碰到敌人
- if(collision.gameObject.tag=="Enemy")
- {
- //消灭敌人
- Enemy enemy = collision.gameObject.GetComponent <Enemy>();//获得敌人父类引用
- if(anim.GetBool("fall"))//判断是否下落跳到敌人头上消灭敌人
- {
- enemy.Death();//调用父类函数
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- anim.SetBool("jump", true);
- }
- //如果人物在敌人的左边
- else if(transform.position.x<collision.gameObject.transform.position.x)
- {
- rb.velocity = new Vector2(-8, rb.velocity.y);
- isHurt = true;
- SoundManager.instance.HurtAudio();
- }
- //如果人物在敌人的右边
- else if(transform.position.x>collision.gameObject.transform.position.x)
- {
- rb.velocity = new Vector2(8, rb.velocity.y);
- isHurt = true;
- SoundManager.instance.HurtAudio();
- }
- }
- }
- }

如有错漏之处,敬请指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。