diff --git a/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/CoilSoundComponentInspector.cs b/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/CoilSoundComponentInspector.cs index d1bd0a02b..f710accf1 100644 --- a/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/CoilSoundComponentInspector.cs +++ b/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/CoilSoundComponentInspector.cs @@ -34,7 +34,7 @@ public override VisualElement CreateInspectorGUI() var inspectorUi = inspectorXml.Instantiate(); root.Add(inspectorUi); var coilNameDropdown = root.Q("coil-name"); - var coilNameProp = serializedObject.FindProperty(nameof(CoilSoundComponent._coilName)); + var coilNameProp = serializedObject.FindProperty(nameof(CoilSoundComponent.CoilName)); var availableCoils = GetAvailableCoils(); ConfigureDropdown(coilNameDropdown, coilNameProp, availableCoils); return root; diff --git a/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/SwitchSoundComponentInspector.cs b/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/SwitchSoundComponentInspector.cs index 25aa7cc48..57fc0d2e8 100644 --- a/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/SwitchSoundComponentInspector.cs +++ b/VisualPinball.Unity/VisualPinball.Unity.Editor/Sound/SwitchSoundComponentInspector.cs @@ -34,7 +34,7 @@ public override VisualElement CreateInspectorGUI() var inspectorUi = inspectorXml.Instantiate(); root.Add(inspectorUi); var switchNameDropdown = root.Q("switch-name"); - var switchNameProp = serializedObject.FindProperty(nameof(SwitchSoundComponent._switchName)); + var switchNameProp = serializedObject.FindProperty(nameof(SwitchSoundComponent.SwitchName)); var availableSwitches = GetAvailableSwitches(); ConfigureDropdown(switchNameDropdown, switchNameProp, availableSwitches); return root; diff --git a/VisualPinball.Unity/VisualPinball.Unity/Sound/BinaryEventSoundComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Sound/BinaryEventSoundComponent.cs index 425568792..848c3eb3a 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Sound/BinaryEventSoundComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Sound/BinaryEventSoundComponent.cs @@ -15,7 +15,9 @@ // along with this program. If not, see . // ReSharper disable InconsistentNaming + using UnityEngine; +using UnityEngine.Serialization; namespace VisualPinball.Unity { @@ -23,27 +25,25 @@ namespace VisualPinball.Unity /// Start and or stop a sound when an event occurs that represents a binary state change, /// such as a switch closing or a coil being energized. /// - public abstract class BinaryEventSoundComponent - : EventSoundComponent - where TEventSource : class + public abstract class BinaryEventSoundComponent : EventSoundComponent where TEventSource : class { - public enum StartWhen { TurnedOn, TurnedOff }; - public enum StopWhen { Never, TurnedOn, TurnedOff }; + [FormerlySerializedAs("_startWhen")] + public StartWhen StartWhen = StartWhen.TurnedOn; - [SerializeField] private StartWhen _startWhen = StartWhen.TurnedOn; - [SerializeField] private StopWhen _stopWhen = StopWhen.Never; + [FormerlySerializedAs("_stopWhen")] + public StopWhen StopWhen = StopWhen.Never; protected override async void OnEvent(object sender, TEventArgs e) { bool isEnabled = InterpretAsBinary(e); - if ((isEnabled && _stopWhen == StopWhen.TurnedOn) || - (!isEnabled && _stopWhen == StopWhen.TurnedOff)) + if ((isEnabled && StopWhen == StopWhen.TurnedOn) || + (!isEnabled && StopWhen == StopWhen.TurnedOff)) { Stop(allowFade: true); } - if ((isEnabled && _startWhen == StartWhen.TurnedOn) || - (!isEnabled && _startWhen == StartWhen.TurnedOff)) + if ((isEnabled && StartWhen == StartWhen.TurnedOn) || + (!isEnabled && StartWhen == StartWhen.TurnedOff)) { await Play(); } @@ -51,7 +51,9 @@ protected override async void OnEvent(object sender, TEventArgs e) protected abstract bool InterpretAsBinary(TEventArgs e); - public override bool SupportsLoopingSoundAssets() - => _stopWhen != StopWhen.Never; + public override bool SupportsLoopingSoundAssets() => StopWhen != StopWhen.Never; } + + public enum StartWhen { TurnedOn, TurnedOff } + public enum StopWhen { Never, TurnedOn, TurnedOff } } diff --git a/VisualPinball.Unity/VisualPinball.Unity/Sound/CoilSoundComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Sound/CoilSoundComponent.cs index 00859f121..471a16ff5 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Sound/CoilSoundComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Sound/CoilSoundComponent.cs @@ -18,6 +18,7 @@ using UnityEngine; using System; +using UnityEngine.Serialization; namespace VisualPinball.Unity { @@ -26,10 +27,11 @@ namespace VisualPinball.Unity /// [PackAs("CoilSound")] [AddComponentMenu("Visual Pinball/Sound/Coil Sound")] - public class CoilSoundComponent : BinaryEventSoundComponent + public class CoilSoundComponent : BinaryEventSoundComponent, IPackable { - [SerializeField, HideInInspector] - public string _coilName; + [FormerlySerializedAs("_coilName")] + [HideInInspector] + public string CoilName; public override Type GetRequiredType() => typeof(ICoilDeviceComponent); @@ -42,7 +44,7 @@ protected override bool TryFindEventSource(out IApiCoil coil) } foreach (var component in GetComponents()) { - coil = player.Coil(component, _coilName); + coil = player.Coil(component, CoilName); if (coil != null) { return true; } @@ -55,5 +57,15 @@ protected override bool TryFindEventSource(out IApiCoil coil) protected override void Unsubscribe(IApiCoil coil) => coil.CoilStatusChanged -= OnEvent; protected override bool InterpretAsBinary(NoIdCoilEventArgs e) => e.IsEnergized; + + #region Packaging + + // refs are handled by SoundComponent + + public new byte[] Pack() => CoilSoundPackable.Pack(this); + + public new void Unpack(byte[] bytes) => CoilSoundPackable.Unpack(bytes, this); + + #endregion } } diff --git a/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundComponent.cs index c74389114..b077e6127 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundComponent.cs @@ -63,7 +63,6 @@ public void UnpackReferences(byte[] data, Transform root, PackNameLookup lookup, #endregion - protected override void OnEnableAfterAfterAwake() { base.OnEnableAfterAfterAwake(); diff --git a/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundPackable.cs b/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundPackable.cs index 13b7c39d2..3df836b1a 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundPackable.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Sound/SoundPackable.cs @@ -21,7 +21,7 @@ namespace VisualPinball.Unity { - public struct SoundPackable { + public class SoundPackable { public bool Interrupt; public float Volume; @@ -39,6 +39,60 @@ public static void Unpack(byte[] bytes, SoundComponent comp) } } + public class BinaryEventSoundPackable : SoundPackable + { + public StartWhen StartWhen; + public StopWhen StopWhen; + } + + public class SwitchSoundPackable : BinaryEventSoundPackable + { + public string SwitchName; + + public static byte[] Pack(SwitchSoundComponent comp) + => PackageApi.Packer.Pack(new SwitchSoundPackable { + Interrupt = comp.Interrupt, + Volume = comp.Volume, + StartWhen = comp.StartWhen, + StopWhen = comp.StopWhen, + SwitchName = comp.SwitchName + }); + + public static void Unpack(byte[] bytes, SwitchSoundComponent comp) + { + var data = PackageApi.Packer.Unpack(bytes); + comp.Interrupt = data.Interrupt; + comp.Volume = data.Volume; + comp.StartWhen = data.StartWhen; + comp.StopWhen = data.StopWhen; + comp.SwitchName = data.SwitchName; + } + } + + public class CoilSoundPackable : BinaryEventSoundPackable + { + public string CoilName; + + public static byte[] Pack(CoilSoundComponent comp) + => PackageApi.Packer.Pack(new CoilSoundPackable { + Interrupt = comp.Interrupt, + Volume = comp.Volume, + StartWhen = comp.StartWhen, + StopWhen = comp.StopWhen, + CoilName = comp.CoilName + }); + + public static void Unpack(byte[] bytes, CoilSoundComponent comp) + { + var data = PackageApi.Packer.Unpack(bytes); + comp.Interrupt = data.Interrupt; + comp.Volume = data.Volume; + comp.StartWhen = data.StartWhen; + comp.StopWhen = data.StopWhen; + comp.CoilName = data.CoilName; + } + } + public struct SoundReferencesPackable { public int SoundAssetRef; @@ -53,8 +107,8 @@ public static byte[] PackReferences(SoundComponent comp, PackagedFiles files) // pack asset var assetRef = files.AddAsset(comp.SoundAsset); - var clipRefs = comp.SoundAsset.Clips != null // pack sound files + var clipRefs = comp.SoundAsset.Clips != null ? comp.SoundAsset.Clips.Select(files.Add).ToArray() : Array.Empty(); diff --git a/VisualPinball.Unity/VisualPinball.Unity/Sound/SwitchSoundComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Sound/SwitchSoundComponent.cs index 4bab79f3d..b1f1919e3 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Sound/SwitchSoundComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Sound/SwitchSoundComponent.cs @@ -18,6 +18,7 @@ using System; using UnityEngine; +using UnityEngine.Serialization; namespace VisualPinball.Unity { @@ -26,10 +27,11 @@ namespace VisualPinball.Unity /// [PackAs("SwitchSound")] [AddComponentMenu("Visual Pinball/Sound/Switch Sound")] - public class SwitchSoundComponent : BinaryEventSoundComponent + public class SwitchSoundComponent : BinaryEventSoundComponent, IPackable { - [SerializeField, HideInInspector] - public string _switchName; + [FormerlySerializedAs("_switchName")] + [HideInInspector] + public string SwitchName; public override Type GetRequiredType() => typeof(ISwitchDeviceComponent); @@ -42,7 +44,7 @@ protected override bool TryFindEventSource(out IApiSwitch @switch) } foreach (var component in GetComponents()) { - @switch = player.Switch(component, _switchName); + @switch = player.Switch(component, SwitchName); if (@switch != null) { return true; } @@ -55,5 +57,15 @@ protected override bool TryFindEventSource(out IApiSwitch @switch) protected override void Unsubscribe(IApiSwitch eventSource) => eventSource.Switch -= OnEvent; protected override bool InterpretAsBinary(SwitchEventArgs e) => e.IsEnabled; + + #region Packaging + + // refs are handled by SoundComponent + + public new byte[] Pack() => SwitchSoundPackable.Pack(this); + + public new void Unpack(byte[] bytes) => SwitchSoundPackable.Unpack(bytes, this); + + #endregion } }