GameFramework---声音(八)

特别提示: 本系列基于Unity 2019.4.8,框架版本GameFramework 2021.05.31

本系列博客地址: 传送门

Sound组件提供管理声音和声音组的功能,用户可以自定义一个声音的音量、是 2D 声音还是 3D 声音,甚至是直接绑定到某个实体上跟随实体移动。

一、常见用法

获取声音组件

SoundComponent soundComponent = GameEntry.GetComponent<SoundComponent>();

获取指定声音组

public ISoundGroup GetSoundGroup(string soundGroupName)

增加声音组

public bool AddSoundGroup(string soundGroupName, int soundAgentHelperCount)

播放声音

public int PlaySound(string soundAssetName, string soundGroupName)
public int PlaySound(string soundAssetName, string soundGroupName, int priority)
public int PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams)
public int PlaySound(string soundAssetName, string soundGroupName, Entity bindingEntity)
public int PlaySound(string soundAssetName, string soundGroupName, Vector3 worldPosition)
public int PlaySound(string soundAssetName, string soundGroupName, object userData)
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams)
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)

停止播放声音

public bool StopSound(int serialId)
public bool StopSound(int serialId, float fadeOutSeconds)

暂停播放声音

public void PauseSound(int serialId)
public void PauseSound(int serialId, float fadeOutSeconds)

暂恢复播放声音

public void ResumeSound(int serialId)
public void ResumeSound(int serialId, float fadeInSeconds)

二、配置

1、需要配置SoundGroup分组,其中AgentHelperCount是声音代理辅助器数量,可以理解为生成AudioSource的个数,最少一个不然没声音在这里插入图片描述
2、指定一个声音混合器,混合器中的分组要跟SoundGroup的对应在这里插入图片描述

三、实践

先看一下demo实现效果

  • 写了一个SoundComponent拓展,用来设置音量大小,声音开关,播放不同类型声音
using GameFramework;
using GameFramework.Sound;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

public static class Sound
{

	public static void PlayMusic(this SoundComponent soundComponent, string assetsName)
	{
		PlaySoundParams playSoundParams = PlaySoundParams.Create();
		playSoundParams.MuteInSoundGroup = false;

		playSoundParams.Priority = 64;
		playSoundParams.Loop = true;
		playSoundParams.VolumeInSoundGroup = 1f;
		playSoundParams.SpatialBlend = 0f;
		Component.Sound.PlaySound($"Assets/Audio/{assetsName}.mp3", "Music", playSoundParams);
	}
	public static void PlaySound(this SoundComponent soundComponent, string assetsName)
	{

		PlaySoundParams playSoundParams = PlaySoundParams.Create();
		playSoundParams.Priority = 30;
		playSoundParams.Loop = false;
		playSoundParams.VolumeInSoundGroup = 1f;
		playSoundParams.SpatialBlend = 0f;
		Component.Sound.PlaySound($"Assets/Audio/{assetsName}.wav", "SoundEffect", playSoundParams);
	}
	public static void PlayUISound(this SoundComponent soundComponent, string assetsName)
	{

		PlaySoundParams playSoundParams = PlaySoundParams.Create();
		playSoundParams.Priority = 30;
		playSoundParams.Loop = false;
		playSoundParams.VolumeInSoundGroup = 1f;
		playSoundParams.SpatialBlend = 0f;
		Component.Sound.PlaySound($"Assets/Audio/{assetsName}.wav", "UISoundEffect", playSoundParams);
	}
	public static void SetVolume(this SoundComponent soundComponent, string soundGroupName, float volume)
	{
		if (string.IsNullOrEmpty(soundGroupName))
		{
			Log.Error("Sound group is invalid.");
			return;
		}

		ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
		if (soundGroup == null)
		{
			Log.Error("Sound group '{0}' is invalid.", soundGroupName);
			return;
		}

		soundGroup.Volume = volume;

	//	Component.Setting.SetFloat(string.Format("Setting.{0}Volume", soundGroupName), volume);

	}
	public static void Mute(this SoundComponent soundComponent, string soundGroupName, bool mute)
	{
		if (string.IsNullOrEmpty(soundGroupName))
		{
			Log.Error("Sound group is invalid.");
			return;
		}

		ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
		if (soundGroup == null)
		{
			Log.Error("Sound group '{0}' is invalid.", soundGroupName);
			return;
		}

		soundGroup.Mute = mute;

	//	Component.Setting.SetBool(string.Format("Setting.{0}Muted", soundGroupName), mute);

	}
	public static bool IsMuted(this SoundComponent soundComponent, string soundGroupName)
	{
		if (string.IsNullOrEmpty(soundGroupName))
		{
			Log.Error("Sound group is invalid.");
			return true;
		}

		ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
		if (soundGroup == null)
		{
			Log.Error("Sound group '{0}' is invalid.", soundGroupName);
			return true;
		}

		return soundGroup.Mute;
	}
	public static float GetVolume(this SoundComponent soundComponent, string soundGroupName)
	{
		if (string.IsNullOrEmpty(soundGroupName))
		{
			Log.Error("Sound group is invalid.");
			return 0f;
		}

		ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
		if (soundGroup == null)
		{
			Log.Error("Sound group '{0}' is invalid.", soundGroupName);
			return 0f;
		}

		return soundGroup.Volume;
	}
}

  • 完善UIForm_Setting脚本的Toggle,Slider,Btn的绑定事件,完整代码可以去demo里看
    在这里插入图片描述
  • 在ProcedureExample流程初始化声音设置
    在这里插入图片描述

Demo地址: 传送门

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值