From 869e2f608d54cda58a7cc54195cb404df23de530 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 6 Oct 2019 19:40:54 +0200 Subject: [PATCH] Music smoothing --- Assets/Code/AfterStart.cs | 4 + Assets/{Editor => Code}/AfterStart.cs.meta | 2 +- Assets/Code/BeforeStart.cs | 4 + Assets/{Editor => Code}/BeforeStart.cs.meta | 2 +- Assets/Components/MusicManager.cs | 48 +++++-- ...rStart.cs => AfterStartAttributeDrawer.cs} | 2 - .../Editor/AfterStartAttributeDrawer.cs.meta | 11 ++ ...Start.cs => BeforeStartAttributeDrawer.cs} | 6 +- .../Editor/BeforeStartAttributeDrawer.cs.meta | 11 ++ Assets/Music/layer2.ogg.meta | 22 +++ Assets/Prefabs/GameController.prefab | 132 +++++++++++++++++- Assets/Scenes/Game.unity | 5 - Assets/Sprites/5_Aura_pianeta.png.meta | 91 ++++++++++++ Assets/Sprites/5_Planet.png.meta | 91 ++++++++++++ 14 files changed, 408 insertions(+), 23 deletions(-) create mode 100644 Assets/Code/AfterStart.cs rename Assets/{Editor => Code}/AfterStart.cs.meta (83%) create mode 100644 Assets/Code/BeforeStart.cs rename Assets/{Editor => Code}/BeforeStart.cs.meta (83%) rename Assets/Editor/{AfterStart.cs => AfterStartAttributeDrawer.cs} (94%) create mode 100644 Assets/Editor/AfterStartAttributeDrawer.cs.meta rename Assets/Editor/{BeforeStart.cs => BeforeStartAttributeDrawer.cs} (80%) create mode 100644 Assets/Editor/BeforeStartAttributeDrawer.cs.meta create mode 100644 Assets/Music/layer2.ogg.meta create mode 100644 Assets/Sprites/5_Aura_pianeta.png.meta create mode 100644 Assets/Sprites/5_Planet.png.meta diff --git a/Assets/Code/AfterStart.cs b/Assets/Code/AfterStart.cs new file mode 100644 index 0000000..b7c983a --- /dev/null +++ b/Assets/Code/AfterStart.cs @@ -0,0 +1,4 @@ +//From https://answers.unity.com/questions/442342/how-to-make-public-variable-read-only-during-run-t.html +using UnityEngine; + +public class AfterStart : PropertyAttribute {} diff --git a/Assets/Editor/AfterStart.cs.meta b/Assets/Code/AfterStart.cs.meta similarity index 83% rename from Assets/Editor/AfterStart.cs.meta rename to Assets/Code/AfterStart.cs.meta index d25ef57..9a6351d 100644 --- a/Assets/Editor/AfterStart.cs.meta +++ b/Assets/Code/AfterStart.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 9903d7c519b2c6849943c4d6b8dab4bf +guid: 9fae7738546fcf6408a8a1e986460fda MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Code/BeforeStart.cs b/Assets/Code/BeforeStart.cs new file mode 100644 index 0000000..2118cda --- /dev/null +++ b/Assets/Code/BeforeStart.cs @@ -0,0 +1,4 @@ +//From https://answers.unity.com/questions/442342/how-to-make-public-variable-read-only-during-run-t.html +using UnityEngine; + +public class BeforeStart : PropertyAttribute {} diff --git a/Assets/Editor/BeforeStart.cs.meta b/Assets/Code/BeforeStart.cs.meta similarity index 83% rename from Assets/Editor/BeforeStart.cs.meta rename to Assets/Code/BeforeStart.cs.meta index 6ff2567..1b63ee2 100644 --- a/Assets/Editor/BeforeStart.cs.meta +++ b/Assets/Code/BeforeStart.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: de6a180c8ef2dbd44ae2fd0f7a040623 +guid: c80c3920999989b4b920fe2953ade20e MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Components/MusicManager.cs b/Assets/Components/MusicManager.cs index a3cbaec..50059bc 100644 --- a/Assets/Components/MusicManager.cs +++ b/Assets/Components/MusicManager.cs @@ -7,7 +7,13 @@ using UnityEngine; public class MusicManager : MonoBehaviour { public AudioSource baseLayer; - public List layers; + public float changeSpeed = 1f; + + [BeforeStart] + public AudioSource[] layers; + + protected float baseLayerTargetVolume; + protected float[] targetVolume; protected bool neverStarted; @@ -16,15 +22,20 @@ public class MusicManager : MonoBehaviour foreach(AudioSource audioSource in layers) { audioSource.volume = 0; } + targetVolume = new float[layers.Length]; + for(int i = 0; i < layers.Length; i++) { + targetVolume[i] = 0f; + } + baseLayerTargetVolume = 1f; neverStarted = true; } public void UpdateLayers(int maxTier) { if(maxTier == -1) { - baseLayer.volume = 1f; + baseLayerTargetVolume = 1f; } else { - baseLayer.volume = 0f; + baseLayerTargetVolume = 0f; } if(neverStarted) { @@ -34,20 +45,39 @@ public class MusicManager : MonoBehaviour neverStarted = false; } - if(maxTier >= layers.Count) { - foreach(AudioSource layer in layers) { - layer.volume = 1f; + if(maxTier >= layers.Length) { + for(int i = 0; i < layers.Length; i++) { + targetVolume[i] = 1f; } } else { - for(int i = 0; i < layers.Count; i++) { + for(int i = 0; i < layers.Length; i++) { if(maxTier >= i) { - layers[i].volume = 1f; + targetVolume[i] = 1f; } else { - layers[i].volume = 0f; + targetVolume[i] = 0f; } } } } + + protected void Update() { + if(baseLayer.volume > baseLayerTargetVolume) { + baseLayer.volume = Mathf.Clamp(baseLayer.volume - Time.deltaTime * changeSpeed, baseLayerTargetVolume, float.PositiveInfinity); + } + else if(baseLayer.volume < baseLayerTargetVolume) { + baseLayer.volume = Mathf.Clamp(baseLayer.volume + Time.deltaTime * changeSpeed, 0f, baseLayerTargetVolume); + } + + for(int i = 0; i < layers.Length; i++) { + if(layers[i].volume > targetVolume[i]) { + layers[i].volume = Mathf.Clamp(layers[i].volume - Time.deltaTime * changeSpeed, targetVolume[i], float.PositiveInfinity); + } + else if(layers[i].volume < targetVolume[i]) { + layers[i].volume = Mathf.Clamp(layers[i].volume + Time.deltaTime * changeSpeed, 0f, targetVolume[i]); + } + } + } + } diff --git a/Assets/Editor/AfterStart.cs b/Assets/Editor/AfterStartAttributeDrawer.cs similarity index 94% rename from Assets/Editor/AfterStart.cs rename to Assets/Editor/AfterStartAttributeDrawer.cs index a9787e3..0be6663 100644 --- a/Assets/Editor/AfterStart.cs +++ b/Assets/Editor/AfterStartAttributeDrawer.cs @@ -19,5 +19,3 @@ public class AfterStartAttributeDrawer : PropertyDrawer GUI.enabled = true; } } - -public class AfterStart : PropertyAttribute {} diff --git a/Assets/Editor/AfterStartAttributeDrawer.cs.meta b/Assets/Editor/AfterStartAttributeDrawer.cs.meta new file mode 100644 index 0000000..347df41 --- /dev/null +++ b/Assets/Editor/AfterStartAttributeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb6935448ef8afa40b70179ed82526f3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/BeforeStart.cs b/Assets/Editor/BeforeStartAttributeDrawer.cs similarity index 80% rename from Assets/Editor/BeforeStart.cs rename to Assets/Editor/BeforeStartAttributeDrawer.cs index 63ada79..1ef0ab8 100644 --- a/Assets/Editor/BeforeStart.cs +++ b/Assets/Editor/BeforeStartAttributeDrawer.cs @@ -1,4 +1,4 @@ -//From https://answers.unity.com/questions/442342/how-to-make-public-variable-read-only-during-run-t.html +//From https://answers.unity.com/questions/442342/how-to-make-public-variable-read-only-during-run-t.html using UnityEngine; using UnityEditor; @@ -18,6 +18,4 @@ public class BeforeStartAttributeDrawer : PropertyDrawer EditorGUI.PropertyField(position, property, label, true); GUI.enabled = true; } -} - -public class BeforeStart : PropertyAttribute {} +} \ No newline at end of file diff --git a/Assets/Editor/BeforeStartAttributeDrawer.cs.meta b/Assets/Editor/BeforeStartAttributeDrawer.cs.meta new file mode 100644 index 0000000..470f5a0 --- /dev/null +++ b/Assets/Editor/BeforeStartAttributeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0c5fc3bdbfd0b2c4cb8ffcc597e1d884 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Music/layer2.ogg.meta b/Assets/Music/layer2.ogg.meta new file mode 100644 index 0000000..cf79a83 --- /dev/null +++ b/Assets/Music/layer2.ogg.meta @@ -0,0 +1,22 @@ +fileFormatVersion: 2 +guid: 7f5aa2a2cf682734ba7c9842badb7b84 +AudioImporter: + externalObjects: {} + serializedVersion: 6 + defaultSettings: + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + preloadAudioData: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/GameController.prefab b/Assets/Prefabs/GameController.prefab index be03dd8..06d7535 100644 --- a/Assets/Prefabs/GameController.prefab +++ b/Assets/Prefabs/GameController.prefab @@ -1,5 +1,132 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &1636788349 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1636788350} + - component: {fileID: 1636788351} + m_Layer: 0 + m_Name: Layer 2 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1636788350 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1636788349} + 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: [] + m_Father: {fileID: 1990625892755510357} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!82 &1636788351 +AudioSource: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1636788349} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 8300000, guid: 7f5aa2a2cf682734ba7c9842badb7b84, type: 3} + m_PlayOnAwake: 0 + m_Volume: 1 + m_Pitch: 1 + Loop: 1 + Mute: 0 + Spatialize: 0 + SpatializePostEffects: 0 + Priority: 128 + DopplerLevel: 1 + MinDistance: 1 + MaxDistance: 500 + Pan2D: 0 + rolloffMode: 0 + 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: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 --- !u!1 &2840017336573732720 GameObject: m_ObjectHideFlags: 0 @@ -42,7 +169,7 @@ AudioSource: serializedVersion: 4 OutputAudioMixerGroup: {fileID: 0} m_audioClip: {fileID: 8300000, guid: 37e30c686763c1e4ca22b20a356252c0, type: 3} - m_PlayOnAwake: 1 + m_PlayOnAwake: 0 m_Volume: 1 m_Pitch: 1 Loop: 1 @@ -392,8 +519,10 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: baseLayer: {fileID: 787973444833592173} + changeSpeed: 1 layers: - {fileID: 4850948939192426854} + - {fileID: 1636788351} --- !u!1 &7887871963136015922 GameObject: m_ObjectHideFlags: 0 @@ -550,6 +679,7 @@ Transform: m_Children: - {fileID: 2826821897386302087} - {fileID: 576137496605705657} + - {fileID: 1636788350} m_Father: {fileID: 9012813807232547518} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index e8fe65c..0e634fa 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -590,11 +590,6 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 4850948939192426854, guid: bf08d766b6e86cf4a8773489143ab2d5, - type: 3} - propertyPath: m_PlayOnAwake - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: bf08d766b6e86cf4a8773489143ab2d5, type: 3} --- !u!1001 &9221969453494073200 diff --git a/Assets/Sprites/5_Aura_pianeta.png.meta b/Assets/Sprites/5_Aura_pianeta.png.meta new file mode 100644 index 0000000..35fe17f --- /dev/null +++ b/Assets/Sprites/5_Aura_pianeta.png.meta @@ -0,0 +1,91 @@ +fileFormatVersion: 2 +guid: e4ec9161cccbbf6418c6303578c92a09 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 10 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: -1 + aniso: -1 + mipBias: -100 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 1 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: 5e97eb03825dee720800000000000000 + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Sprites/5_Planet.png.meta b/Assets/Sprites/5_Planet.png.meta new file mode 100644 index 0000000..8354ab9 --- /dev/null +++ b/Assets/Sprites/5_Planet.png.meta @@ -0,0 +1,91 @@ +fileFormatVersion: 2 +guid: cf0aaa5da1a763b4ca8944ec483ffb86 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 10 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: -1 + aniso: -1 + mipBias: -100 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 1 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: 5e97eb03825dee720800000000000000 + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: