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.
This commit is contained in:
Jakob Feldmann 2024-05-05 17:50:04 +02:00
parent 5278d5ba81
commit 9a9a9c8b21
6 changed files with 355 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 6c47edb79416b2145b9eae4412e65992
PrefabImporter:
externalObjects: {}
userData:
assetBundleName: audio
assetBundleVariant:

View File

@ -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

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: eb40049a3fc02ca43a0842043a1a517a
PrefabImporter:
externalObjects: {}
userData:
assetBundleName: audio
assetBundleVariant:

View File

@ -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));
}
/// <summary>
/// Instantiates a random sound, chosen from a tag group,
/// from the library which will be used for global and
/// scene independent sound.
/// </summary>
/// <param name="audioTag">Tag group which will be randomly chosen from</param>
/// <param name="dependsOnScene">Should the audio be destroyed, when the calling scene is destroyed?</param>
/// <returns>ManageableAudio instance to control the playback with.</returns>
public ManageableAudio GetGlobalSoundRandom(string audioTag, bool dependsOnScene = false)
{
return GetGlobalSound(null, -1, dependsOnScene, SearchSoundRandom(audioTag));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="audioTag">Tag group which will be randomly chosen from</param>
/// <param name="transform">Transform which will parent the sound object</param>
/// <returns>ManageableAudio instance to control the playback with.</returns>
public ManageableAudio GetLocalSoundRandom(string audioTag, Transform transform)
{
return GetLocalSound(null, -1, transform, SearchSoundRandom(audioTag));
}
/// <summary>
/// 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)