Skip to content

Commit

Permalink
fix music playing
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiemontese committed Aug 30, 2024
1 parent 10bfaa6 commit 0dea477
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 403 deletions.
142 changes: 142 additions & 0 deletions Assets/Prefabs/MusicPlayer.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &8946490389915889960
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8946490389915889963}
- component: {fileID: 8946490389915889962}
- component: {fileID: 8946490389915889973}
m_Layer: 0
m_Name: MusicPlayer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8946490389915889963
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8946490389915889960}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.9940233, y: -2.6260545, z: 104.16295}
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!114 &8946490389915889962
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8946490389915889960}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 10464f60cc680df43959029edeba5d54, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!82 &8946490389915889973
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8946490389915889960}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 8300000, guid: 3cb0a16784fa7864f97cb714ff3a6528, 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
7 changes: 7 additions & 0 deletions Assets/Prefabs/MusicPlayer.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class GameManager : MonoBehaviour
{
public GameObject gamePausedUI;
public GameObject settingsUI;
public MusicPlayer musicPlayer;
public bool openPausedUIOnEsc = false;
public AudioSource musicSource;
public Toggle[] difficultyToggles;
public Toggle musicToggle;
public int winningScore = 10;
Expand Down Expand Up @@ -49,7 +49,7 @@ public int music
{
_music = 1 - _music;
PrefsManager.music = _music;
UpdateMusicSource();
UpdateMusicPlayer();
}
}

Expand All @@ -74,11 +74,13 @@ public bool settingsOpen

void Awake()
{
musicPlayer = GameObject.FindGameObjectWithTag("MusicPlayer").GetComponent<MusicPlayer>();

Time.timeScale = 1f;
PrefsManager.PrintPrefs();
_music = PrefsManager.music;
_difficulty = PrefsManager.difficulty;
UpdateMusicSource();
UpdateMusicPlayer();

if (_difficulty < difficultyToggles.Length)
{
Expand All @@ -98,14 +100,14 @@ void Update()
}
}

void UpdateMusicSource()
void UpdateMusicPlayer()
{
if (musicSource)
if (musicPlayer != null)
{
if (_music == 1)
musicSource.Play();
musicPlayer.PlayMusic();
else
musicSource.Pause();
musicPlayer.PauseMusic();
}
}

Expand Down
22 changes: 22 additions & 0 deletions Assets/Scripts/MusicPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEngine;

public class MusicPlayer : MonoBehaviour
{
private AudioSource _audioSource;
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
_audioSource = GetComponent<AudioSource>();
}

public void PlayMusic()
{
if (_audioSource.isPlaying) return;
_audioSource.Play();
}

public void PauseMusic()
{
_audioSource.Pause();
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/MusicPlayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 1 addition & 98 deletions Assets/_Scenes/AIWin.unity
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ GameObject:
- component: {fileID: 14724808}
- component: {fileID: 14724807}
- component: {fileID: 14724806}
- component: {fileID: 14724809}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
Expand Down Expand Up @@ -204,102 +203,6 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!82 &14724809
AudioSource:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 14724805}
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 8300000, guid: 3cb0a16784fa7864f97cb714ff3a6528, type: 3}
m_PlayOnAwake: 0
m_Volume: 0.5
m_Pitch: 1
Loop: 1
Mute: 1
Spatialize: 0
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 1
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 &92338370
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1133,7 +1036,7 @@ PrefabInstance:
type: 3}
propertyPath: musicSource
value:
objectReference: {fileID: 14724809}
objectReference: {fileID: 0}
- target: {fileID: 9175822297736086647, guid: 8c333de40ae158fdeb4abff8fdfac6cb,
type: 3}
propertyPath: m_LocalPosition.x
Expand Down
Loading

0 comments on commit 0dea477

Please sign in to comment.