From 5a658d6cd4ac4e03b665d5330f14e75b33a2c01b Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Wed, 10 Apr 2024 16:57:25 +0200 Subject: [PATCH] feat: audio management/playback features Every sound in the game is now a ManageableAudio prefab. The prefabs are picked up in an audio library automatically and other game objects can get the sound they need from the AudioManager. They obtain a ManageableAudio instance which offers various methods to interact with the AudioSource. Depending on how they request the ManageableAudio, the AudioSource is attached to the requesting GameObject, to the Scene or the global management Scene. This provides options to play sounds spatially, globally and scene independent. The prefabs are identified by a tag and an ID, so it is easy to swap out sounds globally, by just replacing the prefab. --- Assets/Models/Materials.meta | 8 + Assets/Models/Materials/defaultMat.mat | 132 ++++++++++++ Assets/Models/Materials/defaultMat.mat.meta | 8 + Assets/Prefabs/Audio/Main Menu Music.prefab | 4 +- Assets/Prefabs/Audio/Thruster Sound 01.prefab | 9 +- ...ectsManager.prefab => AudioManager.prefab} | 17 +- ...r.prefab.meta => AudioManager.prefab.meta} | 0 Assets/Scenes/GameManagement.unity | 2 +- Assets/ScriptedAssets/AudioLibrary.asset | 15 ++ Assets/ScriptedAssets/AudioLibrary.asset.meta | 8 + Assets/Scripts/AudioLibrary.cs | 39 +++- Assets/Scripts/ManagableAudio.cs | 46 ----- Assets/Scripts/ManageableAudio.cs | 67 ++++++ ...eAudio.cs.meta => ManageableAudio.cs.meta} | 0 Assets/Scripts/Managers/AVEffectsManager.cs | 48 ----- Assets/Scripts/Managers/AudioManager.cs | 192 ++++++++++++++++++ ...tsManager.cs.meta => AudioManager.cs.meta} | 0 Assets/Scripts/Managers/GameManager.cs | 6 +- Assets/Scripts/Ship.cs | 14 ++ .../Scripts/Managers/SlimUIMainMenu.cs | 4 + Packages/manifest.json | 12 +- Packages/packages-lock.json | 48 +++-- ProjectSettings/ProjectVersion.txt | 4 +- ProjectSettings/ShaderGraphSettings.asset | 1 + 24 files changed, 549 insertions(+), 135 deletions(-) create mode 100644 Assets/Models/Materials.meta create mode 100644 Assets/Models/Materials/defaultMat.mat create mode 100644 Assets/Models/Materials/defaultMat.mat.meta rename Assets/Prefabs/Managers/{AVEffectsManager.prefab => AudioManager.prefab} (73%) rename Assets/Prefabs/Managers/{AVEffectsManager.prefab.meta => AudioManager.prefab.meta} (100%) create mode 100644 Assets/ScriptedAssets/AudioLibrary.asset create mode 100644 Assets/ScriptedAssets/AudioLibrary.asset.meta delete mode 100644 Assets/Scripts/ManagableAudio.cs create mode 100644 Assets/Scripts/ManageableAudio.cs rename Assets/Scripts/{ManagableAudio.cs.meta => ManageableAudio.cs.meta} (100%) delete mode 100644 Assets/Scripts/Managers/AVEffectsManager.cs create mode 100644 Assets/Scripts/Managers/AudioManager.cs rename Assets/Scripts/Managers/{AVEffectsManager.cs.meta => AudioManager.cs.meta} (100%) diff --git a/Assets/Models/Materials.meta b/Assets/Models/Materials.meta new file mode 100644 index 0000000..e111bae --- /dev/null +++ b/Assets/Models/Materials.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d058681f956108945a5e0a77ccaca7ae +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Models/Materials/defaultMat.mat b/Assets/Models/Materials/defaultMat.mat new file mode 100644 index 0000000..fa97a15 --- /dev/null +++ b/Assets/Models/Materials/defaultMat.mat @@ -0,0 +1,132 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &-7783281861020253796 +MonoBehaviour: + m_ObjectHideFlags: 11 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} + m_Name: + m_EditorClassIdentifier: + version: 7 +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: defaultMat + m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: [] + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BaseMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _SpecGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_Lightmaps: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_LightmapsInd: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_ShadowMasks: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - _AlphaClip: 0 + - _AlphaToMask: 0 + - _Blend: 0 + - _BlendModePreserveSpecular: 1 + - _BumpScale: 1 + - _ClearCoatMask: 0 + - _ClearCoatSmoothness: 0 + - _Cull: 2 + - _Cutoff: 0.5 + - _DetailAlbedoMapScale: 1 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _DstBlendAlpha: 0 + - _EnvironmentReflections: 1 + - _GlossMapScale: 0 + - _Glossiness: 0 + - _GlossyReflections: 0 + - _Metallic: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.005 + - _QueueOffset: 0 + - _ReceiveShadows: 1 + - _Smoothness: 0.5 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _SrcBlendAlpha: 1 + - _Surface: 0 + - _WorkflowMode: 1 + - _ZWrite: 1 + m_Colors: + - _BaseColor: {r: 0.8, g: 0.8, b: 0.8, a: 1} + - _Color: {r: 0.8, g: 0.8, b: 0.8, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Models/Materials/defaultMat.mat.meta b/Assets/Models/Materials/defaultMat.mat.meta new file mode 100644 index 0000000..dfe58af --- /dev/null +++ b/Assets/Models/Materials/defaultMat.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4d8d9f678971ac94f80eb9849d414cc5 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Audio/Main Menu Music.prefab b/Assets/Prefabs/Audio/Main Menu Music.prefab index de2bd3f..e1ac6bd 100644 --- a/Assets/Prefabs/Audio/Main Menu Music.prefab +++ b/Assets/Prefabs/Audio/Main Menu Music.prefab @@ -62,7 +62,7 @@ AudioSource: OutputAudioMixerGroup: {fileID: 0} m_audioClip: {fileID: 8300000, guid: f03afcd094e19824e8549f6fec3655ef, type: 3} m_PlayOnAwake: 0 - m_Volume: 0.2 + m_Volume: 0.092 m_Pitch: 0.95 Loop: 1 Mute: 0 @@ -71,7 +71,7 @@ AudioSource: Priority: 128 DopplerLevel: 0 MinDistance: 1 - MaxDistance: 500 + MaxDistance: 100 Pan2D: 0 rolloffMode: 1 BypassEffects: 0 diff --git a/Assets/Prefabs/Audio/Thruster Sound 01.prefab b/Assets/Prefabs/Audio/Thruster Sound 01.prefab index e2c9a19..bce5d57 100644 --- a/Assets/Prefabs/Audio/Thruster Sound 01.prefab +++ b/Assets/Prefabs/Audio/Thruster Sound 01.prefab @@ -45,11 +45,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 887650ff1f3850a43aa8d1281fc70528, type: 3} m_Name: m_EditorClassIdentifier: - maxParallel: 4 id: 1 - audioTag: music - audioSource: {fileID: 7696877450501702533} - audioSources: [] + audioTag: sound --- !u!82 &7696877450501702533 AudioSource: m_ObjectHideFlags: 0 @@ -62,7 +59,7 @@ AudioSource: OutputAudioMixerGroup: {fileID: 0} m_audioClip: {fileID: 8300000, guid: 82a8d17c2698c834f96370756847edec, type: 3} m_PlayOnAwake: 0 - m_Volume: 0.2 + m_Volume: 0.112 m_Pitch: 0.95 Loop: 1 Mute: 0 @@ -106,7 +103,7 @@ AudioSource: m_Curve: - serializedVersion: 3 time: 0 - value: 0 + value: 1 inSlope: 0 outSlope: 0 tangentMode: 0 diff --git a/Assets/Prefabs/Managers/AVEffectsManager.prefab b/Assets/Prefabs/Managers/AudioManager.prefab similarity index 73% rename from Assets/Prefabs/Managers/AVEffectsManager.prefab rename to Assets/Prefabs/Managers/AudioManager.prefab index 8cb8263..dae1f33 100644 --- a/Assets/Prefabs/Managers/AVEffectsManager.prefab +++ b/Assets/Prefabs/Managers/AudioManager.prefab @@ -10,8 +10,9 @@ GameObject: m_Component: - component: {fileID: 4498597859498223686} - component: {fileID: 4421901766771881742} + - component: {fileID: -2235839436850680582} m_Layer: 0 - m_Name: AVEffectsManager + m_Name: AudioManager m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -45,3 +46,17 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: audioLibrary: {fileID: 3600236761918540295, guid: 205eaed4caed63646b0c053361d7ed04, type: 3} +--- !u!114 &-2235839436850680582 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1668925289118893306} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d0bceda0769741041bf8f466bee87e01, type: 3} + m_Name: + m_EditorClassIdentifier: + audios: [] + manageableAudioFolder: Assets/Prefabs/Audio diff --git a/Assets/Prefabs/Managers/AVEffectsManager.prefab.meta b/Assets/Prefabs/Managers/AudioManager.prefab.meta similarity index 100% rename from Assets/Prefabs/Managers/AVEffectsManager.prefab.meta rename to Assets/Prefabs/Managers/AudioManager.prefab.meta diff --git a/Assets/Scenes/GameManagement.unity b/Assets/Scenes/GameManagement.unity index 1914e1e..18a1585 100644 --- a/Assets/Scenes/GameManagement.unity +++ b/Assets/Scenes/GameManagement.unity @@ -503,7 +503,7 @@ MonoBehaviour: MatchManager: {fileID: 5376598442414673498, guid: ab53d1c11aaff9a4680d8265bada73b6, type: 3} StatisticsManager: {fileID: -4248400137136794068, guid: 602e864eae420734599caa76d056f5c5, type: 3} PlayerManager: {fileID: 4324267968298468629, guid: bf1ba420952f8cd4e8e6e02554c13bac, type: 3} - AVEffectsManager: {fileID: 4421901766771881742, guid: d2fa91ae61bbe6644b3847b83f0f21fb, type: 3} + AudioManager: {fileID: 4421901766771881742, guid: d2fa91ae61bbe6644b3847b83f0f21fb, type: 3} startCamera: {fileID: 1164443743} --- !u!1660057539 &9223372036854775807 SceneRoots: diff --git a/Assets/ScriptedAssets/AudioLibrary.asset b/Assets/ScriptedAssets/AudioLibrary.asset new file mode 100644 index 0000000..4fcc418 --- /dev/null +++ b/Assets/ScriptedAssets/AudioLibrary.asset @@ -0,0 +1,15 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d0bceda0769741041bf8f466bee87e01, type: 3} + m_Name: AudioLibrary + m_EditorClassIdentifier: + audios: [] diff --git a/Assets/ScriptedAssets/AudioLibrary.asset.meta b/Assets/ScriptedAssets/AudioLibrary.asset.meta new file mode 100644 index 0000000..7a28e94 --- /dev/null +++ b/Assets/ScriptedAssets/AudioLibrary.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 09be763b286b985409e3cbcdba2d8bc7 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/AudioLibrary.cs b/Assets/Scripts/AudioLibrary.cs index f151a5e..dfcf8c8 100644 --- a/Assets/Scripts/AudioLibrary.cs +++ b/Assets/Scripts/AudioLibrary.cs @@ -1,7 +1,44 @@ using System.Collections.Generic; +using System.IO; +using System.Reflection; +using log4net; +using UnityEditor; using UnityEngine; public class AudioLibrary : MonoBehaviour { - public List audios; + private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + public List audios; + public string manageableAudioFolder = "Assets/Prefabs/Audio"; + + public void Awake() + { + LoadAvailableSounds(); + } + + /// + /// Loads all the ManageableAudio prefabs from the specified folder. + /// + private void LoadAvailableSounds() + { + string[] files = Directory.GetFiles(manageableAudioFolder, "*.prefab", + SearchOption.TopDirectoryOnly); + List gos = new List(); + foreach (var file in files) + { + gos.Add(AssetDatabase.LoadAssetAtPath(file)); + } + foreach (GameObject go in gos) + { + if (go.TryGetComponent(out _)) + { + audios.Add(go); + } + else + { + Log.Warn("Audio library can only load prefabs with ManageableAudio components!"); + } + } + } + } \ No newline at end of file diff --git a/Assets/Scripts/ManagableAudio.cs b/Assets/Scripts/ManagableAudio.cs deleted file mode 100644 index 72080ae..0000000 --- a/Assets/Scripts/ManagableAudio.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Reflection; -using log4net; -using UnityEngine; - -public class ManagableAudio : MonoBehaviour -{ - private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - - [SerializeField] - private int maxParallel = 1; - public int id = 0; - public string audioTag = "audio"; - public AudioSource audioSource; - - [HideInInspector] - public List audioSources; - - void Awake() - { - if (gameObject.TryGetComponent(out AudioSource audioS)) - { - audioSource = audioS; - } - else - { - Log.Warn("Managable Audio Instance hasn't got an audio source game object" - + $". Tag:{audioTag} ID:{id}"); - return; - } - audioSources.Add(audioSource); - BuildAudioSourceList(); - } - - private void BuildAudioSourceList() - { - while (audioSources.Count < maxParallel) - { - var newAS = Instantiate(audioSource); - audioSources.Add(newAS); - } - } - -} diff --git a/Assets/Scripts/ManageableAudio.cs b/Assets/Scripts/ManageableAudio.cs new file mode 100644 index 0000000..d7be806 --- /dev/null +++ b/Assets/Scripts/ManageableAudio.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Reflection; +using log4net; +using PrimeTween; +using UnityEngine; + +public class ManageableAudio : MonoBehaviour +{ + private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + public int id = 0; + public string audioTag = "audio"; + public AudioSource AudioSource { get; set; } + + void Awake() + { + if (gameObject.TryGetComponent(out AudioSource audioS)) + { + AudioSource = audioS; + } + else + { + Log.Warn("Managable Audio Instance hasn't got an audio source game object" + + $". Tag:{audioTag} ID:{id}"); + return; + } + } + + public void PlayAudio() + { + if (AudioSource == null) + { + return; + } + if (!AudioSource.enabled) + { + AudioSource.enabled = true; + } + AudioSource.Play(); + } + + public void StopAudio() + { + if (AudioSource == null) + { + return; + } + if (AudioSource.isPlaying) + { + AudioSource.Stop(); + } + } + + public void FadeOutAudio(float duration = 1) + { + float initVolume = AudioSource.volume; + Tween.Custom(initVolume, 0, duration, onValueChange: value => AudioSource.volume = value) + .OnComplete(() => + { + StopAudio(); + AudioSource.volume = initVolume; + }); + } + +} diff --git a/Assets/Scripts/ManagableAudio.cs.meta b/Assets/Scripts/ManageableAudio.cs.meta similarity index 100% rename from Assets/Scripts/ManagableAudio.cs.meta rename to Assets/Scripts/ManageableAudio.cs.meta diff --git a/Assets/Scripts/Managers/AVEffectsManager.cs b/Assets/Scripts/Managers/AVEffectsManager.cs deleted file mode 100644 index f671aa7..0000000 --- a/Assets/Scripts/Managers/AVEffectsManager.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Linq; -using System.Reflection; -using log4net; -using UnityEditor; -using UnityEngine; - -namespace Managers -{ - public class AVEffectsManager : MonoBehaviour - { - private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - - public static AVEffectsManager G { get; private set; } - - [HideInInspector] - public AudioLibrary audioLibrary; - - private AudioSource centralAudioSource; - - // Start is called before the first frame update - void Awake() - { - G = this; - Log.Info("Awake"); - if (gameObject.TryGetComponent(out AudioLibrary al)) - { - audioLibrary = al; - } - } - - void Start() - { - if(audioLibrary == null){ - return; - } - centralAudioSource = audioLibrary.audios.First(a => a.tag == "music")?.audioSource; - if(centralAudioSource != null) - centralAudioSource.Play(); - } - - } - -} - -public enum Music -{ - -} \ No newline at end of file diff --git a/Assets/Scripts/Managers/AudioManager.cs b/Assets/Scripts/Managers/AudioManager.cs new file mode 100644 index 0000000..d5801a1 --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager.cs @@ -0,0 +1,192 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using log4net; +using UnityEditor; +using UnityEngine; + +namespace Managers +{ + public class AudioManager : MonoBehaviour + { + private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + public static AudioManager G { get; private set; } + + [HideInInspector] + public AudioLibrary audioLibrary; + + private Dictionary audioDictionary = + new Dictionary(); + + // Start is called before the first frame update + void Awake() + { + G = this; + Log.Info("Awake"); + if (gameObject.TryGetComponent(out AudioLibrary al)) + { + audioLibrary = al; + } + else + { + Log.Warn("There is no audio library component in the AudioManager. There will be no sound."); + } + } + void Start() + { + if (audioLibrary == null) + { + return; + } + audioDictionary = ReadLibrary(audioLibrary); + } + + /// + /// Instantiates a sound from the library which will be used for global and + /// scene independent sound. + /// + /// Name of the sound prefab + /// Should the audio be destroyed, when the calling scene is destroyed? + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetGlobalSoundByName(string prefabName, bool dependsOnScene = false) + { + return GetGlobalSound(null, -1, false, SearchSound(null, -1, prefabName)); + } + + /// + /// Instantiates a sound 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. + /// + /// Name of the sound prefab + /// Transform which will parent the sound object. + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetLocalSoundByName(string prefabName, Transform transform) + { + return GetLocalSound(null, -1, transform, SearchSound(null, -1, prefabName)); + } + + /// + /// Instantiates a sound from the library which will be used for global sound. + /// TODO: Only makes sense, if requested prefab has 0 spatial value. + /// + /// Tag of the sound + /// ID of the sound + /// Should the audio be destroyed, when the calling scene is destroyed? + /// Optionally provide the sound GameObject directly. + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetGlobalSound(string audioTag, int id, bool dependsOnScene = false, + GameObject go = null) + { + GameObject sound; + if (go == null) + { + sound = SearchSound(audioTag, id); + } + else + { + sound = go; + } + + if (sound == null) + { + Log.Warn($"Requested sound: {audioTag}_{id} was not in the audio library."); + return null; + } + sound = Instantiate(sound); + if (!dependsOnScene) + sound.transform.SetParent(gameObject.transform); + if (sound.TryGetComponent(out ManageableAudio ma)) + { + return ma; + } + else + { + Log.Warn($"Prefab for sound: {audioTag}_{id} contained no ManageableAudio component"); + return null; + } + } + + /// + /// Instantiates a sound 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 of the sound + /// ID of the sound + /// Transform which will parent the sound object. + /// Optionally provide the sound GameObject directly. + /// ManageableAudio instance to control the playback with. + public ManageableAudio GetLocalSound(string audioTag, int id, Transform newParent, + GameObject go = null) + { + GameObject sound; + if (go == null) + { + sound = SearchSound(audioTag, id); + } + else + { + sound = go; + } + + if (sound == null) + { + Log.Warn($"Requested sound: {audioTag}_{id} was not in the audio library."); + return null; + } + sound = Instantiate(sound); + sound.transform.SetParent(newParent, false); + + if (sound.TryGetComponent(out ManageableAudio ma)) + { + return ma; + } + else + { + Log.Warn($"Prefab for sound: {audioTag}_{id} contained no ManageableAudio component"); + return null; + } + } + + // There are sounds which are scene independent and always global -> will continue playing even if the scene invoking is destroyed + // -"- scene dependent and global -> will play globally but when the scene is destroyed they will be also destroyed + // -"- scene dependent and local -> will play spatially from a object and be destroyed when the scene is destroyed + + // + + Dictionary ReadLibrary(AudioLibrary al) + { + Dictionary audioDictionary = + new Dictionary(); + foreach (GameObject go in al.audios) + { + if (go.TryGetComponent(out ManageableAudio ma)) + { + audioDictionary.Add(ma, go); + } + } + return audioDictionary; + } + + private GameObject SearchSound(string audioTag = null, int id = -1, + string prefabName = null) + { + if (prefabName == null) + { + return audioDictionary. + Where(manageableAudio => manageableAudio.Key.audioTag == audioTag). + First(manageableAudio => manageableAudio.Key.id == id).Value; + } + else + { + return audioDictionary. + First(manageableAudio => manageableAudio.Value.name == prefabName).Value; + } + + } + + } + +} \ No newline at end of file diff --git a/Assets/Scripts/Managers/AVEffectsManager.cs.meta b/Assets/Scripts/Managers/AudioManager.cs.meta similarity index 100% rename from Assets/Scripts/Managers/AVEffectsManager.cs.meta rename to Assets/Scripts/Managers/AudioManager.cs.meta diff --git a/Assets/Scripts/Managers/GameManager.cs b/Assets/Scripts/Managers/GameManager.cs index 7d1c0d6..62ded26 100644 --- a/Assets/Scripts/Managers/GameManager.cs +++ b/Assets/Scripts/Managers/GameManager.cs @@ -50,7 +50,7 @@ namespace Managers [SerializeField] private PlayerManager PlayerManager; [SerializeField] - private AVEffectsManager AVEffectsManager; + private AudioManager AudioManager; [SerializeField] private GameObject startCamera; @@ -135,9 +135,9 @@ namespace Managers { Log.Error("PlayerManager Manager Prefab missing."); } - if (AVEffectsManager != null) + if (AudioManager != null) { - AVEffectsManager = Instantiate(AVEffectsManager); + AudioManager = Instantiate(AudioManager); Log.Info("PlayerManager Manager instantiated."); } else diff --git a/Assets/Scripts/Ship.cs b/Assets/Scripts/Ship.cs index 113583c..3ee55a1 100644 --- a/Assets/Scripts/Ship.cs +++ b/Assets/Scripts/Ship.cs @@ -25,6 +25,8 @@ public class Ship : MonoBehaviour // Current Zone the player occupies private Zone zone = Zone.NimbleZone; + private ManageableAudio ThrusterSound; + void Awake() { if (forceManager == null) @@ -33,6 +35,7 @@ public class Ship : MonoBehaviour GetComponent(); } body = GetComponent(); + ThrusterSound = AudioManager.G.GetLocalSound("sound", 1, gameObject.transform); } // Start is called before the first frame update @@ -92,6 +95,17 @@ public class Ship : MonoBehaviour /// void UpdateMovement() { + if (state.thrustInput > 0) + { + if (!ThrusterSound.AudioSource.isPlaying) + { + ThrusterSound.PlayAudio(); + } + } + else + { + ThrusterSound.StopAudio(); + } //Debug.Log("inupdatemove " + currentThrustInput); // Player rotation is always possible and same speed diff --git a/Assets/SlimUI/Modern Menu 1/Scripts/Managers/SlimUIMainMenu.cs b/Assets/SlimUI/Modern Menu 1/Scripts/Managers/SlimUIMainMenu.cs index 43894e9..b7c6f5a 100644 --- a/Assets/SlimUI/Modern Menu 1/Scripts/Managers/SlimUIMainMenu.cs +++ b/Assets/SlimUI/Modern Menu 1/Scripts/Managers/SlimUIMainMenu.cs @@ -18,6 +18,7 @@ namespace SlimUI.ModernMenu private Animator CameraObject; private event EventHandler CustomSceneLoaded; private GameManager GM; + private ManageableAudio MainMenuMusic; // campaign button sub menu [Header("MENUS")] @@ -99,6 +100,8 @@ namespace SlimUI.ModernMenu firstMenu.SetActive(true); // Position1(); SetThemeColors(); + MainMenuMusic = AudioManager.G.GetGlobalSound("music", 1, true); + MainMenuMusic.PlayAudio(); } void SetThemeColors() @@ -215,6 +218,7 @@ namespace SlimUI.ModernMenu /// IEnumerator for async statemachine private IEnumerator SwitchToIngameUI() { + MainMenuMusic.FadeOutAudio(2); AsyncOperation o = SceneManager.LoadSceneAsync((int)Scenes.GameplayUserInterface, LoadSceneMode.Additive); diff --git a/Packages/manifest.json b/Packages/manifest.json index b1c4b4f..dbd8673 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,16 +3,16 @@ "com.kyrylokuzyk.primetween": "file:../Assets/Plugins/PrimeTween/internal/com.kyrylokuzyk.primetween.tgz", "com.unity.adaptiveperformance": "5.0.2", "com.unity.cloud.gltfast": "6.0.1", - "com.unity.collab-proxy": "2.2.0", + "com.unity.collab-proxy": "2.3.1", "com.unity.feature.development": "1.0.1", "com.unity.ide.visualstudio": "2.0.22", - "com.unity.inputsystem": "1.6.3", - "com.unity.postprocessing": "3.2.2", - "com.unity.render-pipelines.universal": "14.0.8", - "com.unity.textmeshpro": "3.0.7", + "com.unity.inputsystem": "1.7.0", + "com.unity.postprocessing": "3.4.0", + "com.unity.render-pipelines.universal": "14.0.10", + "com.unity.textmeshpro": "3.0.8", "com.unity.timeline": "1.7.6", "com.unity.ugui": "1.0.0", - "com.unity.visualscripting": "1.8.0", + "com.unity.visualscripting": "1.9.2", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 9cb5645..b6b104a 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -16,11 +16,12 @@ "url": "https://packages.unity.com" }, "com.unity.burst": { - "version": "1.8.7", + "version": "1.8.13", "depth": 1, "source": "registry", "dependencies": { - "com.unity.mathematics": "1.2.1" + "com.unity.mathematics": "1.2.1", + "com.unity.modules.jsonserialize": "1.0.0" }, "url": "https://packages.unity.com" }, @@ -37,7 +38,7 @@ "url": "https://packages.unity.com" }, "com.unity.collab-proxy": { - "version": "2.2.0", + "version": "2.3.1", "depth": 0, "source": "registry", "dependencies": {}, @@ -62,17 +63,17 @@ "depth": 0, "source": "builtin", "dependencies": { - "com.unity.ide.visualstudio": "2.0.18", - "com.unity.ide.rider": "3.0.24", + "com.unity.ide.visualstudio": "2.0.22", + "com.unity.ide.rider": "3.0.28", "com.unity.ide.vscode": "1.2.5", "com.unity.editorcoroutines": "1.0.0", "com.unity.performance.profile-analyzer": "1.2.2", "com.unity.test-framework": "1.1.33", - "com.unity.testtools.codecoverage": "1.2.4" + "com.unity.testtools.codecoverage": "1.2.5" } }, "com.unity.ide.rider": { - "version": "3.0.24", + "version": "3.0.28", "depth": 1, "source": "registry", "dependencies": { @@ -97,7 +98,7 @@ "url": "https://packages.unity.com" }, "com.unity.inputsystem": { - "version": "1.6.3", + "version": "1.7.0", "depth": 0, "source": "registry", "dependencies": { @@ -120,7 +121,7 @@ "url": "https://packages.unity.com" }, "com.unity.postprocessing": { - "version": "3.2.2", + "version": "3.4.0", "depth": 0, "source": "registry", "dependencies": { @@ -136,7 +137,7 @@ "url": "https://packages.unity.com" }, "com.unity.render-pipelines.core": { - "version": "14.0.8", + "version": "14.0.10", "depth": 1, "source": "builtin", "dependencies": { @@ -147,14 +148,23 @@ } }, "com.unity.render-pipelines.universal": { - "version": "14.0.8", + "version": "14.0.10", "depth": 0, "source": "builtin", "dependencies": { "com.unity.mathematics": "1.2.1", - "com.unity.burst": "1.8.4", - "com.unity.render-pipelines.core": "14.0.8", - "com.unity.shadergraph": "14.0.8" + "com.unity.burst": "1.8.9", + "com.unity.render-pipelines.core": "14.0.10", + "com.unity.shadergraph": "14.0.10", + "com.unity.render-pipelines.universal-config": "14.0.9" + } + }, + "com.unity.render-pipelines.universal-config": { + "version": "14.0.9", + "depth": 1, + "source": "builtin", + "dependencies": { + "com.unity.render-pipelines.core": "14.0.9" } }, "com.unity.searcher": { @@ -172,11 +182,11 @@ "url": "https://packages.unity.com" }, "com.unity.shadergraph": { - "version": "14.0.8", + "version": "14.0.10", "depth": 1, "source": "builtin", "dependencies": { - "com.unity.render-pipelines.core": "14.0.8", + "com.unity.render-pipelines.core": "14.0.10", "com.unity.searcher": "4.9.2" } }, @@ -192,7 +202,7 @@ "url": "https://packages.unity.com" }, "com.unity.testtools.codecoverage": { - "version": "1.2.4", + "version": "1.2.5", "depth": 1, "source": "registry", "dependencies": { @@ -202,7 +212,7 @@ "url": "https://packages.unity.com" }, "com.unity.textmeshpro": { - "version": "3.0.7", + "version": "3.0.8", "depth": 0, "source": "registry", "dependencies": { @@ -232,7 +242,7 @@ } }, "com.unity.visualscripting": { - "version": "1.8.0", + "version": "1.9.2", "depth": 0, "source": "registry", "dependencies": { diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 0a60761..759df3a 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2022.3.5f1 -m_EditorVersionWithRevision: 2022.3.5f1 (9674261d40ee) +m_EditorVersion: 2022.3.23f1 +m_EditorVersionWithRevision: 2022.3.23f1 (dbb3f7c5b5c6) diff --git a/ProjectSettings/ShaderGraphSettings.asset b/ProjectSettings/ShaderGraphSettings.asset index 9b28428..3250b06 100644 --- a/ProjectSettings/ShaderGraphSettings.asset +++ b/ProjectSettings/ShaderGraphSettings.asset @@ -12,5 +12,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: de02f9e1d18f588468e474319d09a723, type: 3} m_Name: m_EditorClassIdentifier: + shaderVariantLimit: 128 customInterpolatorErrorThreshold: 32 customInterpolatorWarningThreshold: 16