引入 BGM 切换+文档初版草稿
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Event;
|
||||
using Sound;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
@@ -53,6 +54,15 @@ namespace CustomComponent
|
||||
/// </summary>
|
||||
[SerializeField] private UnityEvent _onPuzzleCompleted = new UnityEvent();
|
||||
|
||||
[SerializeField] [Tooltip("开始拼装时是否自动切换 BGM。")]
|
||||
private bool _autoSwitchBgmOnStart = true;
|
||||
|
||||
[SerializeField] [Tooltip("拼装阶段 BGM Id。")]
|
||||
private int _combineBgmId = 5;
|
||||
|
||||
[SerializeField] [Tooltip("拼装结束后是否自动恢复被打断的 BGM(带进度)。")]
|
||||
private bool _autoRestoreInterruptedBgm = true;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Runtime State
|
||||
@@ -105,6 +115,10 @@ namespace CustomComponent
|
||||
/// </summary>
|
||||
private int _nextLevelStructureIndex = 0;
|
||||
|
||||
private bool _hasInterruptedBgm = false;
|
||||
private int _interruptedBgmId = 0;
|
||||
private float _interruptedBgmTimeSeconds = 0f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Query
|
||||
@@ -139,6 +153,16 @@ namespace CustomComponent
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GameEntry.Event.Subscribe(CombineCompletedEventArgs.EventId, OnCombineCompleted);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(CombineCompletedEventArgs.EventId, OnCombineCompleted);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Level Lifecycle
|
||||
@@ -167,6 +191,7 @@ namespace CustomComponent
|
||||
return null;
|
||||
}
|
||||
|
||||
SwitchToCombineBgm();
|
||||
_nextLevelStructureIndex++;
|
||||
return serialId;
|
||||
}
|
||||
@@ -178,6 +203,7 @@ namespace CustomComponent
|
||||
{
|
||||
_formController?.CloseUI();
|
||||
ClearRuntimeContext();
|
||||
RestoreInterruptedBgmIfNeeded();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -435,6 +461,53 @@ namespace CustomComponent
|
||||
_isPaused = false;
|
||||
}
|
||||
|
||||
private void OnCombineCompleted(object sender, GameFramework.Event.GameEventArgs e)
|
||||
{
|
||||
if (!(e is CombineCompletedEventArgs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RestoreInterruptedBgmIfNeeded();
|
||||
}
|
||||
|
||||
private void SwitchToCombineBgm()
|
||||
{
|
||||
if (!_autoSwitchBgmOnStart || _combineBgmId <= 0 || GameEntry.Sound == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_hasInterruptedBgm && GameEntry.Sound.TryGetCurrentBgmSnapshot(out int bgmId, out float timeSeconds))
|
||||
{
|
||||
if (bgmId != _combineBgmId)
|
||||
{
|
||||
_hasInterruptedBgm = true;
|
||||
_interruptedBgmId = bgmId;
|
||||
_interruptedBgmTimeSeconds = Mathf.Max(0f, timeSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
GameEntry.Sound.PlayBGM(_combineBgmId);
|
||||
}
|
||||
|
||||
private void RestoreInterruptedBgmIfNeeded()
|
||||
{
|
||||
if (!_autoRestoreInterruptedBgm || !_hasInterruptedBgm || GameEntry.Sound == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_interruptedBgmId > 0)
|
||||
{
|
||||
GameEntry.Sound.PlayBGM(_interruptedBgmId, _interruptedBgmTimeSeconds);
|
||||
}
|
||||
|
||||
_hasInterruptedBgm = false;
|
||||
_interruptedBgmId = 0;
|
||||
_interruptedBgmTimeSeconds = 0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前拖拽根节点。
|
||||
/// </summary>
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomComponent
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Story/Directive/Change BGM", fileName = "Directive_ChangeBGM")]
|
||||
public sealed class StoryChangeBgmDirectiveAsset : StoryDirectiveAsset
|
||||
{
|
||||
[SerializeField] [Tooltip("BGM 配置表 Id;<=0 表示停止当前 BGM。")]
|
||||
private int _bgmId = 0;
|
||||
|
||||
public override string ActionName => "ChangeBGM";
|
||||
|
||||
public override void Execute(StoryDirectorComponent director)
|
||||
{
|
||||
if (director == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
director.ExecuteChangeBgm(_bgmId);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2f756304e6e46b48c7cabf5887971fc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Event;
|
||||
using Sound;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
@@ -273,6 +274,27 @@ namespace CustomComponent
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteChangeBgm(int bgmId)
|
||||
{
|
||||
if (GameEntry.Sound == null)
|
||||
{
|
||||
Log.Warning("StoryDirector change BGM failed. Sound component is missing.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bgmId <= 0)
|
||||
{
|
||||
GameEntry.Sound.StopMusic();
|
||||
return;
|
||||
}
|
||||
|
||||
int? serialId = GameEntry.Sound.PlayBGM(bgmId);
|
||||
if (!serialId.HasValue)
|
||||
{
|
||||
Log.Warning("StoryDirector change BGM failed. bgmId={0}", bgmId.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteEndChapter(int chapterId)
|
||||
{
|
||||
int finalChapterId = chapterId > 0
|
||||
|
||||
@@ -31,15 +31,6 @@ namespace DataTable
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取背景音乐编号。
|
||||
/// </summary>
|
||||
public int BackgroundMusicId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public override bool ParseDataRow(string dataRowString, object userData)
|
||||
{
|
||||
string[] columnStrings = dataRowString.Split(DataTableExtension.DataSplitSeparators);
|
||||
@@ -53,7 +44,6 @@ namespace DataTable
|
||||
m_Id = int.Parse(columnStrings[index++]);
|
||||
index++;
|
||||
AssetName = columnStrings[index++];
|
||||
BackgroundMusicId = int.Parse(columnStrings[index++]);
|
||||
|
||||
GeneratePropertyArray();
|
||||
return true;
|
||||
@@ -67,7 +57,6 @@ namespace DataTable
|
||||
{
|
||||
m_Id = binaryReader.Read7BitEncodedInt32();
|
||||
AssetName = binaryReader.ReadString();
|
||||
BackgroundMusicId = binaryReader.Read7BitEncodedInt32();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ namespace Procedure
|
||||
{
|
||||
private int _nextSceneId = 0;
|
||||
private bool _isChangeSceneComplete = false;
|
||||
private int _backgroundMusicId = 0;
|
||||
|
||||
public override bool UseNativeDialog => false;
|
||||
|
||||
@@ -59,7 +58,6 @@ namespace Procedure
|
||||
|
||||
GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(drScene.AssetName), Constant.AssetPriority.SceneAsset,
|
||||
this);
|
||||
_backgroundMusicId = drScene.BackgroundMusicId;
|
||||
}
|
||||
|
||||
protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
|
||||
@@ -110,11 +108,6 @@ namespace Procedure
|
||||
|
||||
Log.Info("Load scene '{0}' OK.", ne.SceneAssetName);
|
||||
|
||||
if (_backgroundMusicId > 0)
|
||||
{
|
||||
GameEntry.Sound.PlayBGM(_backgroundMusicId);
|
||||
}
|
||||
|
||||
_isChangeSceneComplete = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,8 @@ namespace Procedure
|
||||
|
||||
_menuFormController = new MenuFormController();
|
||||
_menuFormController.OpenUI(new MenuFormContext());
|
||||
|
||||
GameEntry.Sound.PlayBGM(1);
|
||||
}
|
||||
|
||||
protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown)
|
||||
|
||||
@@ -3,6 +3,7 @@ using DataTable;
|
||||
using Definition;
|
||||
using GameFramework.DataTable;
|
||||
using GameFramework.Sound;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace Sound
|
||||
@@ -11,8 +12,17 @@ namespace Sound
|
||||
{
|
||||
private const float FadeVolumeDuration = 1f;
|
||||
private static int? s_MusicSerialId = null;
|
||||
private static int? s_CurrentBgmId = null;
|
||||
private static float s_CurrentBgmStartOffsetSeconds = 0f;
|
||||
private static float s_CurrentBgmStartRealtime = 0f;
|
||||
|
||||
public static int? PlayBGM(this SoundComponent soundComponent, int musicId, object userData = null)
|
||||
{
|
||||
return PlayBGM(soundComponent, musicId, 0f, userData);
|
||||
}
|
||||
|
||||
public static int? PlayBGM(this SoundComponent soundComponent, int musicId, float startTimeSeconds,
|
||||
object userData = null)
|
||||
{
|
||||
soundComponent.StopMusic();
|
||||
|
||||
@@ -30,8 +40,12 @@ namespace Sound
|
||||
playSoundParams.VolumeInSoundGroup = 1f;
|
||||
playSoundParams.FadeInSeconds = FadeVolumeDuration;
|
||||
playSoundParams.SpatialBlend = 0f;
|
||||
playSoundParams.Time = Mathf.Max(0f, startTimeSeconds);
|
||||
s_MusicSerialId = soundComponent.PlaySound(AssetUtility.GetMusicAsset(drBgm.AssetName), "BGM",
|
||||
Constant.AssetPriority.MusicAsset, playSoundParams, null, userData);
|
||||
s_CurrentBgmId = musicId;
|
||||
s_CurrentBgmStartOffsetSeconds = playSoundParams.Time;
|
||||
s_CurrentBgmStartRealtime = Time.realtimeSinceStartup;
|
||||
return s_MusicSerialId;
|
||||
}
|
||||
|
||||
@@ -44,6 +58,26 @@ namespace Sound
|
||||
|
||||
soundComponent.StopSound(s_MusicSerialId.Value, FadeVolumeDuration);
|
||||
s_MusicSerialId = null;
|
||||
s_CurrentBgmId = null;
|
||||
s_CurrentBgmStartOffsetSeconds = 0f;
|
||||
s_CurrentBgmStartRealtime = 0f;
|
||||
}
|
||||
|
||||
public static bool TryGetCurrentBgmSnapshot(this SoundComponent soundComponent, out int bgmId,
|
||||
out float timeSeconds)
|
||||
{
|
||||
bgmId = 0;
|
||||
timeSeconds = 0f;
|
||||
|
||||
if (!s_MusicSerialId.HasValue || !s_CurrentBgmId.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bgmId = s_CurrentBgmId.Value;
|
||||
timeSeconds = s_CurrentBgmStartOffsetSeconds +
|
||||
Mathf.Max(0f, Time.realtimeSinceStartup - s_CurrentBgmStartRealtime);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int? PlaySE(this SoundComponent soundComponent, int uiSoundId, object userData = null)
|
||||
@@ -104,4 +138,4 @@ namespace Sound
|
||||
GameEntry.Setting.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user