Randomized music

This commit is contained in:
2025-09-04 15:59:58 -07:00
parent 3332c12711
commit 29af8817a7
4 changed files with 85 additions and 5 deletions

View File

@@ -4979,6 +4979,7 @@ GameObject:
m_Component:
- component: {fileID: 1625416040}
- component: {fileID: 1625416039}
- component: {fileID: 1625416041}
m_Layer: 0
m_Name: SongLoop
m_TagString: Untagged
@@ -4997,11 +4998,11 @@ AudioSource:
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0}
m_Resource: {fileID: 8300000, guid: 99cb5320fee003746a29683a53dd3337, type: 3}
m_PlayOnAwake: 1
m_Resource: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
Loop: 1
Loop: 0
Mute: 0
Spatialize: 0
SpatializePostEffects: 0
@@ -5098,6 +5099,23 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1625416041
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1625416038}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 81a98a63df08841c4b3fdc71ceecda4c, type: 3}
m_Name:
m_EditorClassIdentifier: '::'
audioClips:
- {fileID: 8300000, guid: ea648a173226047439e18c96ebe3c1bb, type: 3}
- {fileID: 8300000, guid: bdf9c412cfb2642209d10186296c0782, type: 3}
- {fileID: 8300000, guid: f9fc94c25bdee4c0d9f5774720bd4cfc, type: 3}
- {fileID: 8300000, guid: 0e89a6f887c07452aae4a602586ec917, type: 3}
--- !u!1 &1639562819
GameObject:
m_ObjectHideFlags: 0

View File

@@ -0,0 +1,60 @@
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class GameMusicHandler : MonoBehaviour
{
public AudioClip[] audioClips;
private AudioSource audioSource;
private int lastIndex = -1;
private bool isPaused = false;
void Awake()
{
audioSource = GetComponent<AudioSource>();
PlayRandomClip();
}
void Update()
{
if (!audioSource.isPlaying && !isPaused) PlayRandomClip();
}
void PlayRandomClip()
{
if (audioClips.Length == 0) return;
int index;
do
{
index = Random.Range(0, audioClips.Length);
} while (audioClips.Length > 1 && index == lastIndex);
lastIndex = index;
audioSource.clip = audioClips[index];
audioSource.Play();
}
public void PauseMusic()
{
if (audioSource.isPlaying)
{
audioSource.Pause();
isPaused = true;
}
}
public void ResumeMusic()
{
if (isPaused)
{
audioSource.Play();
isPaused = false;
}
}
public void RestartMusic()
{
audioSource.Stop();
audioSource.Play();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 81a98a63df08841c4b3fdc71ceecda4c

View File

@@ -639,7 +639,7 @@ public class GamePlayer : MonoBehaviour
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
backgroundMusic.Pause();
backgroundMusic.GetComponent<GameMusicHandler>().PauseMusic();
pausePanel.SetActive(true);
}
@@ -648,7 +648,7 @@ public class GamePlayer : MonoBehaviour
lastMoveTime = Time.time;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
backgroundMusic.Play();
backgroundMusic.GetComponent<GameMusicHandler>().ResumeMusic();
pausePanel.SetActive(false);
if (GamePlayerPauseMenu.Instance.editingUI == true) GamePlayerPauseMenu.Instance.ToggleEditingUI();
}