Music and Sound Settins functionality and UI adjustments
parent
763a24af1e
commit
2f294b3d7c
@ -0,0 +1,133 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class AudioSourceController : MonoBehaviour
|
||||
{
|
||||
public AudioSource[] audioSources;
|
||||
public Scrollbar VolScroll;
|
||||
public GameObject OnButton;
|
||||
public GameObject OffButton;
|
||||
|
||||
private float lastSavedVolume;
|
||||
private bool lastSavedMusicState;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
audioSourceGetter();
|
||||
Invoke(nameof(audioSourceGetter), 2); // Ensures delayed fetching in second scene
|
||||
|
||||
// Ensure default music is ON if running for the first time
|
||||
if (!PlayerPrefs.HasKey("MusicBool"))
|
||||
{
|
||||
PlayerPrefs.SetInt("MusicBool", 1); // Default to ON
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
// Load saved settings
|
||||
lastSavedMusicState = PlayerPrefs.GetInt("MusicBool") == 1;
|
||||
lastSavedVolume = PlayerPrefs.GetFloat("Volume", 1);
|
||||
|
||||
// Apply the saved state
|
||||
Set_Value(lastSavedVolume);
|
||||
ApplyAudioState(lastSavedMusicState);
|
||||
}
|
||||
|
||||
public void audioSourceGetter()
|
||||
{
|
||||
audioSources = FindObjectsOfType<AudioSource>();
|
||||
Set_Value(PlayerPrefs.GetFloat("Volume", 1));
|
||||
}
|
||||
|
||||
public void PlayerPrefSaver()
|
||||
{
|
||||
if (VolScroll != null)
|
||||
{
|
||||
PlayerPrefs.SetFloat("Volume", VolScroll.value);
|
||||
}
|
||||
|
||||
PlayerPrefs.SetInt("MusicBool", OffButton.activeSelf ? 1 : 0);
|
||||
PlayerPrefs.Save(); // Ensure immediate save
|
||||
|
||||
// Update last saved state
|
||||
lastSavedVolume = VolScroll.value;
|
||||
lastSavedMusicState = OffButton.activeSelf;
|
||||
}
|
||||
|
||||
public void Delayed_Starter()
|
||||
{
|
||||
Invoke(nameof(Awake), 0.5f);
|
||||
}
|
||||
|
||||
public void Set_Value(float value)
|
||||
{
|
||||
if (audioSources == null || audioSources.Length == 0)
|
||||
audioSourceGetter(); // Ensures sources are updated if needed
|
||||
|
||||
foreach (var source in audioSources)
|
||||
{
|
||||
source.volume = value;
|
||||
}
|
||||
|
||||
if (VolScroll != null && VolScroll.value != value)
|
||||
{
|
||||
VolScroll.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
// **New Functions for On/Off Button Assignment in Inspector**
|
||||
public void ToggleAudioOn()
|
||||
{
|
||||
ApplyAudioState(true);
|
||||
}
|
||||
|
||||
public void ToggleAudioOff()
|
||||
{
|
||||
ApplyAudioState(false);
|
||||
}
|
||||
|
||||
// **Handles audio state & button switching**
|
||||
private void ApplyAudioState(bool isOn)
|
||||
{
|
||||
if (isOn)
|
||||
{
|
||||
OnButton.SetActive(false);
|
||||
OffButton.SetActive(true);
|
||||
PlayAudio();
|
||||
}
|
||||
else
|
||||
{
|
||||
OffButton.SetActive(false);
|
||||
OnButton.SetActive(true);
|
||||
StopAudio();
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAudio()
|
||||
{
|
||||
if (audioSources == null || audioSources.Length == 0)
|
||||
audioSourceGetter();
|
||||
|
||||
foreach (var source in audioSources)
|
||||
{
|
||||
source.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void StopAudio()
|
||||
{
|
||||
if (audioSources == null || audioSources.Length == 0)
|
||||
audioSourceGetter();
|
||||
|
||||
foreach (var source in audioSources)
|
||||
{
|
||||
source.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
// **Cancel Button - Resets to last saved settings**
|
||||
public void CancelSettings()
|
||||
{
|
||||
Set_Value(lastSavedVolume);
|
||||
ApplyAudioState(lastSavedMusicState);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e27a29ed46a63b541be4dbf759435497
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
@ -0,0 +1,140 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecf495d1903f9ff4d93ae184ea2a6a5d
|
||||
TextureImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 13
|
||||
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
|
||||
flipGreenChannel: 0
|
||||
isReadable: 0
|
||||
streamingMipmaps: 0
|
||||
streamingMipmapsPriority: 0
|
||||
vTOnly: 0
|
||||
ignoreMipmapLimit: 0
|
||||
grayScaleToAlpha: 0
|
||||
generateCubemap: 6
|
||||
cubemapConvolution: 0
|
||||
seamlessCubemap: 0
|
||||
textureFormat: 1
|
||||
maxTextureSize: 2048
|
||||
textureSettings:
|
||||
serializedVersion: 2
|
||||
filterMode: 1
|
||||
aniso: 1
|
||||
mipBias: 0
|
||||
wrapU: 1
|
||||
wrapV: 1
|
||||
wrapW: 0
|
||||
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
|
||||
flipbookRows: 1
|
||||
flipbookColumns: 1
|
||||
maxTextureSizeSet: 0
|
||||
compressionQualitySet: 0
|
||||
textureFormatSet: 0
|
||||
ignorePngGamma: 0
|
||||
applyGammaDecoding: 0
|
||||
swizzle: 50462976
|
||||
cookieLightType: 0
|
||||
platformSettings:
|
||||
- serializedVersion: 3
|
||||
buildTarget: DefaultTexturePlatform
|
||||
maxTextureSize: 2048
|
||||
resizeAlgorithm: 0
|
||||
textureFormat: -1
|
||||
textureCompression: 1
|
||||
compressionQuality: 50
|
||||
crunchedCompression: 0
|
||||
allowsAlphaSplitting: 0
|
||||
overridden: 0
|
||||
ignorePlatformSupport: 0
|
||||
androidETC2FallbackOverride: 0
|
||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||
- serializedVersion: 3
|
||||
buildTarget: Standalone
|
||||
maxTextureSize: 2048
|
||||
resizeAlgorithm: 0
|
||||
textureFormat: -1
|
||||
textureCompression: 1
|
||||
compressionQuality: 50
|
||||
crunchedCompression: 0
|
||||
allowsAlphaSplitting: 0
|
||||
overridden: 0
|
||||
ignorePlatformSupport: 0
|
||||
androidETC2FallbackOverride: 0
|
||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||
- serializedVersion: 3
|
||||
buildTarget: iPhone
|
||||
maxTextureSize: 2048
|
||||
resizeAlgorithm: 0
|
||||
textureFormat: -1
|
||||
textureCompression: 1
|
||||
compressionQuality: 50
|
||||
crunchedCompression: 0
|
||||
allowsAlphaSplitting: 0
|
||||
overridden: 0
|
||||
ignorePlatformSupport: 0
|
||||
androidETC2FallbackOverride: 0
|
||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||
- serializedVersion: 3
|
||||
buildTarget: Android
|
||||
maxTextureSize: 2048
|
||||
resizeAlgorithm: 0
|
||||
textureFormat: -1
|
||||
textureCompression: 1
|
||||
compressionQuality: 50
|
||||
crunchedCompression: 0
|
||||
allowsAlphaSplitting: 0
|
||||
overridden: 0
|
||||
ignorePlatformSupport: 0
|
||||
androidETC2FallbackOverride: 0
|
||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||
spriteSheet:
|
||||
serializedVersion: 2
|
||||
sprites: []
|
||||
outline: []
|
||||
physicsShape: []
|
||||
bones: []
|
||||
spriteID: 5e97eb03825dee720800000000000000
|
||||
internalID: 0
|
||||
vertices: []
|
||||
indices:
|
||||
edges: []
|
||||
weights: []
|
||||
secondaryTextures: []
|
||||
nameFileIdTable: {}
|
||||
mipmapLimitGroupName:
|
||||
pSDRemoveMatte: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Binary file not shown.
Before Width: | Height: | Size: 477 KiB After Width: | Height: | Size: 554 KiB |
Loading…
Reference in New Issue