From 2fc7041438513eddbf1da3ce82bbf139bb5412da Mon Sep 17 00:00:00 2001 From: D1GQ Date: Wed, 4 Mar 2026 13:34:45 -0600 Subject: [PATCH] Add preview to outfit presets --- src/Data/Json/OutfitData.cs | 17 +++++ src/Helpers/ActionExtensions.cs | 69 +++++++++++++++++++++ src/Helpers/ObjectHelper.cs | 15 +++++ src/Helpers/PlayerControlHelper.cs | 3 +- src/Helpers/Utils.cs | 1 - src/Patches/Gameplay/UI/PlayerTabPatch.cs | 75 ++++++++++++++++++----- 6 files changed, 161 insertions(+), 19 deletions(-) create mode 100644 src/Helpers/ActionExtensions.cs diff --git a/src/Data/Json/OutfitData.cs b/src/Data/Json/OutfitData.cs index ec0ccc6..55d2cf8 100644 --- a/src/Data/Json/OutfitData.cs +++ b/src/Data/Json/OutfitData.cs @@ -142,4 +142,21 @@ internal sealed class OutfitData VisorId = DataManager.Player.Customization.Visor; NamePlateId = DataManager.Player.Customization.NamePlate; } + + /// + /// Converts to network player outfit. + /// + /// + internal NetworkedPlayerInfo.PlayerOutfit ToPlayerOutfit() + { + return new NetworkedPlayerInfo.PlayerOutfit + { + HatId = HatId, + PetId = PetId, + SkinId = SkinId, + VisorId = VisorId, + NamePlateId = NamePlateId, + ColorId = DataManager.Player.Customization.Color + }; + } } \ No newline at end of file diff --git a/src/Helpers/ActionExtensions.cs b/src/Helpers/ActionExtensions.cs new file mode 100644 index 0000000..05bc80d --- /dev/null +++ b/src/Helpers/ActionExtensions.cs @@ -0,0 +1,69 @@ +using UnityEngine.Events; + +namespace BetterAmongUs.Helpers; + +/// +/// Provides extension methods for UnityEvent classes to simplify adding listeners with Action delegates. +/// +internal static class ActionExtensions +{ + /// + /// Adds a listener to the UnityEvent that invokes the specified action when the event is raised. + /// + /// The UnityEvent to add the listener to. + /// The action to invoke when the event is raised. + internal static void AddListener(this UnityEvent unityEvent, Action action) + { + unityEvent.AddListener(action); + } + + /// + /// Adds a listener to the UnityEvent<T0> that invokes the specified action when the event is raised. + /// + /// The type of the first event parameter. + /// The UnityEvent<T0> to add the listener to. + /// The action to invoke when the event is raised, accepting one parameter of type T0. + internal static void AddListener(this UnityEvent unityEvent, Action action) + { + unityEvent.AddListener(action); + } + + /// + /// Adds a listener to the UnityEvent<T0, T1> that invokes the specified action when the event is raised. + /// + /// The type of the first event parameter. + /// The type of the second event parameter. + /// The UnityEvent<T0, T1> to add the listener to. + /// The action to invoke when the event is raised, accepting two parameters of types T0 and T1. + internal static void AddListener(this UnityEvent unityEvent, Action action) + { + unityEvent.AddListener(action); + } + + /// + /// Adds a listener to the UnityEvent<T0, T1, T2> that invokes the specified action when the event is raised. + /// + /// The type of the first event parameter. + /// The type of the second event parameter. + /// The type of the third event parameter. + /// The UnityEvent<T0, T1, T2> to add the listener to. + /// The action to invoke when the event is raised, accepting three parameters of types T0, T1, and T2. + internal static void AddListener(this UnityEvent unityEvent, Action action) + { + unityEvent.AddListener(action); + } + + /// + /// Adds a listener to the UnityEvent<T0, T1, T2, T3> that invokes the specified action when the event is raised. + /// + /// The type of the first event parameter. + /// The type of the second event parameter. + /// The type of the third event parameter. + /// The type of the fourth event parameter. + /// The UnityEvent<T0, T1, T2, T3> to add the listener to. + /// The action to invoke when the event is raised, accepting four parameters of types T0, T1, T2, and T3. + internal static void AddListener(this UnityEvent unityEvent, Action action) + { + unityEvent.AddListener(action); + } +} \ No newline at end of file diff --git a/src/Helpers/ObjectHelper.cs b/src/Helpers/ObjectHelper.cs index cdd6bdc..ade0bb5 100644 --- a/src/Helpers/ObjectHelper.cs +++ b/src/Helpers/ObjectHelper.cs @@ -54,6 +54,21 @@ internal static class ObjectHelper /// The MonoBehaviour whose GameObject should be searched. internal static void DestroyTextTranslators(this MonoBehaviour mono) => mono.gameObject.DestroyTextTranslators(); + + /// + /// Destroys a specified component on a game object + /// + /// The Type of the Component to destroy + /// + internal static void RemoveComponent(this GameObject obj) where T : Component + { + var comp = obj.GetComponent(); + if (comp != null) + { + UnityEngine.Object.Destroy(comp); + } + } + /// /// Sets the color of all SpriteRenderer components in the children of a GameObject. /// diff --git a/src/Helpers/PlayerControlHelper.cs b/src/Helpers/PlayerControlHelper.cs index c4c9a27..59facd5 100644 --- a/src/Helpers/PlayerControlHelper.cs +++ b/src/Helpers/PlayerControlHelper.cs @@ -12,7 +12,7 @@ namespace BetterAmongUs.Helpers; /// /// Provides extension methods and utilities for working with PlayerControl instances. /// -static class PlayerControlHelper +internal static class PlayerControlHelper { /// /// Gets the ClientData associated with a player. @@ -83,6 +83,7 @@ static class PlayerControlHelper { return false; } + return true; } diff --git a/src/Helpers/Utils.cs b/src/Helpers/Utils.cs index 6b77f51..831683a 100644 --- a/src/Helpers/Utils.cs +++ b/src/Helpers/Utils.cs @@ -203,7 +203,6 @@ internal static class Utils } catch { - // Intentionally empty - chat failures shouldn't crash the game } } diff --git a/src/Patches/Gameplay/UI/PlayerTabPatch.cs b/src/Patches/Gameplay/UI/PlayerTabPatch.cs index c1f17da..2aa3958 100644 --- a/src/Patches/Gameplay/UI/PlayerTabPatch.cs +++ b/src/Patches/Gameplay/UI/PlayerTabPatch.cs @@ -1,4 +1,5 @@ -using BetterAmongUs.Data; +using AmongUs.Data; +using BetterAmongUs.Data; using BetterAmongUs.Data.Json; using BetterAmongUs.Helpers; using BetterAmongUs.Patches.Client.Managers; @@ -12,26 +13,41 @@ namespace BetterAmongUs.Patches.Gameplay.UI; internal static class PlayerTabPatch { private static List presetButtons = []; // Outfit preset buttons + private static List presetPreviews = []; // Outfit previews private static float cooldown = 0f; // Button click cooldown + private static readonly List _favoriteIcons = []; [HarmonyPatch(typeof(PlayerTab), nameof(PlayerTab.OnEnable))] - [HarmonyPrefix] - private static void PlayerTab_OnEnable_Prefix(PlayerTab __instance) + [HarmonyPostfix] + private static void PlayerTab_OnEnable_Postfix(PlayerTab __instance) { - // Clean up old preset buttons + SetupOutfitPresets(__instance); + SetupFavoriteColor(__instance); + } + + private static void SetupOutfitPresets(PlayerTab playerTab) + { + // Clean up old preset buttons and previews foreach (var button in presetButtons.ToArray()) { if (button == null) continue; UnityEngine.Object.Destroy(button.gameObject); } presetButtons.Clear(); + foreach (var preview in presetPreviews.ToArray()) + { + if (preview == null) continue; + UnityEngine.Object.Destroy(preview.gameObject); + } + presetPreviews.Clear(); // Create preset buttons (0 = Among Us preset, 1-5 = custom presets) for (int i = 0; i <= 5; i++) { int currentI = i; var name = currentI == 0 ? "Among Us Preset" : $"Preset {i}"; - var button = __instance.CreateButton(name, new Vector3(2.5f, 1.55f - currentI * 0.45f, 0f), () => + var data = OutfitData.GetOutfitData(currentI); + var button = playerTab.CreateOutfitPresetButton(name, new Vector3(2.5f, 1.55f - currentI * 0.45f, 0f), out var playerPreview, () => { // Ignore if cooldown active or same preset selected if (cooldown > 0f || BetterDataManager.BetterDataFile.SelectedOutfitPreset == currentI) return; @@ -48,36 +64,38 @@ internal static class PlayerTabPatch } // Load and apply outfit from preset - var data = OutfitData.GetOutfitData(currentI); data.Load(() => { if (LoadPlayerOutfit(data)) { - __instance.PlayerPreview.UpdateFromLocalPlayer(PlayerMaterial.MaskType.None); + playerTab.PlayerPreview.UpdateFromLocalPlayer(PlayerMaterial.MaskType.None); } else { - __instance.PlayerPreview.UpdateFromDataManager(PlayerMaterial.MaskType.None); + playerTab.PlayerPreview.UpdateFromDataManager(PlayerMaterial.MaskType.None); } }); }); + + // Set up preview + playerPreview.UpdateFromPlayerOutfit(data.ToPlayerOutfit(), PlayerMaterial.MaskType.None, false, true); + playerPreview.ToggleName(false); + playerPreview.transform.position += new Vector3(0f, 0f, -1f * currentI); // Fix rendering order + presetPreviews.Add(playerPreview); + presetButtons.Add(button); } } - private static readonly List _favoriteIcons = []; - - [HarmonyPatch(typeof(PlayerTab), nameof(PlayerTab.OnEnable))] - [HarmonyPostfix] - private static void PlayerTab_OnEnable_Postfix(PlayerTab __instance) + private static void SetupFavoriteColor(PlayerTab playerTab) { _favoriteIcons.Clear(); // Add favorite functionality to color chips - for (int i = 0; i < __instance.ColorChips.Count; i++) + for (int i = 0; i < playerTab.ColorChips.Count; i++) { var index = i; - var colorChip = __instance.ColorChips[i]; + var colorChip = playerTab.ColorChips[i]; // Override click behavior colorChip.Button.OnClick = new(); @@ -100,7 +118,7 @@ internal static class PlayerTabPatch } // Normal click = equip color - __instance.ClickEquip(); + playerTab.ClickEquip(); })); // Add favorite star indicator @@ -142,18 +160,34 @@ internal static class PlayerTabPatch } // Helper to create preset buttons - private static PassiveButton CreateButton(this PlayerTab __instance, string name, Vector3 pos, Action callback) + private static PassiveButton CreateOutfitPresetButton(this PlayerTab __instance, string name, Vector3 pos, out PoolablePlayer playerPreview, Action callback) { var button = UnityEngine.Object.Instantiate(MainMenuManagerPatch.ButtonPrefab, __instance.transform); button.gameObject.SetActive(true); button.gameObject.SetLayers("UI"); button.transform.localPosition = pos; button.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); + + // Create preview + button.transform.Find("Highlight/Icon")?.gameObject.DestroyObj(); + button.transform.Find("Inactive/Icon")?.gameObject.DestroyObj(); + var outfitPreview = UnityEngine.Object.Instantiate(__instance.PlayerPreview); + outfitPreview.transform.SetParent(button.transform); + outfitPreview.transform.localPosition = new Vector3(-1.3f, 0.15f, -10f); + outfitPreview.transform.localScale = Vector3.one * 0.45f; + outfitPreview.ResetCosmetics(); + foreach (var pet in outfitPreview.GetComponentsInChildren(true)) + { + pet.gameObject.DestroyObj(); + } + playerPreview = outfitPreview; + button.OnClick = new(); button.OnClick.AddListener(callback); button.DestroyTextTranslators(); var text = button.GetComponentInChildren(); text?.SetText(name); + return button; } @@ -180,6 +214,13 @@ internal static class PlayerTabPatch { button.SetPassiveButtonHoverStateActive(); // Highlight selected preset } + + // Update preview color + var preview = presetPreviews[i]; + if (preview.Cosmetics.ColorId != DataManager.Player.Customization.Color) + { + preview.SetBodyColor(DataManager.Player.Customization.Color); + } } } } \ No newline at end of file