init
This commit is contained in:
@@ -0,0 +1,434 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Sound;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认声音代理辅助器。
|
||||
/// </summary>
|
||||
public class DefaultSoundAgentHelper : SoundAgentHelperBase
|
||||
{
|
||||
private Transform m_CachedTransform = null;
|
||||
private AudioSource m_AudioSource = null;
|
||||
private EntityLogic m_BindingEntityLogic = null;
|
||||
private float m_VolumeWhenPause = 0f;
|
||||
private bool m_ApplicationPauseFlag = false;
|
||||
private EventHandler<ResetSoundAgentEventArgs> m_ResetSoundAgentEventHandler = null;
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前是否正在播放。
|
||||
/// </summary>
|
||||
public override bool IsPlaying
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.isPlaying;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音长度。
|
||||
/// </summary>
|
||||
public override float Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.clip != null ? m_AudioSource.clip.length : 0f;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置播放位置。
|
||||
/// </summary>
|
||||
public override float Time
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.time;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.time = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否静音。
|
||||
/// </summary>
|
||||
public override bool Mute
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.mute;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.mute = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否循环播放。
|
||||
/// </summary>
|
||||
public override bool Loop
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.loop;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.loop = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音优先级。
|
||||
/// </summary>
|
||||
public override int Priority
|
||||
{
|
||||
get
|
||||
{
|
||||
return 128 - m_AudioSource.priority;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.priority = 128 - value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置音量大小。
|
||||
/// </summary>
|
||||
public override float Volume
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.volume;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.volume = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音音调。
|
||||
/// </summary>
|
||||
public override float Pitch
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.pitch;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.pitch = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音立体声声相。
|
||||
/// </summary>
|
||||
public override float PanStereo
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.panStereo;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.panStereo = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音空间混合量。
|
||||
/// </summary>
|
||||
public override float SpatialBlend
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.spatialBlend;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.spatialBlend = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音最大距离。
|
||||
/// </summary>
|
||||
public override float MaxDistance
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.maxDistance;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
m_AudioSource.maxDistance = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音多普勒等级。
|
||||
/// </summary>
|
||||
public override float DopplerLevel
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.dopplerLevel;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.dopplerLevel = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音代理辅助器所在的混音组。
|
||||
/// </summary>
|
||||
public override AudioMixerGroup AudioMixerGroup
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioSource.outputAudioMixerGroup;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioSource.outputAudioMixerGroup = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置声音代理事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<ResetSoundAgentEventArgs> ResetSoundAgent
|
||||
{
|
||||
add
|
||||
{
|
||||
m_ResetSoundAgentEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_ResetSoundAgentEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
|
||||
public override void Play(float fadeInSeconds)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
m_AudioSource.Play();
|
||||
if (fadeInSeconds > 0f)
|
||||
{
|
||||
float volume = m_AudioSource.volume;
|
||||
m_AudioSource.volume = 0f;
|
||||
StartCoroutine(FadeToVolume(m_AudioSource, volume, fadeInSeconds));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public override void Stop(float fadeOutSeconds)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
|
||||
{
|
||||
StartCoroutine(StopCo(fadeOutSeconds));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_AudioSource.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public override void Pause(float fadeOutSeconds)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
m_VolumeWhenPause = m_AudioSource.volume;
|
||||
if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
|
||||
{
|
||||
StartCoroutine(PauseCo(fadeOutSeconds));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_AudioSource.Pause();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
|
||||
public override void Resume(float fadeInSeconds)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
m_AudioSource.UnPause();
|
||||
if (fadeInSeconds > 0f)
|
||||
{
|
||||
StartCoroutine(FadeToVolume(m_AudioSource, m_VolumeWhenPause, fadeInSeconds));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_AudioSource.volume = m_VolumeWhenPause;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置声音代理辅助器。
|
||||
/// </summary>
|
||||
public override void Reset()
|
||||
{
|
||||
m_CachedTransform.localPosition = Vector3.zero;
|
||||
m_AudioSource.clip = null;
|
||||
m_BindingEntityLogic = null;
|
||||
m_VolumeWhenPause = 0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音资源。
|
||||
/// </summary>
|
||||
/// <param name="soundAsset">声音资源。</param>
|
||||
/// <returns>是否设置声音资源成功。</returns>
|
||||
public override bool SetSoundAsset(object soundAsset)
|
||||
{
|
||||
AudioClip audioClip = soundAsset as AudioClip;
|
||||
if (audioClip == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_AudioSource.clip = audioClip;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音绑定的实体。
|
||||
/// </summary>
|
||||
/// <param name="bindingEntity">声音绑定的实体。</param>
|
||||
public override void SetBindingEntity(Entity bindingEntity)
|
||||
{
|
||||
m_BindingEntityLogic = bindingEntity.Logic;
|
||||
if (m_BindingEntityLogic != null)
|
||||
{
|
||||
UpdateAgentPosition();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_ResetSoundAgentEventHandler != null)
|
||||
{
|
||||
ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
|
||||
m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
|
||||
ReferencePool.Release(resetSoundAgentEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音所在的世界坐标。
|
||||
/// </summary>
|
||||
/// <param name="worldPosition">声音所在的世界坐标。</param>
|
||||
public override void SetWorldPosition(Vector3 worldPosition)
|
||||
{
|
||||
m_CachedTransform.position = worldPosition;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_CachedTransform = transform;
|
||||
m_AudioSource = gameObject.GetOrAddComponent<AudioSource>();
|
||||
m_AudioSource.playOnAwake = false;
|
||||
m_AudioSource.rolloffMode = AudioRolloffMode.Custom;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!m_ApplicationPauseFlag && !IsPlaying && m_AudioSource.clip != null && m_ResetSoundAgentEventHandler != null)
|
||||
{
|
||||
ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
|
||||
m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
|
||||
ReferencePool.Release(resetSoundAgentEventArgs);
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_BindingEntityLogic != null)
|
||||
{
|
||||
UpdateAgentPosition();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnApplicationPause(bool pause)
|
||||
{
|
||||
m_ApplicationPauseFlag = pause;
|
||||
}
|
||||
|
||||
private void UpdateAgentPosition()
|
||||
{
|
||||
if (m_BindingEntityLogic.Available)
|
||||
{
|
||||
m_CachedTransform.position = m_BindingEntityLogic.CachedTransform.position;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_ResetSoundAgentEventHandler != null)
|
||||
{
|
||||
ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
|
||||
m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
|
||||
ReferencePool.Release(resetSoundAgentEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator StopCo(float fadeOutSeconds)
|
||||
{
|
||||
yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
|
||||
m_AudioSource.Stop();
|
||||
}
|
||||
|
||||
private IEnumerator PauseCo(float fadeOutSeconds)
|
||||
{
|
||||
yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
|
||||
m_AudioSource.Pause();
|
||||
}
|
||||
|
||||
private IEnumerator FadeToVolume(AudioSource audioSource, float volume, float duration)
|
||||
{
|
||||
float time = 0f;
|
||||
float originalVolume = audioSource.volume;
|
||||
while (time < duration)
|
||||
{
|
||||
time += UnityEngine.Time.deltaTime;
|
||||
audioSource.volume = Mathf.Lerp(originalVolume, volume, time / duration);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
audioSource.volume = volume;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e41f05fd242c0d4fb2a5bd81ccca27c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认声音组辅助器。
|
||||
/// </summary>
|
||||
public class DefaultSoundGroupHelper : SoundGroupHelperBase
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3b0f546449448c488d755cee4d701d2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认声音辅助器。
|
||||
/// </summary>
|
||||
public class DefaultSoundHelper : SoundHelperBase
|
||||
{
|
||||
private ResourceComponent m_ResourceComponent = null;
|
||||
|
||||
/// <summary>
|
||||
/// 释放声音资源。
|
||||
/// </summary>
|
||||
/// <param name="soundAsset">要释放的声音资源。</param>
|
||||
public override void ReleaseSoundAsset(object soundAsset)
|
||||
{
|
||||
m_ResourceComponent.UnloadAsset(soundAsset);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_ResourceComponent = GameEntry.GetComponent<ResourceComponent>();
|
||||
if (m_ResourceComponent == null)
|
||||
{
|
||||
Log.Fatal("Resource component is invalid.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a025072340d72e4e8468db061720cde
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,169 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
using GameFramework.Sound;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音时加载依赖资源事件。
|
||||
/// </summary>
|
||||
public sealed class PlaySoundDependencyAssetEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音时加载依赖资源事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(PlaySoundDependencyAssetEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化播放声音时加载依赖资源事件的新实例。
|
||||
/// </summary>
|
||||
public PlaySoundDependencyAssetEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
DependencyAssetName = null;
|
||||
LoadedCount = 0;
|
||||
TotalCount = 0;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音时加载依赖资源事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音资源名称。
|
||||
/// </summary>
|
||||
public string SoundAssetName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音组名称。
|
||||
/// </summary>
|
||||
public string SoundGroupName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音参数。
|
||||
/// </summary>
|
||||
public PlaySoundParams PlaySoundParams
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取被加载的依赖资源名称。
|
||||
/// </summary>
|
||||
public string DependencyAssetName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前已加载依赖资源数量。
|
||||
/// </summary>
|
||||
public int LoadedCount
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取总共加载依赖资源数量。
|
||||
/// </summary>
|
||||
public int TotalCount
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音绑定的实体。
|
||||
/// </summary>
|
||||
public Entity BindingEntity
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建播放声音时加载依赖资源事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的播放声音时加载依赖资源事件。</returns>
|
||||
public static PlaySoundDependencyAssetEventArgs Create(GameFramework.Sound.PlaySoundDependencyAssetEventArgs e)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = (PlaySoundInfo)e.UserData;
|
||||
PlaySoundDependencyAssetEventArgs playSoundDependencyAssetEventArgs = ReferencePool.Acquire<PlaySoundDependencyAssetEventArgs>();
|
||||
playSoundDependencyAssetEventArgs.SerialId = e.SerialId;
|
||||
playSoundDependencyAssetEventArgs.SoundAssetName = e.SoundAssetName;
|
||||
playSoundDependencyAssetEventArgs.SoundGroupName = e.SoundGroupName;
|
||||
playSoundDependencyAssetEventArgs.PlaySoundParams = e.PlaySoundParams;
|
||||
playSoundDependencyAssetEventArgs.DependencyAssetName = e.DependencyAssetName;
|
||||
playSoundDependencyAssetEventArgs.LoadedCount = e.LoadedCount;
|
||||
playSoundDependencyAssetEventArgs.TotalCount = e.TotalCount;
|
||||
playSoundDependencyAssetEventArgs.BindingEntity = playSoundInfo.BindingEntity;
|
||||
playSoundDependencyAssetEventArgs.UserData = playSoundInfo.UserData;
|
||||
return playSoundDependencyAssetEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理播放声音时加载依赖资源事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
DependencyAssetName = null;
|
||||
LoadedCount = 0;
|
||||
TotalCount = 0;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82bc0837101049b47bca9dc4c0dfadb2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,158 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
using GameFramework.Sound;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音失败事件。
|
||||
/// </summary>
|
||||
public sealed class PlaySoundFailureEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音失败事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(PlaySoundFailureEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化播放声音失败事件的新实例。
|
||||
/// </summary>
|
||||
public PlaySoundFailureEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
BindingEntity = null;
|
||||
ErrorCode = 0;
|
||||
ErrorMessage = null;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音失败事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音资源名称。
|
||||
/// </summary>
|
||||
public string SoundAssetName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音组名称。
|
||||
/// </summary>
|
||||
public string SoundGroupName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音参数。
|
||||
/// </summary>
|
||||
public PlaySoundParams PlaySoundParams
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音绑定的实体。
|
||||
/// </summary>
|
||||
public Entity BindingEntity
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取错误码。
|
||||
/// </summary>
|
||||
public PlaySoundErrorCode ErrorCode
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取错误信息。
|
||||
/// </summary>
|
||||
public string ErrorMessage
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建播放声音失败事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的播放声音失败事件。</returns>
|
||||
public static PlaySoundFailureEventArgs Create(GameFramework.Sound.PlaySoundFailureEventArgs e)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = (PlaySoundInfo)e.UserData;
|
||||
PlaySoundFailureEventArgs playSoundFailureEventArgs = ReferencePool.Acquire<PlaySoundFailureEventArgs>();
|
||||
playSoundFailureEventArgs.SerialId = e.SerialId;
|
||||
playSoundFailureEventArgs.SoundAssetName = e.SoundAssetName;
|
||||
playSoundFailureEventArgs.SoundGroupName = e.SoundGroupName;
|
||||
playSoundFailureEventArgs.PlaySoundParams = e.PlaySoundParams;
|
||||
playSoundFailureEventArgs.BindingEntity = playSoundInfo.BindingEntity;
|
||||
playSoundFailureEventArgs.ErrorCode = e.ErrorCode;
|
||||
playSoundFailureEventArgs.ErrorMessage = e.ErrorMessage;
|
||||
playSoundFailureEventArgs.UserData = playSoundInfo.UserData;
|
||||
ReferencePool.Release(playSoundInfo);
|
||||
return playSoundFailureEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理播放声音失败事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
BindingEntity = null;
|
||||
ErrorCode = 0;
|
||||
ErrorMessage = null;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e72fc153d76f6e04e9b0036f95f75b3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,66 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
internal sealed class PlaySoundInfo : IReference
|
||||
{
|
||||
private Entity m_BindingEntity;
|
||||
private Vector3 m_WorldPosition;
|
||||
private object m_UserData;
|
||||
|
||||
public PlaySoundInfo()
|
||||
{
|
||||
m_BindingEntity = null;
|
||||
m_WorldPosition = Vector3.zero;
|
||||
m_UserData = null;
|
||||
}
|
||||
|
||||
public Entity BindingEntity
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_BindingEntity;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 WorldPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_WorldPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public object UserData
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_UserData;
|
||||
}
|
||||
}
|
||||
|
||||
public static PlaySoundInfo Create(Entity bindingEntity, Vector3 worldPosition, object userData)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = ReferencePool.Acquire<PlaySoundInfo>();
|
||||
playSoundInfo.m_BindingEntity = bindingEntity;
|
||||
playSoundInfo.m_WorldPosition = worldPosition;
|
||||
playSoundInfo.m_UserData = userData;
|
||||
return playSoundInfo;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_BindingEntity = null;
|
||||
m_WorldPosition = Vector3.zero;
|
||||
m_UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7b0ac4f756eac174ba485dae0218a1a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,134 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
using GameFramework.Sound;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音成功事件。
|
||||
/// </summary>
|
||||
public sealed class PlaySoundSuccessEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音成功事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(PlaySoundSuccessEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化播放声音成功事件的新实例。
|
||||
/// </summary>
|
||||
public PlaySoundSuccessEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundAgent = null;
|
||||
Duration = 0f;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音成功事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音资源名称。
|
||||
/// </summary>
|
||||
public string SoundAssetName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用于播放的声音代理。
|
||||
/// </summary>
|
||||
public ISoundAgent SoundAgent
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取加载持续时间。
|
||||
/// </summary>
|
||||
public float Duration
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音绑定的实体。
|
||||
/// </summary>
|
||||
public Entity BindingEntity
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建播放声音成功事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的播放声音成功事件。</returns>
|
||||
public static PlaySoundSuccessEventArgs Create(GameFramework.Sound.PlaySoundSuccessEventArgs e)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = (PlaySoundInfo)e.UserData;
|
||||
PlaySoundSuccessEventArgs playSoundSuccessEventArgs = ReferencePool.Acquire<PlaySoundSuccessEventArgs>();
|
||||
playSoundSuccessEventArgs.SerialId = e.SerialId;
|
||||
playSoundSuccessEventArgs.SoundAssetName = e.SoundAssetName;
|
||||
playSoundSuccessEventArgs.SoundAgent = e.SoundAgent;
|
||||
playSoundSuccessEventArgs.Duration = e.Duration;
|
||||
playSoundSuccessEventArgs.BindingEntity = playSoundInfo.BindingEntity;
|
||||
playSoundSuccessEventArgs.UserData = playSoundInfo.UserData;
|
||||
ReferencePool.Release(playSoundInfo);
|
||||
return playSoundSuccessEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理播放声音成功事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundAgent = null;
|
||||
Duration = 0f;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ba92a17225be4a41a9824d08651692f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,145 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
using GameFramework.Sound;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音更新事件。
|
||||
/// </summary>
|
||||
public sealed class PlaySoundUpdateEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 播放声音更新事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(PlaySoundUpdateEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化播放声音更新事件的新实例。
|
||||
/// </summary>
|
||||
public PlaySoundUpdateEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
Progress = 0f;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音更新事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音资源名称。
|
||||
/// </summary>
|
||||
public string SoundAssetName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音组名称。
|
||||
/// </summary>
|
||||
public string SoundGroupName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取播放声音参数。
|
||||
/// </summary>
|
||||
public PlaySoundParams PlaySoundParams
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取加载声音进度。
|
||||
/// </summary>
|
||||
public float Progress
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音绑定的实体。
|
||||
/// </summary>
|
||||
public Entity BindingEntity
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建播放声音更新事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的播放声音更新事件。</returns>
|
||||
public static PlaySoundUpdateEventArgs Create(GameFramework.Sound.PlaySoundUpdateEventArgs e)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = (PlaySoundInfo)e.UserData;
|
||||
PlaySoundUpdateEventArgs playSoundUpdateEventArgs = ReferencePool.Acquire<PlaySoundUpdateEventArgs>();
|
||||
playSoundUpdateEventArgs.SerialId = e.SerialId;
|
||||
playSoundUpdateEventArgs.SoundAssetName = e.SoundAssetName;
|
||||
playSoundUpdateEventArgs.SoundGroupName = e.SoundGroupName;
|
||||
playSoundUpdateEventArgs.PlaySoundParams = e.PlaySoundParams;
|
||||
playSoundUpdateEventArgs.Progress = e.Progress;
|
||||
playSoundUpdateEventArgs.BindingEntity = playSoundInfo.BindingEntity;
|
||||
playSoundUpdateEventArgs.UserData = playSoundInfo.UserData;
|
||||
return playSoundUpdateEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理播放声音更新事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
SoundAssetName = null;
|
||||
SoundGroupName = null;
|
||||
PlaySoundParams = null;
|
||||
Progress = 0f;
|
||||
BindingEntity = null;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edad8ee73e896ab4cb00c9d37667699d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,188 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework.Sound;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 声音代理辅助器基类。
|
||||
/// </summary>
|
||||
public abstract class SoundAgentHelperBase : MonoBehaviour, ISoundAgentHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前是否正在播放。
|
||||
/// </summary>
|
||||
public abstract bool IsPlaying
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音长度。
|
||||
/// </summary>
|
||||
public abstract float Length
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置播放位置。
|
||||
/// </summary>
|
||||
public abstract float Time
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否静音。
|
||||
/// </summary>
|
||||
public abstract bool Mute
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否循环播放。
|
||||
/// </summary>
|
||||
public abstract bool Loop
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音优先级。
|
||||
/// </summary>
|
||||
public abstract int Priority
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置音量大小。
|
||||
/// </summary>
|
||||
public abstract float Volume
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音音调。
|
||||
/// </summary>
|
||||
public abstract float Pitch
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音立体声声相。
|
||||
/// </summary>
|
||||
public abstract float PanStereo
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音空间混合量。
|
||||
/// </summary>
|
||||
public abstract float SpatialBlend
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音最大距离。
|
||||
/// </summary>
|
||||
public abstract float MaxDistance
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音多普勒等级。
|
||||
/// </summary>
|
||||
public abstract float DopplerLevel
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音代理辅助器所在的混音组。
|
||||
/// </summary>
|
||||
public abstract AudioMixerGroup AudioMixerGroup
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置声音代理事件。
|
||||
/// </summary>
|
||||
public abstract event EventHandler<ResetSoundAgentEventArgs> ResetSoundAgent;
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
|
||||
public abstract void Play(float fadeInSeconds);
|
||||
|
||||
/// <summary>
|
||||
/// 停止播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public abstract void Stop(float fadeOutSeconds);
|
||||
|
||||
/// <summary>
|
||||
/// 暂停播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public abstract void Pause(float fadeOutSeconds);
|
||||
|
||||
/// <summary>
|
||||
/// 恢复播放声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
|
||||
public abstract void Resume(float fadeInSeconds);
|
||||
|
||||
/// <summary>
|
||||
/// 重置声音代理辅助器。
|
||||
/// </summary>
|
||||
public abstract void Reset();
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音资源。
|
||||
/// </summary>
|
||||
/// <param name="soundAsset">声音资源。</param>
|
||||
/// <returns>是否设置声音资源成功。</returns>
|
||||
public abstract bool SetSoundAsset(object soundAsset);
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音绑定的实体。
|
||||
/// </summary>
|
||||
/// <param name="bindingEntity">声音绑定的实体。</param>
|
||||
public abstract void SetBindingEntity(Entity bindingEntity);
|
||||
|
||||
/// <summary>
|
||||
/// 设置声音所在的世界坐标。
|
||||
/// </summary>
|
||||
/// <param name="worldPosition">声音所在的世界坐标。</param>
|
||||
public abstract void SetWorldPosition(Vector3 worldPosition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67ec8ad22a10aae41a982dbdd607ceef
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,74 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
public sealed partial class SoundComponent : GameFrameworkComponent
|
||||
{
|
||||
[Serializable]
|
||||
private sealed class SoundGroup
|
||||
{
|
||||
[SerializeField]
|
||||
private string m_Name = null;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_AvoidBeingReplacedBySamePriority = false;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_Mute = false;
|
||||
|
||||
[SerializeField, Range(0f, 1f)]
|
||||
private float m_Volume = 1f;
|
||||
|
||||
[SerializeField]
|
||||
private int m_AgentHelperCount = 1;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
}
|
||||
|
||||
public bool AvoidBeingReplacedBySamePriority
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AvoidBeingReplacedBySamePriority;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Mute
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Mute;
|
||||
}
|
||||
}
|
||||
|
||||
public float Volume
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Volume;
|
||||
}
|
||||
}
|
||||
|
||||
public int AgentHelperCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AgentHelperCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50effaea047572b4fae5b3500f1fc109
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,692 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Resource;
|
||||
#if UNITY_5_3
|
||||
using GameFramework.Scene;
|
||||
#endif
|
||||
using GameFramework.Sound;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 声音组件。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("Game Framework/Sound")]
|
||||
public sealed partial class SoundComponent : GameFrameworkComponent
|
||||
{
|
||||
private const int DefaultPriority = 0;
|
||||
|
||||
private ISoundManager m_SoundManager = null;
|
||||
private EventComponent m_EventComponent = null;
|
||||
private AudioListener m_AudioListener = null;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_EnablePlaySoundUpdateEvent = false;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_EnablePlaySoundDependencyAssetEvent = false;
|
||||
|
||||
[SerializeField]
|
||||
private Transform m_InstanceRoot = null;
|
||||
|
||||
[SerializeField]
|
||||
private AudioMixer m_AudioMixer = null;
|
||||
|
||||
[SerializeField]
|
||||
private string m_SoundHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundHelper";
|
||||
|
||||
[SerializeField]
|
||||
private SoundHelperBase m_CustomSoundHelper = null;
|
||||
|
||||
[SerializeField]
|
||||
private string m_SoundGroupHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundGroupHelper";
|
||||
|
||||
[SerializeField]
|
||||
private SoundGroupHelperBase m_CustomSoundGroupHelper = null;
|
||||
|
||||
[SerializeField]
|
||||
private string m_SoundAgentHelperTypeName = "UnityGameFramework.Runtime.DefaultSoundAgentHelper";
|
||||
|
||||
[SerializeField]
|
||||
private SoundAgentHelperBase m_CustomSoundAgentHelper = null;
|
||||
|
||||
[SerializeField]
|
||||
private SoundGroup[] m_SoundGroups = null;
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音组数量。
|
||||
/// </summary>
|
||||
public int SoundGroupCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_SoundManager.SoundGroupCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取声音混响器。
|
||||
/// </summary>
|
||||
public AudioMixer AudioMixer
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioMixer;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏框架组件初始化。
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
m_SoundManager = GameFrameworkEntry.GetModule<ISoundManager>();
|
||||
if (m_SoundManager == null)
|
||||
{
|
||||
Log.Fatal("Sound manager is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_SoundManager.PlaySoundSuccess += OnPlaySoundSuccess;
|
||||
m_SoundManager.PlaySoundFailure += OnPlaySoundFailure;
|
||||
|
||||
if (m_EnablePlaySoundUpdateEvent)
|
||||
{
|
||||
m_SoundManager.PlaySoundUpdate += OnPlaySoundUpdate;
|
||||
}
|
||||
|
||||
if (m_EnablePlaySoundDependencyAssetEvent)
|
||||
{
|
||||
m_SoundManager.PlaySoundDependencyAsset += OnPlaySoundDependencyAsset;
|
||||
}
|
||||
|
||||
m_AudioListener = gameObject.GetOrAddComponent<AudioListener>();
|
||||
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
||||
#else
|
||||
ISceneManager sceneManager = GameFrameworkEntry.GetModule<ISceneManager>();
|
||||
if (sceneManager == null)
|
||||
{
|
||||
Log.Fatal("Scene manager is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
sceneManager.LoadSceneSuccess += OnLoadSceneSuccess;
|
||||
sceneManager.LoadSceneFailure += OnLoadSceneFailure;
|
||||
sceneManager.UnloadSceneSuccess += OnUnloadSceneSuccess;
|
||||
sceneManager.UnloadSceneFailure += OnUnloadSceneFailure;
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
BaseComponent baseComponent = GameEntry.GetComponent<BaseComponent>();
|
||||
if (baseComponent == null)
|
||||
{
|
||||
Log.Fatal("Base component is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_EventComponent = GameEntry.GetComponent<EventComponent>();
|
||||
if (m_EventComponent == null)
|
||||
{
|
||||
Log.Fatal("Event component is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (baseComponent.EditorResourceMode)
|
||||
{
|
||||
m_SoundManager.SetResourceManager(baseComponent.EditorResourceHelper);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_SoundManager.SetResourceManager(GameFrameworkEntry.GetModule<IResourceManager>());
|
||||
}
|
||||
|
||||
SoundHelperBase soundHelper = Helper.CreateHelper(m_SoundHelperTypeName, m_CustomSoundHelper);
|
||||
if (soundHelper == null)
|
||||
{
|
||||
Log.Error("Can not create sound helper.");
|
||||
return;
|
||||
}
|
||||
|
||||
soundHelper.name = "Sound Helper";
|
||||
Transform transform = soundHelper.transform;
|
||||
transform.SetParent(this.transform);
|
||||
transform.localScale = Vector3.one;
|
||||
|
||||
m_SoundManager.SetSoundHelper(soundHelper);
|
||||
|
||||
if (m_InstanceRoot == null)
|
||||
{
|
||||
m_InstanceRoot = new GameObject("Sound Instances").transform;
|
||||
m_InstanceRoot.SetParent(gameObject.transform);
|
||||
m_InstanceRoot.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_SoundGroups.Length; i++)
|
||||
{
|
||||
if (!AddSoundGroup(m_SoundGroups[i].Name, m_SoundGroups[i].AvoidBeingReplacedBySamePriority, m_SoundGroups[i].Mute, m_SoundGroups[i].Volume, m_SoundGroups[i].AgentHelperCount))
|
||||
{
|
||||
Log.Warning("Add sound group '{0}' failure.", m_SoundGroups[i].Name);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
SceneManager.sceneUnloaded -= OnSceneUnloaded;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在指定声音组。
|
||||
/// </summary>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <returns>指定声音组是否存在。</returns>
|
||||
public bool HasSoundGroup(string soundGroupName)
|
||||
{
|
||||
return m_SoundManager.HasSoundGroup(soundGroupName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定声音组。
|
||||
/// </summary>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <returns>要获取的声音组。</returns>
|
||||
public ISoundGroup GetSoundGroup(string soundGroupName)
|
||||
{
|
||||
return m_SoundManager.GetSoundGroup(soundGroupName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有声音组。
|
||||
/// </summary>
|
||||
/// <returns>所有声音组。</returns>
|
||||
public ISoundGroup[] GetAllSoundGroups()
|
||||
{
|
||||
return m_SoundManager.GetAllSoundGroups();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有声音组。
|
||||
/// </summary>
|
||||
/// <param name="results">所有声音组。</param>
|
||||
public void GetAllSoundGroups(List<ISoundGroup> results)
|
||||
{
|
||||
m_SoundManager.GetAllSoundGroups(results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加声音组。
|
||||
/// </summary>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="soundAgentHelperCount">声音代理辅助器数量。</param>
|
||||
/// <returns>是否增加声音组成功。</returns>
|
||||
public bool AddSoundGroup(string soundGroupName, int soundAgentHelperCount)
|
||||
{
|
||||
return AddSoundGroup(soundGroupName, false, false, 1f, soundAgentHelperCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加声音组。
|
||||
/// </summary>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="soundGroupAvoidBeingReplacedBySamePriority">声音组中的声音是否避免被同优先级声音替换。</param>
|
||||
/// <param name="soundGroupMute">声音组是否静音。</param>
|
||||
/// <param name="soundGroupVolume">声音组音量。</param>
|
||||
/// <param name="soundAgentHelperCount">声音代理辅助器数量。</param>
|
||||
/// <returns>是否增加声音组成功。</returns>
|
||||
public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplacedBySamePriority, bool soundGroupMute, float soundGroupVolume, int soundAgentHelperCount)
|
||||
{
|
||||
if (m_SoundManager.HasSoundGroup(soundGroupName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SoundGroupHelperBase soundGroupHelper = Helper.CreateHelper(m_SoundGroupHelperTypeName, m_CustomSoundGroupHelper, SoundGroupCount);
|
||||
if (soundGroupHelper == null)
|
||||
{
|
||||
Log.Error("Can not create sound group helper.");
|
||||
return false;
|
||||
}
|
||||
|
||||
soundGroupHelper.name = Utility.Text.Format("Sound Group - {0}", soundGroupName);
|
||||
Transform transform = soundGroupHelper.transform;
|
||||
transform.SetParent(m_InstanceRoot);
|
||||
transform.localScale = Vector3.one;
|
||||
|
||||
if (m_AudioMixer != null)
|
||||
{
|
||||
AudioMixerGroup[] audioMixerGroups = m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}", soundGroupName));
|
||||
if (audioMixerGroups.Length > 0)
|
||||
{
|
||||
soundGroupHelper.AudioMixerGroup = audioMixerGroups[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
soundGroupHelper.AudioMixerGroup = m_AudioMixer.FindMatchingGroups("Master")[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_SoundManager.AddSoundGroup(soundGroupName, soundGroupAvoidBeingReplacedBySamePriority, soundGroupMute, soundGroupVolume, soundGroupHelper))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < soundAgentHelperCount; i++)
|
||||
{
|
||||
if (!AddSoundAgentHelper(soundGroupName, soundGroupHelper, i))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有正在加载声音的序列编号。
|
||||
/// </summary>
|
||||
/// <returns>所有正在加载声音的序列编号。</returns>
|
||||
public int[] GetAllLoadingSoundSerialIds()
|
||||
{
|
||||
return m_SoundManager.GetAllLoadingSoundSerialIds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有正在加载声音的序列编号。
|
||||
/// </summary>
|
||||
/// <param name="results">所有正在加载声音的序列编号。</param>
|
||||
public void GetAllLoadingSoundSerialIds(List<int> results)
|
||||
{
|
||||
m_SoundManager.GetAllLoadingSoundSerialIds(results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否正在加载声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">声音序列编号。</param>
|
||||
/// <returns>是否正在加载声音。</returns>
|
||||
public bool IsLoadingSound(int serialId)
|
||||
{
|
||||
return m_SoundManager.IsLoadingSound(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, priority, null, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, DefaultPriority, playSoundParams, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="bindingEntity">声音绑定的实体。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, Entity bindingEntity)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, bindingEntity, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="worldPosition">声音所在的世界坐标。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, Vector3 worldPosition)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, worldPosition, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, object userData)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, DefaultPriority, null, null, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, null, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <param name="bindingEntity">声音绑定的实体。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Entity bindingEntity)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, bindingEntity, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <param name="bindingEntity">声音绑定的实体。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Entity bindingEntity, object userData)
|
||||
{
|
||||
return m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, PlaySoundInfo.Create(bindingEntity, Vector3.zero, userData));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <param name="worldPosition">声音所在的世界坐标。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Vector3 worldPosition)
|
||||
{
|
||||
return PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, worldPosition, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放声音。
|
||||
/// </summary>
|
||||
/// <param name="soundAssetName">声音资源名称。</param>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="priority">加载声音资源的优先级。</param>
|
||||
/// <param name="playSoundParams">播放声音参数。</param>
|
||||
/// <param name="worldPosition">声音所在的世界坐标。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>声音的序列编号。</returns>
|
||||
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, Vector3 worldPosition, object userData)
|
||||
{
|
||||
return m_SoundManager.PlaySound(soundAssetName, soundGroupName, priority, playSoundParams, PlaySoundInfo.Create(null, worldPosition, userData));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要停止播放声音的序列编号。</param>
|
||||
/// <returns>是否停止播放声音成功。</returns>
|
||||
public bool StopSound(int serialId)
|
||||
{
|
||||
return m_SoundManager.StopSound(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要停止播放声音的序列编号。</param>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
/// <returns>是否停止播放声音成功。</returns>
|
||||
public bool StopSound(int serialId, float fadeOutSeconds)
|
||||
{
|
||||
return m_SoundManager.StopSound(serialId, fadeOutSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止所有已加载的声音。
|
||||
/// </summary>
|
||||
public void StopAllLoadedSounds()
|
||||
{
|
||||
m_SoundManager.StopAllLoadedSounds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止所有已加载的声音。
|
||||
/// </summary>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public void StopAllLoadedSounds(float fadeOutSeconds)
|
||||
{
|
||||
m_SoundManager.StopAllLoadedSounds(fadeOutSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止所有正在加载的声音。
|
||||
/// </summary>
|
||||
public void StopAllLoadingSounds()
|
||||
{
|
||||
m_SoundManager.StopAllLoadingSounds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要暂停播放声音的序列编号。</param>
|
||||
public void PauseSound(int serialId)
|
||||
{
|
||||
m_SoundManager.PauseSound(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要暂停播放声音的序列编号。</param>
|
||||
/// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
|
||||
public void PauseSound(int serialId, float fadeOutSeconds)
|
||||
{
|
||||
m_SoundManager.PauseSound(serialId, fadeOutSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要恢复播放声音的序列编号。</param>
|
||||
public void ResumeSound(int serialId)
|
||||
{
|
||||
m_SoundManager.ResumeSound(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复播放声音。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要恢复播放声音的序列编号。</param>
|
||||
/// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
|
||||
public void ResumeSound(int serialId, float fadeInSeconds)
|
||||
{
|
||||
m_SoundManager.ResumeSound(serialId, fadeInSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加声音代理辅助器。
|
||||
/// </summary>
|
||||
/// <param name="soundGroupName">声音组名称。</param>
|
||||
/// <param name="soundGroupHelper">声音组辅助器。</param>
|
||||
/// <param name="index">声音代理辅助器索引。</param>
|
||||
/// <returns>是否增加声音代理辅助器成功。</returns>
|
||||
private bool AddSoundAgentHelper(string soundGroupName, SoundGroupHelperBase soundGroupHelper, int index)
|
||||
{
|
||||
SoundAgentHelperBase soundAgentHelper = Helper.CreateHelper(m_SoundAgentHelperTypeName, m_CustomSoundAgentHelper, index);
|
||||
if (soundAgentHelper == null)
|
||||
{
|
||||
Log.Error("Can not create sound agent helper.");
|
||||
return false;
|
||||
}
|
||||
|
||||
soundAgentHelper.name = Utility.Text.Format("Sound Agent Helper - {0} - {1}", soundGroupName, index);
|
||||
Transform transform = soundAgentHelper.transform;
|
||||
transform.SetParent(soundGroupHelper.transform);
|
||||
transform.localScale = Vector3.one;
|
||||
|
||||
if (m_AudioMixer != null)
|
||||
{
|
||||
AudioMixerGroup[] audioMixerGroups = m_AudioMixer.FindMatchingGroups(Utility.Text.Format("Master/{0}/{1}", soundGroupName, index));
|
||||
if (audioMixerGroups.Length > 0)
|
||||
{
|
||||
soundAgentHelper.AudioMixerGroup = audioMixerGroups[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
soundAgentHelper.AudioMixerGroup = soundGroupHelper.AudioMixerGroup;
|
||||
}
|
||||
}
|
||||
|
||||
m_SoundManager.AddSoundAgentHelper(soundGroupName, soundAgentHelper);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnPlaySoundSuccess(object sender, GameFramework.Sound.PlaySoundSuccessEventArgs e)
|
||||
{
|
||||
PlaySoundInfo playSoundInfo = (PlaySoundInfo)e.UserData;
|
||||
if (playSoundInfo != null)
|
||||
{
|
||||
SoundAgentHelperBase soundAgentHelper = (SoundAgentHelperBase)e.SoundAgent.Helper;
|
||||
if (playSoundInfo.BindingEntity != null)
|
||||
{
|
||||
soundAgentHelper.SetBindingEntity(playSoundInfo.BindingEntity);
|
||||
}
|
||||
else
|
||||
{
|
||||
soundAgentHelper.SetWorldPosition(playSoundInfo.WorldPosition);
|
||||
}
|
||||
}
|
||||
|
||||
m_EventComponent.Fire(this, PlaySoundSuccessEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnPlaySoundFailure(object sender, GameFramework.Sound.PlaySoundFailureEventArgs e)
|
||||
{
|
||||
string logMessage = Utility.Text.Format("Play sound failure, asset name '{0}', sound group name '{1}', error code '{2}', error message '{3}'.", e.SoundAssetName, e.SoundGroupName, e.ErrorCode, e.ErrorMessage);
|
||||
if (e.ErrorCode == PlaySoundErrorCode.IgnoredDueToLowPriority)
|
||||
{
|
||||
Log.Info(logMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Warning(logMessage);
|
||||
}
|
||||
|
||||
m_EventComponent.Fire(this, PlaySoundFailureEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnPlaySoundUpdate(object sender, GameFramework.Sound.PlaySoundUpdateEventArgs e)
|
||||
{
|
||||
m_EventComponent.Fire(this, PlaySoundUpdateEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnPlaySoundDependencyAsset(object sender, GameFramework.Sound.PlaySoundDependencyAssetEventArgs e)
|
||||
{
|
||||
m_EventComponent.Fire(this, PlaySoundDependencyAssetEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnLoadSceneSuccess(object sender, GameFramework.Scene.LoadSceneSuccessEventArgs e)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void OnLoadSceneFailure(object sender, GameFramework.Scene.LoadSceneFailureEventArgs e)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void OnUnloadSceneSuccess(object sender, GameFramework.Scene.UnloadSceneSuccessEventArgs e)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void OnUnloadSceneFailure(object sender, GameFramework.Scene.UnloadSceneFailureEventArgs e)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void OnSceneUnloaded(Scene scene)
|
||||
{
|
||||
RefreshAudioListener();
|
||||
}
|
||||
|
||||
private void RefreshAudioListener()
|
||||
{
|
||||
m_AudioListener.enabled = FindObjectsOfType<AudioListener>().Length <= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed5533de69c4e5a4dabc7c23fce1fa98
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework.Sound;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 声音组辅助器基类。
|
||||
/// </summary>
|
||||
public abstract class SoundGroupHelperBase : MonoBehaviour, ISoundGroupHelper
|
||||
{
|
||||
[SerializeField]
|
||||
private AudioMixerGroup m_AudioMixerGroup = null;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置声音组辅助器所在的混音组。
|
||||
/// </summary>
|
||||
public AudioMixerGroup AudioMixerGroup
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_AudioMixerGroup;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_AudioMixerGroup = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b1aa5b8d134ac846a5a83fd661c9e92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework.Sound;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 声音辅助器基类。
|
||||
/// </summary>
|
||||
public abstract class SoundHelperBase : MonoBehaviour, ISoundHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 释放声音资源。
|
||||
/// </summary>
|
||||
/// <param name="soundAsset">要释放的声音资源。</param>
|
||||
public abstract void ReleaseSoundAsset(object soundAsset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d63865838389abe428ccc3cee437a862
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user