From 26ce45297a95fe31139ebc09b65d58df2d669c49 Mon Sep 17 00:00:00 2001 From: Quantumrunner <58113888+Quantumrunner@users.noreply.github.com> Date: Tue, 13 Jan 2026 21:13:05 +0100 Subject: [PATCH] Added option in OptionsScreen to allow playing sound in background. --- .agent/rules/text-localization.md | 2 +- unity/Assets/Scripts/Game.cs | 11 ++++ .../Scripts/UI/Screens/OptionsScreen.cs | 54 ++++++++++++++----- .../text/Localization.Chinese.txt | 1 + .../text/Localization.Czech.txt | 1 + .../text/Localization.English.txt | 1 + .../text/Localization.French.txt | 1 + .../text/Localization.German.txt | 1 + .../text/Localization.Italian.txt | 1 + .../text/Localization.Japanese.txt | 1 + .../text/Localization.Korean.txt | 1 + .../text/Localization.Polish.txt | 1 + .../text/Localization.Portuguese.txt | 1 + .../text/Localization.Russian.txt | 1 + .../text/Localization.Spanish.txt | 1 + 15 files changed, 65 insertions(+), 14 deletions(-) diff --git a/.agent/rules/text-localization.md b/.agent/rules/text-localization.md index 3921e158..6fa12267 100644 --- a/.agent/rules/text-localization.md +++ b/.agent/rules/text-localization.md @@ -9,7 +9,7 @@ UI text should always get localized. Localization files are located in `Assets/S - The format is `KEY,Value`. - When adding new text: 1. Add the `KEY,English Value` to `Localization.English.txt`. -2. **CRITICAL**: Add a translated version `KEY,Translated Value` to *all* other relevant files (`Localization.German.txt`, `Localization.French.txt`, `Localization.Spanish.txt`, `Localization.Italian.txt`, etc.) **IMMEDIATELY**. Do not defer this task. Failing to do so will result in missing text for users of those languages. +2. **CRITICAL**: Add a translated version `KEY,Translated Value` to *all* other relevant files (`Localization.German.txt`, `Localization.French.txt`, `Localization.Spanish.txt`, `Localization.Italian.txt`, etc.) where the value is translated to the language specified in the filename **IMMEDIATELY**. Do not defer this task. Failing to do so will result in missing text for users of those languages. 3. In C# code, use `new StringKey("val", "KEY")` to reference the text. 4. For commonly used keys, add a static reference in `Assets/Scripts/Content/CommonStringKeys.cs`. 5. **VERIFICATION**: Before finishing the task, use find_by_name or list_dir to list all Localization.*.txt files. Confirm that the new key has been added and translated to the respective file language to EACH file. Do not assume; verify. \ No newline at end of file diff --git a/unity/Assets/Scripts/Game.cs b/unity/Assets/Scripts/Game.cs index 80e3388c..69470d6a 100644 --- a/unity/Assets/Scripts/Game.cs +++ b/unity/Assets/Scripts/Game.cs @@ -220,6 +220,17 @@ void Awake() debugTests = true; } + // Apply background audio setting + string s_playAudio = config.data.Get("UserConfig", "playAudioInBackground"); + if (s_playAudio == "1") + { + Application.runInBackground = true; + } + else + { + Application.runInBackground = false; + } + // Apply saved resolution and fullscreen settings string savedRes = config.data.Get("UserConfig", "resolution"); string savedFs = config.data.Get("UserConfig", "fullscreen"); diff --git a/unity/Assets/Scripts/UI/Screens/OptionsScreen.cs b/unity/Assets/Scripts/UI/Screens/OptionsScreen.cs index 9cf7f9bc..ef5a336e 100644 --- a/unity/Assets/Scripts/UI/Screens/OptionsScreen.cs +++ b/unity/Assets/Scripts/UI/Screens/OptionsScreen.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -29,6 +29,7 @@ public class OptionsScreen private readonly StringKey EXPORT_LOG = new StringKey("val", "EXPORT_LOG"); private readonly StringKey OptionON = new StringKey("val", "ON"); private readonly StringKey OptionOff = new StringKey("val", "OFF"); + private readonly StringKey PLAY_AUDIO_IN_BACKGROUND = new StringKey("val", "PLAY_AUDIO_IN_BACKGROUND"); Game game = Game.Get(); @@ -223,7 +224,7 @@ private void CreateEditorTransparencyElements() // Select language text UIElement ui = new UIElement(Game.DIALOG); - ui.SetLocation(UIScaler.GetHCenter() - 8, 5, 16, 2); + ui.SetLocation(UIScaler.GetHCenter() - 10, 5, 16, 2); ui.SetText(SET_EDITOR_ALPHA); ui.SetTextAlignment(TextAnchor.MiddleCenter); ui.SetFont(game.gameType.GetHeaderFont()); @@ -232,7 +233,7 @@ private void CreateEditorTransparencyElements() Texture2D SampleTex = ContentData.FileToTexture(game.cd.Get(IMG_LOW_EDITOR_TRANSPARENCY).image); Sprite SampleSprite = Sprite.Create(SampleTex, new Rect(0, 0, SampleTex.width, SampleTex.height), Vector2.zero, 1); ui = new UIElement(Game.DIALOG); - ui.SetLocation(UIScaler.GetHCenter() - 3, 8, 6, 6); + ui.SetLocation(UIScaler.GetHCenter() - 5, 8, 6, 6); ui.SetButton(delegate { UpdateEditorTransparency(0.2f); }); ui.SetImage(SampleSprite); if (game.editorTransparency == 0.2f) @@ -241,7 +242,7 @@ private void CreateEditorTransparencyElements() SampleTex = ContentData.FileToTexture(game.cd.Get(IMG_MEDIUM_EDITOR_TRANSPARENCY).image); SampleSprite = Sprite.Create(SampleTex, new Rect(0, 0, SampleTex.width, SampleTex.height), Vector2.zero, 1); ui = new UIElement(Game.DIALOG); - ui.SetLocation(UIScaler.GetHCenter() - 3, 15, 6, 6); + ui.SetLocation(UIScaler.GetHCenter() - 5, 15, 6, 6); ui.SetButton(delegate { UpdateEditorTransparency(0.3f); }); ui.SetImage(SampleSprite); if (game.editorTransparency == 0.3f) @@ -250,7 +251,7 @@ private void CreateEditorTransparencyElements() SampleTex = ContentData.FileToTexture(game.cd.Get(IMG_HIGH_EDITOR_TRANSPARENCY).image); SampleSprite = Sprite.Create(SampleTex, new Rect(0, 0, SampleTex.width, SampleTex.height), Vector2.zero, 1); ui = new UIElement(Game.DIALOG); - ui.SetLocation(UIScaler.GetHCenter() - 3, 22, 6, 6); + ui.SetLocation(UIScaler.GetHCenter() - 5, 22, 6, 6); ui.SetButton(delegate { UpdateEditorTransparency(0.4f); }); ui.SetImage(SampleSprite); if (game.editorTransparency == 0.4f) @@ -261,7 +262,7 @@ private void CreateEditorTransparencyElements() private void CreateAudioElements() { UIElement ui = new UIElement(); - ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 4, 5, 10, 2); + ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 4, 4, 10, 2); ui.SetText(MUSIC); ui.SetFont(game.gameType.GetHeaderFont()); ui.SetFontSize(UIScaler.GetMediumFont()); @@ -272,7 +273,7 @@ private void CreateAudioElements() if (vSet.Length == 0) mVolume = 1; ui = new UIElement(); - ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 6, 8, 14, 2); + ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 6, 6, 14, 2); ui.SetBGColor(Color.clear); new UIElementBorder(ui); @@ -281,7 +282,7 @@ private void CreateAudioElements() musicSlideObj.transform.SetParent(game.uICanvas.transform); musicSlide = musicSlideObj.AddComponent(); RectTransform musicSlideRect = musicSlideObj.GetComponent(); - musicSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 8 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); + musicSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 6 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); musicSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, ((0.75f * UIScaler.GetWidthUnits()) - 6) * UIScaler.GetPixelsPerUnit(), 14 * UIScaler.GetPixelsPerUnit()); musicSlide.onValueChanged.AddListener(delegate { UpdateMusic(); }); @@ -300,7 +301,7 @@ private void CreateAudioElements() musicSlideObjRev.transform.SetParent(game.uICanvas.transform); musicSlideRev = musicSlideObjRev.AddComponent(); RectTransform musicSlideRectRev = musicSlideObjRev.GetComponent(); - musicSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 8 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); + musicSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 6 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); musicSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, ((0.75f * UIScaler.GetWidthUnits()) - 6) * UIScaler.GetPixelsPerUnit(), 14 * UIScaler.GetPixelsPerUnit()); musicSlideRev.onValueChanged.AddListener(delegate { UpdateMusicRev(); }); musicSlideRev.direction = Slider.Direction.RightToLeft; @@ -319,7 +320,7 @@ private void CreateAudioElements() musicSlideRev.value = 1 - mVolume; ui = new UIElement(); - ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 4, 11, 10, 2); + ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 4, 8.5f, 10, 2); ui.SetText(EFFECTS); ui.SetFont(game.gameType.GetHeaderFont()); ui.SetFontSize(UIScaler.GetMediumFont()); @@ -330,7 +331,7 @@ private void CreateAudioElements() if (vSet.Length == 0) eVolume = 1; ui = new UIElement(); - ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 6, 14, 14, 2); + ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 6, 10.5f, 14, 2); ui.SetBGColor(Color.clear); new UIElementBorder(ui); @@ -339,7 +340,7 @@ private void CreateAudioElements() effectSlideObj.transform.SetParent(game.uICanvas.transform); effectSlide = effectSlideObj.AddComponent(); RectTransform effectSlideRect = effectSlideObj.GetComponent(); - effectSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 14 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); + effectSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 10.5f * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); effectSlideRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, ((0.75f * UIScaler.GetWidthUnits()) - 6) * UIScaler.GetPixelsPerUnit(), 14 * UIScaler.GetPixelsPerUnit()); effectSlide.onValueChanged.AddListener(delegate { UpdateEffects(); }); EventTrigger.Entry entry = new EventTrigger.Entry(); @@ -362,7 +363,7 @@ private void CreateAudioElements() effectSlideObjRev.transform.SetParent(game.uICanvas.transform); effectSlideRev = effectSlideObjRev.AddComponent(); RectTransform effectSlideRectRev = effectSlideObjRev.GetComponent(); - effectSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 14 * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); + effectSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 10.5f * UIScaler.GetPixelsPerUnit(), 2 * UIScaler.GetPixelsPerUnit()); effectSlideRectRev.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, ((0.75f * UIScaler.GetWidthUnits()) - 6) * UIScaler.GetPixelsPerUnit(), 14 * UIScaler.GetPixelsPerUnit()); effectSlideRev.onValueChanged.AddListener(delegate { UpdateEffectsRev(); }); effectSlideRev.direction = Slider.Direction.RightToLeft; @@ -379,6 +380,33 @@ private void CreateAudioElements() effectSlide.value = eVolume; effectSlideRev.value = 1 - eVolume; + + // Background Audio Toggle + // Only render on Windows, Mac or Linux (player or editor) + var p = Application.platform; + bool isSupportedPlatform = + p == RuntimePlatform.WindowsPlayer || p == RuntimePlatform.OSXPlayer || p == RuntimePlatform.LinuxPlayer + || p == RuntimePlatform.WindowsEditor || p == RuntimePlatform.OSXEditor || p == RuntimePlatform.LinuxEditor; + + if (isSupportedPlatform) + { + // Check config + string configBgAudio = game.config.data.Get("UserConfig", "playAudioInBackground"); + bool isBgAudio = configBgAudio == "1"; + + ui = new UIElement(); + ui.SetLocation((0.75f * UIScaler.GetWidthUnits()) - 6, 13.5f, 14, 2); + ui.SetText(PLAY_AUDIO_IN_BACKGROUND); + ui.SetButton(delegate + { + bool newState = !isBgAudio; + Application.runInBackground = newState; + game.config.data.Add("UserConfig", "playAudioInBackground", newState ? "1" : "0"); + game.config.Save(); + new OptionsScreen(); + }); + new UIElementBorder(ui, isBgAudio ? Color.white : Color.grey); + } } diff --git a/unity/Assets/StreamingAssets/text/Localization.Chinese.txt b/unity/Assets/StreamingAssets/text/Localization.Chinese.txt index b63cdc35..99686655 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Chinese.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Chinese.txt @@ -261,6 +261,7 @@ spellattack,特殊攻擊 //Audio +PLAY_AUDIO_IN_BACKGROUND,后台播放音频 menu,目錄 music,音樂 quest,劇本 diff --git a/unity/Assets/StreamingAssets/text/Localization.Czech.txt b/unity/Assets/StreamingAssets/text/Localization.Czech.txt index 7d2ed32c..893eedc6 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Czech.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Czech.txt @@ -431,6 +431,7 @@ spelldefence,Obranné kouzlo spellattack,Útočné kouzlo //Audio +PLAY_AUDIO_IN_BACKGROUND,Přehrávat zvuk na pozadí menu,Menu music,Hudba quest,Úkol diff --git a/unity/Assets/StreamingAssets/text/Localization.English.txt b/unity/Assets/StreamingAssets/text/Localization.English.txt index 3c379804..3e344b19 100644 --- a/unity/Assets/StreamingAssets/text/Localization.English.txt +++ b/unity/Assets/StreamingAssets/text/Localization.English.txt @@ -427,6 +427,7 @@ spelldefence,Defence spell spellattack,Attack spell //Audio +PLAY_AUDIO_IN_BACKGROUND,Play Audio in Background menu,Menu music,Music quest,Quest diff --git a/unity/Assets/StreamingAssets/text/Localization.French.txt b/unity/Assets/StreamingAssets/text/Localization.French.txt index ea40bf8a..4154e9d8 100644 --- a/unity/Assets/StreamingAssets/text/Localization.French.txt +++ b/unity/Assets/StreamingAssets/text/Localization.French.txt @@ -259,6 +259,7 @@ evidence,Preuve ally,Allié //Audio +PLAY_AUDIO_IN_BACKGROUND,Lire l'audio en arrière-plan menu,Menu music,Musique newround,Nouveau tour diff --git a/unity/Assets/StreamingAssets/text/Localization.German.txt b/unity/Assets/StreamingAssets/text/Localization.German.txt index d0b9c12e..5985ab6b 100644 --- a/unity/Assets/StreamingAssets/text/Localization.German.txt +++ b/unity/Assets/StreamingAssets/text/Localization.German.txt @@ -257,6 +257,7 @@ spelldefence,Verteid.-Zauber spellattack,Angriff-Zauber //Audio +PLAY_AUDIO_IN_BACKGROUND,Audio im Hintergrund abspielen menu,Menü music,Musik quest,Szenario diff --git a/unity/Assets/StreamingAssets/text/Localization.Italian.txt b/unity/Assets/StreamingAssets/text/Localization.Italian.txt index 1ed12416..c7f46870 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Italian.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Italian.txt @@ -201,6 +201,7 @@ evidence,Prova ally,Alleato //Audio +PLAY_AUDIO_IN_BACKGROUND,Riproduci audio in background menu,Menu music,Musica quest,Scenario diff --git a/unity/Assets/StreamingAssets/text/Localization.Japanese.txt b/unity/Assets/StreamingAssets/text/Localization.Japanese.txt index d8611c01..019e5c38 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Japanese.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Japanese.txt @@ -484,6 +484,7 @@ spelldefence,特殊防御 spellattack,特殊攻撃 //Audio +PLAY_AUDIO_IN_BACKGROUND,バックグラウンドでオーディオを再生 menu,メニュー music,音楽 quest,クエスト diff --git a/unity/Assets/StreamingAssets/text/Localization.Korean.txt b/unity/Assets/StreamingAssets/text/Localization.Korean.txt index 030f0df1..e6cdab20 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Korean.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Korean.txt @@ -424,6 +424,7 @@ spelldefence,방어 마법 spellattack,공격 마법 //Audio +PLAY_AUDIO_IN_BACKGROUND,백그라운드에서 오디오 재생 menu,메뉴 music,음악 quest,퀘스트 diff --git a/unity/Assets/StreamingAssets/text/Localization.Polish.txt b/unity/Assets/StreamingAssets/text/Localization.Polish.txt index 347b1621..6eafa1ed 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Polish.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Polish.txt @@ -248,6 +248,7 @@ evidence,Dowód ally,Sprzymieńca //Audio +PLAY_AUDIO_IN_BACKGROUND,Odtwarzaj dźwięk w tle menu,Menu music,Muzyka quest,Scenariusz diff --git a/unity/Assets/StreamingAssets/text/Localization.Portuguese.txt b/unity/Assets/StreamingAssets/text/Localization.Portuguese.txt index 125c9dc4..3df8344e 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Portuguese.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Portuguese.txt @@ -265,6 +265,7 @@ spelldefence,Feitiço de Defesa spellattack,Feitiço de Ataque //Audio +PLAY_AUDIO_IN_BACKGROUND,Reproduzir áudio em segundo plano menu,Menu music,Música quest,Aventura diff --git a/unity/Assets/StreamingAssets/text/Localization.Russian.txt b/unity/Assets/StreamingAssets/text/Localization.Russian.txt index 92e0567b..d1720006 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Russian.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Russian.txt @@ -258,6 +258,7 @@ spelldefence,Защитное заклинаниеэ spellattack,Атакующее заклинание //Audio +PLAY_AUDIO_IN_BACKGROUND,Воспроизводить звук в фоне menu,Меню music,Музыка quest,Задание diff --git a/unity/Assets/StreamingAssets/text/Localization.Spanish.txt b/unity/Assets/StreamingAssets/text/Localization.Spanish.txt index a42f2617..8a5de626 100644 --- a/unity/Assets/StreamingAssets/text/Localization.Spanish.txt +++ b/unity/Assets/StreamingAssets/text/Localization.Spanish.txt @@ -256,6 +256,7 @@ evidence,Pista ally,Aliado //Audio +PLAY_AUDIO_IN_BACKGROUND,Reproducir audio en segundo plano menu,Menú music,Música quest,Escenario