From 9bcde1e028591d51dc9f9255c96057b1a14a9155 Mon Sep 17 00:00:00 2001 From: cyndrdev Date: Wed, 24 Mar 2021 11:50:07 +0000 Subject: [PATCH] added music triggers --- game/Assets/Prefabs/AudioManager.prefab | 3 +- game/Assets/Prefabs/Music.meta | 8 + .../Prefabs/Music/MusicTriggerZone.prefab | 60 +++++ .../Music/MusicTriggerZone.prefab.meta | 7 + game/Assets/Scenes/Main.unity | 243 ++++++++++++++++++ game/Assets/Scripts/Audio/AudioManager.cs | 35 ++- game/Assets/Scripts/Audio/MusicTriggerZone.cs | 19 ++ .../Scripts/Audio/MusicTriggerZone.cs.meta | 11 + 8 files changed, 380 insertions(+), 6 deletions(-) create mode 100644 game/Assets/Prefabs/Music.meta create mode 100644 game/Assets/Prefabs/Music/MusicTriggerZone.prefab create mode 100644 game/Assets/Prefabs/Music/MusicTriggerZone.prefab.meta create mode 100644 game/Assets/Scripts/Audio/MusicTriggerZone.cs create mode 100644 game/Assets/Scripts/Audio/MusicTriggerZone.cs.meta diff --git a/game/Assets/Prefabs/AudioManager.prefab b/game/Assets/Prefabs/AudioManager.prefab index 69a3877..0010d5b 100644 --- a/game/Assets/Prefabs/AudioManager.prefab +++ b/game/Assets/Prefabs/AudioManager.prefab @@ -44,7 +44,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 27f3f5345a3cb684195f070b3d44bc05, type: 3} m_Name: m_EditorClassIdentifier: - _musicId: event:/Music/WIPMusic + _musicId: event:/Music/MultiMusic + _trackDeltaPerSecond: 2.5 _musicBus: BusName: Music PlayerPref: {fileID: 11400000, guid: 45598e626e9c27c428f33cca2dd341e2, type: 2} diff --git a/game/Assets/Prefabs/Music.meta b/game/Assets/Prefabs/Music.meta new file mode 100644 index 0000000..3013a0f --- /dev/null +++ b/game/Assets/Prefabs/Music.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a1767ed7c727a304f90471918467166d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Prefabs/Music/MusicTriggerZone.prefab b/game/Assets/Prefabs/Music/MusicTriggerZone.prefab new file mode 100644 index 0000000..57bf976 --- /dev/null +++ b/game/Assets/Prefabs/Music/MusicTriggerZone.prefab @@ -0,0 +1,60 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &1670603406037581903 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1670603406037581902} + - component: {fileID: 1670603406037581897} + - component: {fileID: 1670603406037581898} + m_Layer: 0 + m_Name: MusicTriggerZone + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1670603406037581902 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1670603406037581903} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -26, y: 0, z: 57} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!65 &1670603406037581897 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1670603406037581903} + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 10, y: 10, z: 1} + m_Center: {x: 0, y: 5, z: 0} +--- !u!114 &1670603406037581898 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1670603406037581903} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 42ae269bbe33edf4fb811511820ba248, type: 3} + m_Name: + m_EditorClassIdentifier: + _track: 7 diff --git a/game/Assets/Prefabs/Music/MusicTriggerZone.prefab.meta b/game/Assets/Prefabs/Music/MusicTriggerZone.prefab.meta new file mode 100644 index 0000000..dbc7e2f --- /dev/null +++ b/game/Assets/Prefabs/Music/MusicTriggerZone.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 69243a66990c1e7459a7579bf6208a7d +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Scenes/Main.unity b/game/Assets/Scenes/Main.unity index 5b78c24..6d15cf6 100644 --- a/game/Assets/Scenes/Main.unity +++ b/game/Assets/Scenes/Main.unity @@ -662,6 +662,39 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 3ea432459c1408040880f4c027fab559, type: 3} m_PrefabInstance: {fileID: 3060674078935020117} m_PrefabAsset: {fileID: 0} +--- !u!1 &239903584 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 239903585} + m_Layer: 0 + m_Name: MusicTriggers + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &239903585 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 239903584} + 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_Children: + - {fileID: 796404683} + - {fileID: 1869566704} + - {fileID: 1060814304} + m_Father: {fileID: 0} + m_RootOrder: 13 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &240211937 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: e73358a549de9664fa0a48ccdea07e1c, type: 3} @@ -2358,6 +2391,11 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: e20114b5c13a067488940c5cc8e13b60, type: 3} m_PrefabInstance: {fileID: 3060674078300009146} m_PrefabAsset: {fileID: 0} +--- !u!4 &796404683 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + m_PrefabInstance: {fileID: 1670603406786800517} + m_PrefabAsset: {fileID: 0} --- !u!1 &797647646 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: 506f92c064b9c494f981ac4216e033a8, type: 3} @@ -3096,6 +3134,84 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: ef7a51de2c5c5c5438ce6fea733533b2, type: 3} m_PrefabInstance: {fileID: 3060674079011159413} m_PrefabAsset: {fileID: 0} +--- !u!1001 &1060814303 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 239903585} + m_Modifications: + - target: {fileID: 1670603406037581897, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Size.x + value: 18 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581897, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Size.y + value: 18 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581897, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Size.z + value: 18 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581898, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: _track + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_RootOrder + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.x + value: 14.5 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.z + value: -31 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581903, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Name + value: MusicTriggerZone (2) + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} +--- !u!4 &1060814304 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + m_PrefabInstance: {fileID: 1060814303} + m_PrefabAsset: {fileID: 0} --- !u!1001 &1063558645 PrefabInstance: m_ObjectHideFlags: 0 @@ -5997,6 +6113,72 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 4a31c57c3ca869448a5849436b526efa, type: 3} m_PrefabInstance: {fileID: 3060674078962375218} m_PrefabAsset: {fileID: 0} +--- !u!1001 &1869566703 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 239903585} + m_Modifications: + - target: {fileID: 1670603406037581898, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: _track + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_RootOrder + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.x + value: -7 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.z + value: 27 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.w + value: 0.97629607 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.y + value: -0.21643952 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: -25 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581903, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Name + value: MusicTriggerZone (1) + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} +--- !u!4 &1869566704 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + m_PrefabInstance: {fileID: 1869566703} + m_PrefabAsset: {fileID: 0} --- !u!1 &1871781663 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: e20114b5c13a067488940c5cc8e13b60, type: 3} @@ -6768,6 +6950,67 @@ GameObject: m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 +--- !u!1001 &1670603406786800517 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 239903585} + m_Modifications: + - target: {fileID: 1670603406037581898, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: _track + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_RootOrder + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.x + value: -26 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalPosition.z + value: 57 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581902, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1670603406037581903, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} + propertyPath: m_Name + value: MusicTriggerZone + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 69243a66990c1e7459a7579bf6208a7d, type: 3} --- !u!1001 &3060674077596672761 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/game/Assets/Scripts/Audio/AudioManager.cs b/game/Assets/Scripts/Audio/AudioManager.cs index 1823439..f585dfe 100644 --- a/game/Assets/Scripts/Audio/AudioManager.cs +++ b/game/Assets/Scripts/Audio/AudioManager.cs @@ -1,9 +1,10 @@ using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using UnityEngine; using FMOD; -using FMOD.Studio; using FMODUnity; +using EventInstance = FMOD.Studio.EventInstance; [RequireComponent(typeof(FMODUnity.StudioBankLoader))] public class AudioManager : MonoBehaviour @@ -26,6 +27,14 @@ public class AudioManager : MonoBehaviour [EventRef] private string _musicId; + [SerializeField] + private float _trackDeltaPerSecond; + + private const string TRACK_PARAM = "Track"; + + private float _currentTrack = 1f; + private int _targetTrack = 1; + [SerializeField] private AdjustableAudioBus _musicBus; @@ -37,20 +46,36 @@ public class AudioManager : MonoBehaviour [SerializeField] private AdjustableAudioBus _uiBus; - - public AudioManager Instance { get; private set; } + + private static AudioManager _instance; private EventInstance _musicInstance; + public static void SetTrack( int track, bool forceLower = false ) + { + if ( forceLower || track > _instance._targetTrack ) + { + _instance._targetTrack = track; + } + } + + void Update() + { + float dt = Time.deltaTime; + float maxDelta = dt * _trackDeltaPerSecond; + _currentTrack = Mathf.MoveTowards( _currentTrack, (float) _targetTrack, maxDelta ); + _musicInstance.setParameterByName( TRACK_PARAM, _currentTrack ); + } + void Awake() { - if ( Instance != null ) + if ( _instance != null ) { Destroy( gameObject ); return; } - Instance = this; + _instance = this; GetComponent().enabled = true; diff --git a/game/Assets/Scripts/Audio/MusicTriggerZone.cs b/game/Assets/Scripts/Audio/MusicTriggerZone.cs new file mode 100644 index 0000000..265438a --- /dev/null +++ b/game/Assets/Scripts/Audio/MusicTriggerZone.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MusicTriggerZone : MonoBehaviour +{ + [SerializeField] + [Range( 1, 7 )] + private int _track = 1; + + [SerializeField] + private bool _forceIfLower; + + private void OnTriggerEnter( Collider other ) + { + AudioManager.SetTrack( _track, _forceIfLower ); + } +} diff --git a/game/Assets/Scripts/Audio/MusicTriggerZone.cs.meta b/game/Assets/Scripts/Audio/MusicTriggerZone.cs.meta new file mode 100644 index 0000000..cc0390d --- /dev/null +++ b/game/Assets/Scripts/Audio/MusicTriggerZone.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 42ae269bbe33edf4fb811511820ba248 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: