From 93bb6ddd06cb2f743f35e0e98208c62b52ebcc85 Mon Sep 17 00:00:00 2001 From: Ashley Canning Date: Fri, 15 Mar 2024 11:47:59 +0000 Subject: [PATCH 1/5] Introduce AudioSystem to write back the current state of an audio source --- .../Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef | 3 +- .../DCL.ECSComponents.AudioSource.asmdef | 3 +- .../Interfaces/InternalECSComponentModels.cs | 7 + .../ECS7/Systems/AudioSourceSystem.meta | 3 + .../ECS7Plugin.Systems.AudioSource.asmdef | 25 +++ ...ECS7Plugin.Systems.AudioSource.asmdef.meta | 3 + .../AudioSourceSystem/ECSAudioSourceSystem.cs | 53 ++++++ .../ECSAudioSourceSystem.cs.meta | 3 + .../ECS7/Systems/ECS7Plugin.Systems.asmdef | 3 +- .../Systems/SystemsContext/SystemsContext.cs | 8 +- .../DCL/Components/Audio/DCLAudioSource.cs | 23 +++ .../DecentralandProtocol/AudioSource.gen.cs | 171 +++++++++++++++++- 12 files changed, 294 insertions(+), 11 deletions(-) create mode 100644 unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem.meta create mode 100644 unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef create mode 100644 unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef.meta create mode 100644 unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs create mode 100644 unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs.meta diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef index ca9aea3d26..50ab302c3f 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef @@ -39,7 +39,8 @@ "GUID:7b4a5d0cf82e544daa512b41aec35b00", "GUID:acb0f5633d7c47aba96ee6b61c872d94", "GUID:1e10040ab6604cbe8ad0921047d49e65", - "GUID:ac62e852826a4b36aeb22931dad73edb" + "GUID:ac62e852826a4b36aeb22931dad73edb", + "GUID:f51ebe6a0ceec4240a699833d6309b23" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/DCL.ECSComponents.AudioSource.asmdef b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/DCL.ECSComponents.AudioSource.asmdef index 15a4f3213e..6eb9b4cb44 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/DCL.ECSComponents.AudioSource.asmdef +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/DCL.ECSComponents.AudioSource.asmdef @@ -27,7 +27,8 @@ "GUID:760a1d365aad58044916992b072cf2a6", "GUID:140f4a13272cd0842ab22d3faca39af2", "GUID:9cccce9925d3495d8a5e4fa5b25f54a5", - "GUID:710b2089e5df4140b0d3e30fbc20d9bd" + "GUID:710b2089e5df4140b0d3e30fbc20d9bd", + "GUID:f51ebe6a0ceec4240a699833d6309b23" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/InternalECSComponents/Interfaces/InternalECSComponentModels.cs b/unity-renderer/Assets/DCLPlugins/ECS7/InternalECSComponents/Interfaces/InternalECSComponentModels.cs index 742d2b55e4..1d9519ce0e 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/InternalECSComponents/Interfaces/InternalECSComponentModels.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/InternalECSComponents/Interfaces/InternalECSComponentModels.cs @@ -114,6 +114,13 @@ public InternalRenderers(IList renderers) public struct InternalAudioSource : IInternalComponent { public bool dirty { get; set; } + public bool isPlaying; + public bool loop; + public float volume; + public float pitch; + public string audioClipUrl; + public float speed; + public float decay; public AudioSource audioSource; } diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem.meta b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem.meta new file mode 100644 index 0000000000..9997a95a63 --- /dev/null +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 61b7cb51d05b47a1a722372f24ad2259 +timeCreated: 1710500198 \ No newline at end of file diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef new file mode 100644 index 0000000000..43d39d86a9 --- /dev/null +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef @@ -0,0 +1,25 @@ +{ + "name": "ECS7Plugin.Systems.AudioSource", + "rootNamespace": "", + "references": [ + "GUID:9cccce9925d3495d8a5e4fa5b25f54a5", + "GUID:9239602a072d4e52b5f1cae20ab2b459", + "GUID:0a17d39e1dc14c038aa3c6483a185df0", + "GUID:37d2c04574c1d480d8c817e2a7c578e7", + "GUID:97d8897529779cb49bebd400c7f402a6", + "GUID:ac62e852826a4b36aeb22931dad73edb", + "GUID:1e10040ab6604cbe8ad0921047d49e65", + "GUID:a881b57670b1d2747a6d7f9e32b63230", + "GUID:3c7b57a14671040bd8c549056adc04f5", + "GUID:acb0f5633d7c47aba96ee6b61c872d94" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef.meta b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef.meta new file mode 100644 index 0000000000..9e4279eaa3 --- /dev/null +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECS7Plugin.Systems.AudioSource.asmdef.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cfa7a9aba4cb4fdfac7f5a9abfb2c2c8 +timeCreated: 1710500255 \ No newline at end of file diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs new file mode 100644 index 0000000000..8c3a0e350b --- /dev/null +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs @@ -0,0 +1,53 @@ +using DCL.ECS7; +using DCL.ECS7.ComponentWrapper.Generic; +using DCL.ECS7.InternalComponents; +using DCL.ECSComponents; +using DCL.ECSRuntime; +using System.Collections.Generic; +using UnityEngine; + +namespace ECSSystems.AudioSourceSystem +{ + public class ECSAudioSourceSystem + { + private readonly IInternalECSComponent audioSourceComponent; + private readonly IReadOnlyDictionary componentsWriter; + private readonly WrappedComponentPool> audioSourcePool; + + public ECSAudioSourceSystem( + IInternalECSComponent audioSourceComponent, + IReadOnlyDictionary componentsWriter, + WrappedComponentPool> audioSourcePool) + { + this.audioSourceComponent = audioSourceComponent; + this.componentsWriter = componentsWriter; + this.audioSourcePool = audioSourcePool; + } + + public void Update() + { + var audioSourceComponentList = audioSourceComponent.GetForAll(); + + // Loop through every audio source component + foreach (var component in audioSourceComponentList) + { + var scene = component.key1; + var entity = component.key2; + var model = component.value.model; + + var wrappedComponent = audioSourcePool.Get(); + wrappedComponent.WrappedComponent.Model.Playing = model.audioSource.isPlaying; + wrappedComponent.WrappedComponent.Model.Loop = model.loop; + wrappedComponent.WrappedComponent.Model.Volume = model.volume; + wrappedComponent.WrappedComponent.Model.Pitch = model.pitch; + wrappedComponent.WrappedComponent.Model.AudioClipUrl ??= model.audioClipUrl; + wrappedComponent.WrappedComponent.Model.CurrentTime = model.audioSource.time; + + if (componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer)) + { + writer.Put(entity, ComponentID.AUDIO_SOURCE, wrappedComponent); + } + } + } + } +} diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs.meta b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs.meta new file mode 100644 index 0000000000..d09735b467 --- /dev/null +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c92bbbaacb654a92894db9701b96e26b +timeCreated: 1710500294 \ No newline at end of file diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECS7Plugin.Systems.asmdef b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECS7Plugin.Systems.asmdef index 007bcefaa6..aa5143aa47 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECS7Plugin.Systems.asmdef +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECS7Plugin.Systems.asmdef @@ -46,7 +46,8 @@ "GUID:1e10040ab6604cbe8ad0921047d49e65", "GUID:c76ce68567754cd0abab86915b7ba63e", "GUID:ed6f26f965aa4a39b494b55b335924b9", - "GUID:e55bb9116cf54f04a57453fc981dc81d" + "GUID:e55bb9116cf54f04a57453fc981dc81d", + "GUID:cfa7a9aba4cb4fdfac7f5a9abfb2c2c8" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/SystemsContext/SystemsContext.cs b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/SystemsContext/SystemsContext.cs index 28aba7a396..5a9a8ff781 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/SystemsContext/SystemsContext.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/SystemsContext/SystemsContext.cs @@ -16,14 +16,14 @@ public record SystemsContext public readonly WrappedComponentPool> TransformPool; public readonly WrappedComponentPool> VideoEventPool; public readonly WrappedComponentPool> RaycastResultPool; + public readonly WrappedComponentPool> AudioSourcePool; public readonly WrappedComponentPool> GltfContainerLoadingStatePool; public readonly WrappedComponentPool> EngineInfoPool; public readonly WrappedComponentPool> UiCanvasInformationPool; public readonly WrappedComponentPool> PointerEventsResultPool; public readonly WrappedComponentPool> TweenStatePool; - public SystemsContext( - IReadOnlyDictionary componentWriters, + public SystemsContext(IReadOnlyDictionary componentWriters, IInternalECSComponents internalEcsComponents, IComponentGroups componentGroups, ECSComponent billboards, @@ -37,7 +37,8 @@ public SystemsContext( WrappedComponentPool> engineInfoPool, WrappedComponentPool> uiCanvasInformationPool, WrappedComponentPool> pointerEventsResultPool, - WrappedComponentPool> tweenStatePool) + WrappedComponentPool> tweenStatePool, + WrappedComponentPool> audioSourcePool) { this.internalEcsComponents = internalEcsComponents; this.componentGroups = componentGroups; @@ -54,5 +55,6 @@ public SystemsContext( UiCanvasInformationPool = uiCanvasInformationPool; PointerEventsResultPool = pointerEventsResultPool; TweenStatePool = tweenStatePool; + AudioSourcePool = audioSourcePool; } } diff --git a/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs b/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs index c5ba8a5b94..1bd69217d9 100644 --- a/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs +++ b/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs @@ -9,6 +9,22 @@ namespace DCL.Components { + + public static class AudioSourceExtension { + public static bool IsReversePitch(this AudioSource source) { + return source.pitch < 0f; + } + + public static float GetClipRemainingTime(this AudioSource source) { + // Calculate the remainingTime of the given AudioSource, + // if we keep playing with the same pitch. + float remainingTime = (source.clip.length - source.time) / source.pitch; + return source.IsReversePitch() ? + (source.clip.length + remainingTime) : + remainingTime; + } + } + public class DCLAudioSource : BaseComponent, IOutOfSceneBoundariesHandler { [System.Serializable] @@ -250,6 +266,13 @@ private void ApplyLoadedAudioClip(DCLAudioClip clip) { //To remove a pesky and quite unlikely warning when the audiosource is out of scenebounds audioSource.Play(); + + IEnumerator WaitTillClipDonePlaying() + { + yield return new WaitForSeconds(audioSource.GetClipRemainingTime()); + model.playing = false; + + } } playedAtTimestamp = model.playedAtTimestamp; diff --git a/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs b/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs index 4f18c5895e..f6bd3efa93 100644 --- a/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs +++ b/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs @@ -25,16 +25,18 @@ static AudioSourceReflection() { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( "Ci5kZWNlbnRyYWxhbmQvc2RrL2NvbXBvbmVudHMvYXVkaW9fc291cmNlLnBy", - "b3RvEhtkZWNlbnRyYWxhbmQuc2RrLmNvbXBvbmVudHMiowEKDVBCQXVkaW9T", + "b3RvEhtkZWNlbnRyYWxhbmQuc2RrLmNvbXBvbmVudHMiiwIKDVBCQXVkaW9T", "b3VyY2USFAoHcGxheWluZxgBIAEoCEgAiAEBEhMKBnZvbHVtZRgCIAEoAkgB", "iAEBEhEKBGxvb3AYAyABKAhIAogBARISCgVwaXRjaBgEIAEoAkgDiAEBEhYK", - "DmF1ZGlvX2NsaXBfdXJsGAUgASgJQgoKCF9wbGF5aW5nQgkKB192b2x1bWVC", - "BwoFX2xvb3BCCAoGX3BpdGNoQhSqAhFEQ0wuRUNTQ29tcG9uZW50c2IGcHJv", - "dG8z")); + "DmF1ZGlvX2NsaXBfdXJsGAUgASgJEhkKDGN1cnJlbnRfdGltZRgGIAEoAkgE", + "iAEBEhIKBXNwZWVkGAcgASgCSAWIAQESEgoFZGVjYXkYCCABKAJIBogBAUIK", + "CghfcGxheWluZ0IJCgdfdm9sdW1lQgcKBV9sb29wQggKBl9waXRjaEIPCg1f", + "Y3VycmVudF90aW1lQggKBl9zcGVlZEIICgZfZGVjYXlCFKoCEURDTC5FQ1ND", + "b21wb25lbnRzYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::DCL.ECSComponents.PBAudioSource), global::DCL.ECSComponents.PBAudioSource.Parser, new[]{ "Playing", "Volume", "Loop", "Pitch", "AudioClipUrl" }, new[]{ "Playing", "Volume", "Loop", "Pitch" }, null, null, null) + new pbr::GeneratedClrTypeInfo(typeof(global::DCL.ECSComponents.PBAudioSource), global::DCL.ECSComponents.PBAudioSource.Parser, new[]{ "Playing", "Volume", "Loop", "Pitch", "AudioClipUrl", "CurrentTime", "Speed", "Decay" }, new[]{ "Playing", "Volume", "Loop", "Pitch", "CurrentTime", "Speed", "Decay" }, null, null, null) })); } #endregion @@ -93,6 +95,9 @@ public PBAudioSource(PBAudioSource other) : this() { loop_ = other.loop_; pitch_ = other.pitch_; audioClipUrl_ = other.audioClipUrl_; + currentTime_ = other.currentTime_; + speed_ = other.speed_; + decay_ = other.decay_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -229,6 +234,90 @@ public string AudioClipUrl { } } + /// Field number for the "current_time" field. + public const int CurrentTimeFieldNumber = 6; + private float currentTime_; + /// + /// specifies the current playback time of the clip in seconds (default: 0). + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public float CurrentTime { + get { if ((_hasBits0 & 16) != 0) { return currentTime_; } else { return 0F; } } + set { + _hasBits0 |= 16; + currentTime_ = value; + } + } + /// Gets whether the "current_time" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public bool HasCurrentTime { + get { return (_hasBits0 & 16) != 0; } + } + /// Clears the value of the "current_time" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public void ClearCurrentTime() { + _hasBits0 &= ~16; + } + + /// Field number for the "speed" field. + public const int SpeedFieldNumber = 7; + private float speed_; + /// + /// the playback speed (default: 1.0). + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public float Speed { + get { if ((_hasBits0 & 32) != 0) { return speed_; } else { return 0F; } } + set { + _hasBits0 |= 32; + speed_ = value; + } + } + /// Gets whether the "speed" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public bool HasSpeed { + get { return (_hasBits0 & 32) != 0; } + } + /// Clears the value of the "speed" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public void ClearSpeed() { + _hasBits0 &= ~32; + } + + /// Field number for the "decay" field. + public const int DecayFieldNumber = 8; + private float decay_; + /// + /// rate at which the clip volume fades to silence (default: 1.0). + /// + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public float Decay { + get { if ((_hasBits0 & 64) != 0) { return decay_; } else { return 0F; } } + set { + _hasBits0 |= 64; + decay_ = value; + } + } + /// Gets whether the "decay" field is set + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public bool HasDecay { + get { return (_hasBits0 & 64) != 0; } + } + /// Clears the value of the "decay" field + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] + public void ClearDecay() { + _hasBits0 &= ~64; + } + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] public override bool Equals(object other) { @@ -249,6 +338,9 @@ public bool Equals(PBAudioSource other) { if (Loop != other.Loop) return false; if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Pitch, other.Pitch)) return false; if (AudioClipUrl != other.AudioClipUrl) return false; + if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(CurrentTime, other.CurrentTime)) return false; + if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Speed, other.Speed)) return false; + if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Decay, other.Decay)) return false; return Equals(_unknownFields, other._unknownFields); } @@ -261,6 +353,9 @@ public override int GetHashCode() { if (HasLoop) hash ^= Loop.GetHashCode(); if (HasPitch) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Pitch); if (AudioClipUrl.Length != 0) hash ^= AudioClipUrl.GetHashCode(); + if (HasCurrentTime) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(CurrentTime); + if (HasSpeed) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Speed); + if (HasDecay) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Decay); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -299,6 +394,18 @@ public void WriteTo(pb::CodedOutputStream output) { output.WriteRawTag(42); output.WriteString(AudioClipUrl); } + if (HasCurrentTime) { + output.WriteRawTag(53); + output.WriteFloat(CurrentTime); + } + if (HasSpeed) { + output.WriteRawTag(61); + output.WriteFloat(Speed); + } + if (HasDecay) { + output.WriteRawTag(69); + output.WriteFloat(Decay); + } if (_unknownFields != null) { _unknownFields.WriteTo(output); } @@ -329,6 +436,18 @@ public void WriteTo(pb::CodedOutputStream output) { output.WriteRawTag(42); output.WriteString(AudioClipUrl); } + if (HasCurrentTime) { + output.WriteRawTag(53); + output.WriteFloat(CurrentTime); + } + if (HasSpeed) { + output.WriteRawTag(61); + output.WriteFloat(Speed); + } + if (HasDecay) { + output.WriteRawTag(69); + output.WriteFloat(Decay); + } if (_unknownFields != null) { _unknownFields.WriteTo(ref output); } @@ -354,6 +473,15 @@ public int CalculateSize() { if (AudioClipUrl.Length != 0) { size += 1 + pb::CodedOutputStream.ComputeStringSize(AudioClipUrl); } + if (HasCurrentTime) { + size += 1 + 4; + } + if (HasSpeed) { + size += 1 + 4; + } + if (HasDecay) { + size += 1 + 4; + } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); } @@ -381,6 +509,15 @@ public void MergeFrom(PBAudioSource other) { if (other.AudioClipUrl.Length != 0) { AudioClipUrl = other.AudioClipUrl; } + if (other.HasCurrentTime) { + CurrentTime = other.CurrentTime; + } + if (other.HasSpeed) { + Speed = other.Speed; + } + if (other.HasDecay) { + Decay = other.Decay; + } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -416,6 +553,18 @@ public void MergeFrom(pb::CodedInputStream input) { AudioClipUrl = input.ReadString(); break; } + case 53: { + CurrentTime = input.ReadFloat(); + break; + } + case 61: { + Speed = input.ReadFloat(); + break; + } + case 69: { + Decay = input.ReadFloat(); + break; + } } } #endif @@ -451,6 +600,18 @@ public void MergeFrom(pb::CodedInputStream input) { AudioClipUrl = input.ReadString(); break; } + case 53: { + CurrentTime = input.ReadFloat(); + break; + } + case 61: { + Speed = input.ReadFloat(); + break; + } + case 69: { + Decay = input.ReadFloat(); + break; + } } } } From bfced7d4e4ba1a950014df3846188965cf3deaa1 Mon Sep 17 00:00:00 2001 From: Ashley Canning Date: Fri, 15 Mar 2024 11:52:47 +0000 Subject: [PATCH 2/5] Tidy up --- .../Handler/ECSAudioSourceComponentHandler.cs | 26 ++++++++----------- .../DCL/Components/Audio/DCLAudioSource.cs | 22 ---------------- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs index d64bca73ce..63b0790062 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs @@ -1,10 +1,13 @@ -using System; + +using Cysharp.Threading.Tasks; +using System; using DCL.Controllers; using DCL.ECS7.InternalComponents; using DCL.ECSRuntime; using DCL.Helpers; using DCL.Models; using DCL.SettingsCommon; +using System.Collections.Generic; using UnityEngine; using AudioSettings = DCL.SettingsCommon.AudioSettings; @@ -60,10 +63,7 @@ public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) dataStore.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue; sceneNumber.OnChange += OnCurrentSceneChanged; - audioSourceInternalComponent.PutFor(scene, entity, new InternalAudioSource() - { - audioSource = this.audioSource - }); + sbcInternalComponent.RegisterOnSceneBoundsStateChangeCallback(scene, entity, OnSceneBoundsStateChange); @@ -149,17 +149,13 @@ private void ApplyCurrentModel() audioSource.spatialBlend = 1; audioSource.dopplerLevel = 0.1f; - if (model.Playing != audioSource.isPlaying) - { - if (audioSource.isPlaying) - audioSource.Stop(); - else if (isAudioClipReady) - audioSource.Play(); - } - else if (audioSource.isPlaying) - { + if (model.Playing == audioSource.isPlaying) + return; + + if (!model.Playing && audioSource.isPlaying) audioSource.Stop(); - } + if (model.Playing && !audioSource.isPlaying && isAudioClipReady) + audioSource.Play(); } private void ApplyLoadedAudioClip(AudioClip clip) diff --git a/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs b/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs index 1bd69217d9..e7a9bbceb4 100644 --- a/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs +++ b/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs @@ -10,21 +10,6 @@ namespace DCL.Components { - public static class AudioSourceExtension { - public static bool IsReversePitch(this AudioSource source) { - return source.pitch < 0f; - } - - public static float GetClipRemainingTime(this AudioSource source) { - // Calculate the remainingTime of the given AudioSource, - // if we keep playing with the same pitch. - float remainingTime = (source.clip.length - source.time) / source.pitch; - return source.IsReversePitch() ? - (source.clip.length + remainingTime) : - remainingTime; - } - } - public class DCLAudioSource : BaseComponent, IOutOfSceneBoundariesHandler { [System.Serializable] @@ -266,13 +251,6 @@ private void ApplyLoadedAudioClip(DCLAudioClip clip) { //To remove a pesky and quite unlikely warning when the audiosource is out of scenebounds audioSource.Play(); - - IEnumerator WaitTillClipDonePlaying() - { - yield return new WaitForSeconds(audioSource.GetClipRemainingTime()); - model.playing = false; - - } } playedAtTimestamp = model.playedAtTimestamp; From 7659cf0865d97f46e9bcc7e8b36d63f1b6053de2 Mon Sep 17 00:00:00 2001 From: Ashley Canning Date: Fri, 15 Mar 2024 11:53:23 +0000 Subject: [PATCH 3/5] Missing files --- unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs | 5 +++-- .../DCLPlugins/ECS7/Systems/ECSSystemsController.cs | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs index 3b684a62c4..21019578bf 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs @@ -6,6 +6,7 @@ using DCL.ECSRuntime; using RPC.Context; using System.Collections.Generic; +using UnityEngine; namespace DCL.ECS7 { @@ -68,8 +69,8 @@ public ECS7Plugin() new WrappedComponentPool>(MAX_EXPECTED_SCENES, () => new ProtobufWrappedComponent(new PBEngineInfo())), new WrappedComponentPool>(MAX_EXPECTED_SCENES, () => new ProtobufWrappedComponent(new PBUiCanvasInformation())), new WrappedComponentPool>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent(new PBPointerEventsResult())), - new WrappedComponentPool>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent(new PBTweenState())) - ); + new WrappedComponentPool>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent(new PBTweenState())), + new WrappedComponentPool>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent(new PBAudioSource()))); systemsController = new ECSSystemsController(systemsContext); diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECSSystemsController.cs b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECSSystemsController.cs index 3136d3f2c3..e449c89980 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECSSystemsController.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/ECSSystemsController.cs @@ -1,5 +1,6 @@ using DCL; using ECSSystems.AnimationSystem; +using ECSSystems.AudioSourceSystem; using ECSSystems.AvatarModifierAreaSystem; using ECSSystems.BillboardSystem; using ECSSystems.CameraSystem; @@ -38,6 +39,7 @@ public class ECSSystemsController : IDisposable private readonly ECSPlayerTransformSystem playerTransformSystem; private readonly ECSSceneBoundsCheckerSystem sceneBoundsCheckerSystem; private readonly ECSUiCanvasInformationSystem uiCanvasInformationSystem; + private readonly ECSAudioSourceSystem audioSourceSystem; private readonly GameObject hoverCanvas; private readonly GameObject scenesUi; private readonly IWorldState worldState; @@ -98,6 +100,11 @@ public ECSSystemsController(SystemsContext context) context.ComponentWriters, context.RaycastResultPool); + audioSourceSystem = new ECSAudioSourceSystem( + context.internalEcsComponents.audioSourceComponent, + context.ComponentWriters, + context.AudioSourcePool); + sceneBoundsCheckerSystem = new ECSSceneBoundsCheckerSystem( DataStore.i.ecs7.scenes, context.internalEcsComponents.sceneBoundsCheckComponent, @@ -178,7 +185,8 @@ public ECSSystemsController(SystemsContext context) billboardSystem.Update, videoPlayerSystem.Update, uiCanvasInformationSystem.Update, - animationSystem.Update + animationSystem.Update, + audioSourceSystem.Update }; lateUpdateSystems = new ECS7System[] From 787cc40f598adf8655272c0701097410eceef00d Mon Sep 17 00:00:00 2001 From: Ashley Canning Date: Mon, 18 Mar 2024 11:25:55 +0000 Subject: [PATCH 4/5] Update protocol --- .../DecentralandProtocol/AudioSource.gen.cs | 127 ++++++------------ 1 file changed, 40 insertions(+), 87 deletions(-) diff --git a/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs b/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs index f6bd3efa93..b0cc200ee3 100644 --- a/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs +++ b/unity-renderer/Assets/Scripts/MainScripts/DCL/DecentralandProtocol/AudioSource.gen.cs @@ -25,18 +25,17 @@ static AudioSourceReflection() { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( "Ci5kZWNlbnRyYWxhbmQvc2RrL2NvbXBvbmVudHMvYXVkaW9fc291cmNlLnBy", - "b3RvEhtkZWNlbnRyYWxhbmQuc2RrLmNvbXBvbmVudHMiiwIKDVBCQXVkaW9T", + "b3RvEhtkZWNlbnRyYWxhbmQuc2RrLmNvbXBvbmVudHMi7wEKDVBCQXVkaW9T", "b3VyY2USFAoHcGxheWluZxgBIAEoCEgAiAEBEhMKBnZvbHVtZRgCIAEoAkgB", "iAEBEhEKBGxvb3AYAyABKAhIAogBARISCgVwaXRjaBgEIAEoAkgDiAEBEhYK", "DmF1ZGlvX2NsaXBfdXJsGAUgASgJEhkKDGN1cnJlbnRfdGltZRgGIAEoAkgE", - "iAEBEhIKBXNwZWVkGAcgASgCSAWIAQESEgoFZGVjYXkYCCABKAJIBogBAUIK", - "CghfcGxheWluZ0IJCgdfdm9sdW1lQgcKBV9sb29wQggKBl9waXRjaEIPCg1f", - "Y3VycmVudF90aW1lQggKBl9zcGVlZEIICgZfZGVjYXlCFKoCEURDTC5FQ1ND", - "b21wb25lbnRzYgZwcm90bzM=")); + "iAEBEhMKBmdsb2JhbBgHIAEoCEgFiAEBQgoKCF9wbGF5aW5nQgkKB192b2x1", + "bWVCBwoFX2xvb3BCCAoGX3BpdGNoQg8KDV9jdXJyZW50X3RpbWVCCQoHX2ds", + "b2JhbEIUqgIRRENMLkVDU0NvbXBvbmVudHNiBnByb3RvMw==")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::DCL.ECSComponents.PBAudioSource), global::DCL.ECSComponents.PBAudioSource.Parser, new[]{ "Playing", "Volume", "Loop", "Pitch", "AudioClipUrl", "CurrentTime", "Speed", "Decay" }, new[]{ "Playing", "Volume", "Loop", "Pitch", "CurrentTime", "Speed", "Decay" }, null, null, null) + new pbr::GeneratedClrTypeInfo(typeof(global::DCL.ECSComponents.PBAudioSource), global::DCL.ECSComponents.PBAudioSource.Parser, new[]{ "Playing", "Volume", "Loop", "Pitch", "AudioClipUrl", "CurrentTime", "Global" }, new[]{ "Playing", "Volume", "Loop", "Pitch", "CurrentTime", "Global" }, null, null, null) })); } #endregion @@ -53,6 +52,13 @@ static AudioSourceReflection() { /// Note that the `audio_clip_url` is not actually a URL, but rather the path of a file bundled with /// the scene and declared in its manifest. The name was chosen because the URL use-case will /// eventually be supported. + /// + /// `playing=true` when it's previously `playing=true` + /// a) if clip is playing and `current_time` is NOT SET, the clip remains in the current `current_time` + /// b) if clip is stopped or `current_time` is set, the clip is played from the `current_time` (if set) or from the beginning + /// + /// If other property (volume, loop, pitch) is changed while playing, the clip is keep playing with the new properties + /// Changing `audio_clip_url` while playing stops the current clip and plays the new one (as a new instance) /// public sealed partial class PBAudioSource : pb::IMessage #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE @@ -96,8 +102,7 @@ public PBAudioSource(PBAudioSource other) : this() { pitch_ = other.pitch_; audioClipUrl_ = other.audioClipUrl_; currentTime_ = other.currentTime_; - speed_ = other.speed_; - decay_ = other.decay_; + global_ = other.global_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } @@ -262,62 +267,34 @@ public void ClearCurrentTime() { _hasBits0 &= ~16; } - /// Field number for the "speed" field. - public const int SpeedFieldNumber = 7; - private float speed_; + /// Field number for the "global" field. + public const int GlobalFieldNumber = 7; + private bool global_; /// - /// the playback speed (default: 1.0). + /// whether the audio plays at constant volume across the scene. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public float Speed { - get { if ((_hasBits0 & 32) != 0) { return speed_; } else { return 0F; } } + public bool Global { + get { if ((_hasBits0 & 32) != 0) { return global_; } else { return false; } } set { _hasBits0 |= 32; - speed_ = value; + global_ = value; } } - /// Gets whether the "speed" field is set + /// Gets whether the "global" field is set [global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public bool HasSpeed { + public bool HasGlobal { get { return (_hasBits0 & 32) != 0; } } - /// Clears the value of the "speed" field + /// Clears the value of the "global" field [global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public void ClearSpeed() { + public void ClearGlobal() { _hasBits0 &= ~32; } - /// Field number for the "decay" field. - public const int DecayFieldNumber = 8; - private float decay_; - /// - /// rate at which the clip volume fades to silence (default: 1.0). - /// - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public float Decay { - get { if ((_hasBits0 & 64) != 0) { return decay_; } else { return 0F; } } - set { - _hasBits0 |= 64; - decay_ = value; - } - } - /// Gets whether the "decay" field is set - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public bool HasDecay { - get { return (_hasBits0 & 64) != 0; } - } - /// Clears the value of the "decay" field - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] - public void ClearDecay() { - _hasBits0 &= ~64; - } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] public override bool Equals(object other) { @@ -339,8 +316,7 @@ public bool Equals(PBAudioSource other) { if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Pitch, other.Pitch)) return false; if (AudioClipUrl != other.AudioClipUrl) return false; if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(CurrentTime, other.CurrentTime)) return false; - if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Speed, other.Speed)) return false; - if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals(Decay, other.Decay)) return false; + if (Global != other.Global) return false; return Equals(_unknownFields, other._unknownFields); } @@ -354,8 +330,7 @@ public override int GetHashCode() { if (HasPitch) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Pitch); if (AudioClipUrl.Length != 0) hash ^= AudioClipUrl.GetHashCode(); if (HasCurrentTime) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(CurrentTime); - if (HasSpeed) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Speed); - if (HasDecay) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(Decay); + if (HasGlobal) hash ^= Global.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } @@ -398,13 +373,9 @@ public void WriteTo(pb::CodedOutputStream output) { output.WriteRawTag(53); output.WriteFloat(CurrentTime); } - if (HasSpeed) { - output.WriteRawTag(61); - output.WriteFloat(Speed); - } - if (HasDecay) { - output.WriteRawTag(69); - output.WriteFloat(Decay); + if (HasGlobal) { + output.WriteRawTag(56); + output.WriteBool(Global); } if (_unknownFields != null) { _unknownFields.WriteTo(output); @@ -440,13 +411,9 @@ public void WriteTo(pb::CodedOutputStream output) { output.WriteRawTag(53); output.WriteFloat(CurrentTime); } - if (HasSpeed) { - output.WriteRawTag(61); - output.WriteFloat(Speed); - } - if (HasDecay) { - output.WriteRawTag(69); - output.WriteFloat(Decay); + if (HasGlobal) { + output.WriteRawTag(56); + output.WriteBool(Global); } if (_unknownFields != null) { _unknownFields.WriteTo(ref output); @@ -476,11 +443,8 @@ public int CalculateSize() { if (HasCurrentTime) { size += 1 + 4; } - if (HasSpeed) { - size += 1 + 4; - } - if (HasDecay) { - size += 1 + 4; + if (HasGlobal) { + size += 1 + 1; } if (_unknownFields != null) { size += _unknownFields.CalculateSize(); @@ -512,11 +476,8 @@ public void MergeFrom(PBAudioSource other) { if (other.HasCurrentTime) { CurrentTime = other.CurrentTime; } - if (other.HasSpeed) { - Speed = other.Speed; - } - if (other.HasDecay) { - Decay = other.Decay; + if (other.HasGlobal) { + Global = other.Global; } _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); } @@ -557,12 +518,8 @@ public void MergeFrom(pb::CodedInputStream input) { CurrentTime = input.ReadFloat(); break; } - case 61: { - Speed = input.ReadFloat(); - break; - } - case 69: { - Decay = input.ReadFloat(); + case 56: { + Global = input.ReadBool(); break; } } @@ -604,12 +561,8 @@ public void MergeFrom(pb::CodedInputStream input) { CurrentTime = input.ReadFloat(); break; } - case 61: { - Speed = input.ReadFloat(); - break; - } - case 69: { - Decay = input.ReadFloat(); + case 56: { + Global = input.ReadBool(); break; } } From d9e0f28be0078c2194d17c0223508b78dfb01929 Mon Sep 17 00:00:00 2001 From: Ashley Canning Date: Mon, 18 Mar 2024 11:59:33 +0000 Subject: [PATCH 5/5] Removed enumerator allocation Tidy ups --- .../Assets/DCLPlugins/ECS7/ECS7Plugin.cs | 1 - .../Handler/ECSAudioSourceComponentHandler.cs | 2 -- .../AudioSourceSystem/ECSAudioSourceSystem.cs | 14 +++++--------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs index 21019578bf..01a754731b 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs @@ -6,7 +6,6 @@ using DCL.ECSRuntime; using RPC.Context; using System.Collections.Generic; -using UnityEngine; namespace DCL.ECS7 { diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs index 63b0790062..329f06653e 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioSource/Handler/ECSAudioSourceComponentHandler.cs @@ -1,5 +1,4 @@  -using Cysharp.Threading.Tasks; using System; using DCL.Controllers; using DCL.ECS7.InternalComponents; @@ -7,7 +6,6 @@ using DCL.Helpers; using DCL.Models; using DCL.SettingsCommon; -using System.Collections.Generic; using UnityEngine; using AudioSettings = DCL.SettingsCommon.AudioSettings; diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs index 8c3a0e350b..9589675fce 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/AudioSourceSystem/ECSAudioSourceSystem.cs @@ -2,9 +2,7 @@ using DCL.ECS7.ComponentWrapper.Generic; using DCL.ECS7.InternalComponents; using DCL.ECSComponents; -using DCL.ECSRuntime; using System.Collections.Generic; -using UnityEngine; namespace ECSSystems.AudioSourceSystem { @@ -29,12 +27,14 @@ public void Update() var audioSourceComponentList = audioSourceComponent.GetForAll(); // Loop through every audio source component - foreach (var component in audioSourceComponentList) + for(var i = 0; i < audioSourceComponentList.Count; i++) { + var component = audioSourceComponentList[i]; var scene = component.key1; - var entity = component.key2; + long entity = component.key2; var model = component.value.model; + if (!componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer)) continue; var wrappedComponent = audioSourcePool.Get(); wrappedComponent.WrappedComponent.Model.Playing = model.audioSource.isPlaying; wrappedComponent.WrappedComponent.Model.Loop = model.loop; @@ -42,11 +42,7 @@ public void Update() wrappedComponent.WrappedComponent.Model.Pitch = model.pitch; wrappedComponent.WrappedComponent.Model.AudioClipUrl ??= model.audioClipUrl; wrappedComponent.WrappedComponent.Model.CurrentTime = model.audioSource.time; - - if (componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer)) - { - writer.Put(entity, ComponentID.AUDIO_SOURCE, wrappedComponent); - } + writer.Put(entity, ComponentID.AUDIO_SOURCE, wrappedComponent); } } }