From 9a9a9c8b21626069801ea44b86e63dc09ff7c842 Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Sun, 5 May 2024 17:50:04 +0200 Subject: [PATCH] feat: music which has the same tag(different id) can now be randomly played, new tracks You can say play a "menu_music" and a random track tagged as such will be played. --- Assets/Prefabs/Audio/Main Menu Music.prefab | 2 +- Assets/Prefabs/Audio/Match Music 1.prefab | 147 ++++++++++++++++++ .../Prefabs/Audio/Match Music 1.prefab.meta | 7 + Assets/Prefabs/Audio/Match Music 2.prefab | 147 ++++++++++++++++++ .../Prefabs/Audio/Match Music 2.prefab.meta | 7 + Assets/Scripts/Managers/AudioManager.cs | 46 ++++++ 6 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 Assets/Prefabs/Audio/Match Music 1.prefab create mode 100644 Assets/Prefabs/Audio/Match Music 1.prefab.meta create mode 100644 Assets/Prefabs/Audio/Match Music 2.prefab create mode 100644 Assets/Prefabs/Audio/Match Music 2.prefab.meta diff --git a/Assets/Prefabs/Audio/Main Menu Music.prefab b/Assets/Prefabs/Audio/Main Menu Music.prefab index 316d71e..cd83f7c 100644 --- a/Assets/Prefabs/Audio/Main Menu Music.prefab +++ b/Assets/Prefabs/Audio/Main Menu Music.prefab @@ -59,7 +59,7 @@ AudioSource: m_Enabled: 1 serializedVersion: 4 OutputAudioMixerGroup: {fileID: 0} - m_audioClip: {fileID: 8300000, guid: f03afcd094e19824e8549f6fec3655ef, type: 3} + m_audioClip: {fileID: 8300000, guid: d1ec1eae9a033cd4c93bf12719fcc6da, type: 3} m_PlayOnAwake: 0 m_Volume: 0.092 m_Pitch: 1.05 diff --git a/Assets/Prefabs/Audio/Match Music 1.prefab b/Assets/Prefabs/Audio/Match Music 1.prefab new file mode 100644 index 0000000..4e86d77 --- /dev/null +++ b/Assets/Prefabs/Audio/Match Music 1.prefab @@ -0,0 +1,147 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4479628772504131522 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6423719894035945655} + - component: {fileID: 3310727599597512245} + - component: {fileID: 7696877450501702533} + m_Layer: 0 + m_Name: Match Music 1 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &6423719894035945655 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &3310727599597512245 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 887650ff1f3850a43aa8d1281fc70528, type: 3} + m_Name: + m_EditorClassIdentifier: + id: 2 + audioTag: match_music + pitchRange: 0.3 + volumeRange: 0.3 +--- !u!82 &7696877450501702533 +AudioSource: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 8300000, guid: 63665211bd1fd94498c07fd68e2e85a2, type: 3} + m_PlayOnAwake: 0 + m_Volume: 0.025 + m_Pitch: 1 + Loop: 0 + Mute: 0 + Spatialize: 0 + SpatializePostEffects: 0 + Priority: 128 + DopplerLevel: 0 + MinDistance: 1 + MaxDistance: 100 + Pan2D: 0 + rolloffMode: 1 + BypassEffects: 0 + BypassListenerEffects: 0 + BypassReverbZones: 0 + rolloffCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + - serializedVersion: 3 + time: 1 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + panLevelCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + spreadCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + reverbZoneMixCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 diff --git a/Assets/Prefabs/Audio/Match Music 1.prefab.meta b/Assets/Prefabs/Audio/Match Music 1.prefab.meta new file mode 100644 index 0000000..96e490c --- /dev/null +++ b/Assets/Prefabs/Audio/Match Music 1.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 6c47edb79416b2145b9eae4412e65992 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: audio + assetBundleVariant: diff --git a/Assets/Prefabs/Audio/Match Music 2.prefab b/Assets/Prefabs/Audio/Match Music 2.prefab new file mode 100644 index 0000000..6a85a8d --- /dev/null +++ b/Assets/Prefabs/Audio/Match Music 2.prefab @@ -0,0 +1,147 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4479628772504131522 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6423719894035945655} + - component: {fileID: 3310727599597512245} + - component: {fileID: 7696877450501702533} + m_Layer: 0 + m_Name: Match Music 2 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &6423719894035945655 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &3310727599597512245 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 887650ff1f3850a43aa8d1281fc70528, type: 3} + m_Name: + m_EditorClassIdentifier: + id: 2 + audioTag: match_music + pitchRange: 0.3 + volumeRange: 0.3 +--- !u!82 &7696877450501702533 +AudioSource: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4479628772504131522} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 8300000, guid: d1ec1eae9a033cd4c93bf12719fcc6da, type: 3} + m_PlayOnAwake: 0 + m_Volume: 0.025 + m_Pitch: 1 + Loop: 0 + Mute: 0 + Spatialize: 0 + SpatializePostEffects: 0 + Priority: 128 + DopplerLevel: 0 + MinDistance: 1 + MaxDistance: 100 + Pan2D: 0 + rolloffMode: 1 + BypassEffects: 0 + BypassListenerEffects: 0 + BypassReverbZones: 0 + rolloffCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + - serializedVersion: 3 + time: 1 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + panLevelCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + spreadCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + reverbZoneMixCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 diff --git a/Assets/Prefabs/Audio/Match Music 2.prefab.meta b/Assets/Prefabs/Audio/Match Music 2.prefab.meta new file mode 100644 index 0000000..964f84f --- /dev/null +++ b/Assets/Prefabs/Audio/Match Music 2.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: eb40049a3fc02ca43a0842043a1a517a +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: audio + assetBundleVariant: diff --git a/Assets/Scripts/Managers/AudioManager.cs b/Assets/Scripts/Managers/AudioManager.cs index 526c4d6..37d20b7 100644 --- a/Assets/Scripts/Managers/AudioManager.cs +++ b/Assets/Scripts/Managers/AudioManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using log4net; +using Unity.Burst.Intrinsics; using UnityEngine; namespace Managers @@ -70,6 +71,33 @@ namespace Managers return GetLocalSound(null, -1, transform, SearchSound(null, -1, prefabName)); } + /// + /// Instantiates a random sound, chosen from a tag group, + /// from the library which will be used for global and + /// scene independent sound. + /// + /// Tag group which will be randomly chosen from + /// Should the audio be destroyed, when the calling scene is destroyed? + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetGlobalSoundRandom(string audioTag, bool dependsOnScene = false) + { + return GetGlobalSound(null, -1, dependsOnScene, SearchSoundRandom(audioTag)); + } + + /// + /// Instantiates a random sound, chosen from a tag group, + /// from the library which will be used for local, spatial and + /// scene dependent sound. It reparents the object(AudioSource + ManageableAudio component), + /// to the provided transform. + /// + /// Tag group which will be randomly chosen from + /// Transform which will parent the sound object + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetLocalSoundRandom(string audioTag, Transform transform) + { + return GetLocalSound(null, -1, transform, SearchSoundRandom(audioTag)); + } + /// /// Instantiates a sound from the library which will be used for global sound. /// The ManageableAudio object is instantiated in the currently active scene, @@ -171,6 +199,24 @@ namespace Managers } return audioDictionary; } + private GameObject SearchSoundRandom(string audioTag) + { + try + { + var tagGroup = audioDictionary.Keys. + Where(manageableAudio => manageableAudio.audioTag == audioTag).ToList(); + System.Random rnd = new System.Random(); + rnd.Next(); + int randomIndex = rnd.Next(tagGroup.Count); + return audioDictionary[tagGroup[randomIndex]]; + } + catch (Exception e) + { + Log.Error(e.Message); + return null; + } + + } private GameObject SearchSound(string audioTag = null, int id = -1, string prefabName = null)