From 2eeaf52d6d23e305bfe48d6c327366b9945a146d Mon Sep 17 00:00:00 2001 From: Nanami-Houko Date: Thu, 3 Jul 2025 10:47:03 +0800 Subject: [PATCH] Hotfix for survival mode pagination --- .../ButtonObjects/UIButton_Patch.cs | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs index ec2228f..bd66319 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs @@ -2,6 +2,8 @@ using Il2Cpp; using Il2CppTMPro; using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using UnityEngine.UI; namespace PvZ_Fusion_Translator.Patches.GameObjects { @@ -10,30 +12,59 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects { [HarmonyPatch(nameof(UIButton.Start))] [HarmonyPostfix] - public static void Start(UIButton __instance) + private static void Postfix_Start(UIButton __instance) { try { - TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); - TextMeshProUGUI textShadow = __instance.transform.GetChild(1).GetComponent(); - StringStore.TranslateTextUI(text, false); - StringStore.TranslateTextUI(textShadow, false); - text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + // Convert and translate legacy Text components + Text[] textComponents = __instance.GetComponentsInChildren(true); + foreach (Text textComponent in textComponents) + { + if (textComponent != null && !string.IsNullOrEmpty(textComponent.text)) + { + string translatedText = StringStore.TranslateText(textComponent.text); + if (!string.IsNullOrEmpty(translatedText) && translatedText != textComponent.text) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + TextMeshProUGUI newTMP = StringStore.ConvertToTextMeshPro(textComponent, fontAsset, true); + + if (newTMP != null) + { + newTMP.text = translatedText; + } + } + } + } + + // Also handle TMP components directly under the button + if (__instance.transform.childCount >= 2) + { + TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); + TextMeshProUGUI textShadow = __instance.transform.GetChild(1).GetComponent(); + + StringStore.TranslateTextUI(text, false); + StringStore.TranslateTextUI(textShadow, false); + text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + } } catch (Exception) { } } [HarmonyPatch(nameof(UIButton.OnMouseUpAsButton))] [HarmonyPostfix] - public static void OnMouseUpAsButton(UIButton __instance) + public static void Postfix_OnMouseUpAsButton(UIButton __instance) { try { - TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); - TextMeshProUGUI textShadow = __instance.transform.GetChild(1).GetComponent(); - StringStore.TranslateTextUI(text, false); - StringStore.TranslateTextUI(textShadow, false); - text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + if (__instance.transform.childCount >= 2) + { + TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); + TextMeshProUGUI textShadow = __instance.transform.GetChild(1).GetComponent(); + + StringStore.TranslateTextUI(text, false); + StringStore.TranslateTextUI(textShadow, false); + text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + } } catch (Exception) { } }