Add preview to outfit presets

This commit is contained in:
D1GQ committed 2026-03-04 13:34:45 -06:00
1 parent 589cf8285d
commit 2fc7041438
6 files changed
+161 -19

No files matched your search

+17
View File
@@ -142,4 +142,21 @@ internal sealed class OutfitData
VisorId = DataManager.Player.Customization.Visor;
NamePlateId = DataManager.Player.Customization.NamePlate;
}
/// <summary>
/// Converts to network player outfit.
/// </summary>
/// <returns></returns>
internal NetworkedPlayerInfo.PlayerOutfit ToPlayerOutfit()
{
return new NetworkedPlayerInfo.PlayerOutfit
{
HatId = HatId,
PetId = PetId,
SkinId = SkinId,
VisorId = VisorId,
NamePlateId = NamePlateId,
ColorId = DataManager.Player.Customization.Color
};
}
}
+69
View File
@@ -0,0 +1,69 @@
using UnityEngine.Events;
namespace BetterAmongUs.Helpers;
/// <summary>
/// Provides extension methods for UnityEvent classes to simplify adding listeners with Action delegates.
/// </summary>
internal static class ActionExtensions
{
/// <summary>
/// Adds a listener to the UnityEvent that invokes the specified action when the event is raised.
/// </summary>
/// <param name="unityEvent">The UnityEvent to add the listener to.</param>
/// <param name="action">The action to invoke when the event is raised.</param>
internal static void AddListener(this UnityEvent unityEvent, Action action)
{
unityEvent.AddListener(action);
}
/// <summary>
/// Adds a listener to the UnityEvent&lt;T0&gt; that invokes the specified action when the event is raised.
/// </summary>
/// <typeparam name="T0">The type of the first event parameter.</typeparam>
/// <param name="unityEvent">The UnityEvent&lt;T0&gt; to add the listener to.</param>
/// <param name="action">The action to invoke when the event is raised, accepting one parameter of type T0.</param>
internal static void AddListener<T0>(this UnityEvent<T0> unityEvent, Action<T0> action)
{
unityEvent.AddListener(action);
}
/// <summary>
/// Adds a listener to the UnityEvent&lt;T0, T1&gt; that invokes the specified action when the event is raised.
/// </summary>
/// <typeparam name="T0">The type of the first event parameter.</typeparam>
/// <typeparam name="T1">The type of the second event parameter.</typeparam>
/// <param name="unityEvent">The UnityEvent&lt;T0, T1&gt; to add the listener to.</param>
/// <param name="action">The action to invoke when the event is raised, accepting two parameters of types T0 and T1.</param>
internal static void AddListener<T0, T1>(this UnityEvent<T0, T1> unityEvent, Action<T0, T1> action)
{
unityEvent.AddListener(action);
}
/// <summary>
/// Adds a listener to the UnityEvent&lt;T0, T1, T2&gt; that invokes the specified action when the event is raised.
/// </summary>
/// <typeparam name="T0">The type of the first event parameter.</typeparam>
/// <typeparam name="T1">The type of the second event parameter.</typeparam>
/// <typeparam name="T2">The type of the third event parameter.</typeparam>
/// <param name="unityEvent">The UnityEvent&lt;T0, T1, T2&gt; to add the listener to.</param>
/// <param name="action">The action to invoke when the event is raised, accepting three parameters of types T0, T1, and T2.</param>
internal static void AddListener<T0, T1, T2>(this UnityEvent<T0, T1, T2> unityEvent, Action<T0, T1, T2> action)
{
unityEvent.AddListener(action);
}
/// <summary>
/// Adds a listener to the UnityEvent&lt;T0, T1, T2, T3&gt; that invokes the specified action when the event is raised.
/// </summary>
/// <typeparam name="T0">The type of the first event parameter.</typeparam>
/// <typeparam name="T1">The type of the second event parameter.</typeparam>
/// <typeparam name="T2">The type of the third event parameter.</typeparam>
/// <typeparam name="T3">The type of the fourth event parameter.</typeparam>
/// <param name="unityEvent">The UnityEvent&lt;T0, T1, T2, T3&gt; to add the listener to.</param>
/// <param name="action">The action to invoke when the event is raised, accepting four parameters of types T0, T1, T2, and T3.</param>
internal static void AddListener<T0, T1, T2, T3>(this UnityEvent<T0, T1, T2, T3> unityEvent, Action<T0, T1, T2, T3> action)
{
unityEvent.AddListener(action);
}
}
+15
View File
@@ -54,6 +54,21 @@ internal static class ObjectHelper
/// <param name="mono">The MonoBehaviour whose GameObject should be searched.</param>
internal static void DestroyTextTranslators(this MonoBehaviour mono) => mono.gameObject.DestroyTextTranslators();
/// <summary>
/// Destroys a specified component on a game object
/// </summary>
/// <typeparam name="T">The Type of the Component to destroy</typeparam>
/// <param name="obj"></param>
internal static void RemoveComponent<T>(this GameObject obj) where T : Component
{
var comp = obj.GetComponent<T>();
if (comp != null)
{
UnityEngine.Object.Destroy(comp);
}
}
/// <summary>
/// Sets the color of all SpriteRenderer components in the children of a GameObject.
/// </summary>
+2 -1
View File
@@ -12,7 +12,7 @@ namespace BetterAmongUs.Helpers;
/// <summary>
/// Provides extension methods and utilities for working with PlayerControl instances.
/// </summary>
static class PlayerControlHelper
internal static class PlayerControlHelper
{
/// <summary>
/// Gets the ClientData associated with a player.
@@ -83,6 +83,7 @@ static class PlayerControlHelper
{
return false;
}
return true;
}
-1
View File
@@ -203,7 +203,6 @@ internal static class Utils
}
catch
{
// Intentionally empty - chat failures shouldn't crash the game
}
}
+58 -17
View File
@@ -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<PassiveButton> presetButtons = []; // Outfit preset buttons
private static List<PoolablePlayer> presetPreviews = []; // Outfit previews
private static float cooldown = 0f; // Button click cooldown
private static readonly List<SpriteRenderer> _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<SpriteRenderer> _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<PetBehaviour>(true))
{
pet.gameObject.DestroyObj();
}
playerPreview = outfitPreview;
button.OnClick = new();
button.OnClick.AddListener(callback);
button.DestroyTextTranslators();
var text = button.GetComponentInChildren<TextMeshPro>();
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);
}
}
}
}