天天看點

GameFramework---聲音(八)一、常見用法二、配置三、實踐

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

本系列部落格位址: 傳送門

Sound元件提供管理聲音和聲音組的功能,使用者可以自定義一個聲音的音量、是 2D 聲音還是 3D 聲音,甚至是直接綁定到某個實體上跟随實體移動。

一、常見用法

擷取聲音元件

擷取指定聲音組

增加聲音組

播放聲音

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的個數,最少一個不然沒聲音

GameFramework---聲音(八)一、常見用法二、配置三、實踐

2、指定一個聲音混合器,混合器中的分組要跟SoundGroup的對應

GameFramework---聲音(八)一、常見用法二、配置三、實踐

三、實踐

先看一下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裡看
    GameFramework---聲音(八)一、常見用法二、配置三、實踐
  • 在ProcedureExample流程初始化聲音設定
    GameFramework---聲音(八)一、常見用法二、配置三、實踐

Demo位址: 傳送門

繼續閱讀