Skip to content

Instantly share code, notes, and snippets.

@gabriel-nsiqueira
Last active January 19, 2021 18:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabriel-nsiqueira/827dea0a1cdc2210db6f9a045ec4ce0a to your computer and use it in GitHub Desktop.
Save gabriel-nsiqueira/827dea0a1cdc2210db6f9a045ec4ce0a to your computer and use it in GitHub Desktop.
CooldownButton for AmongUs mod
//In HudManager.Start, Initialize the class
//HudManager.Update, call CooldownButton.HudUpdate
public class CooldownButton
{
public static List<CooldownButton> buttons = new List<CooldownButton>();
public KillButtonManager killButtonManager;
private Color startColorButton = new Color(255, 255, 255);
private Color startColorText = new Color(255, 255, 255);
public Vector2 PositionOffset = Vector2.zero;
public float MaxTimer = 0f;
public float Timer = 0f;
public float EffectDuration = 0f;
public bool isEffectActive;
public bool hasEffectDuration;
public bool enabled = true;
public Category category;
private string ResourceName;
private Action OnClick;
private Action OnEffectEnd;
private HudManager hudManager;
private float pixelsPerUnit;
private bool canUse;
public CooldownButton(Action OnClick, float Cooldown, string ImageEmbededResourcePath, float PixelsPerUnit, Vector2 PositionOffset, Category category, HudManager hudManager, float EffectDuration, Action OnEffectEnd)
{
this.hudManager = hudManager;
this.OnClick = OnClick;
this.OnEffectEnd = OnEffectEnd;
this.PositionOffset = PositionOffset;
this.EffectDuration = EffectDuration;
this.category = category;
pixelsPerUnit = PixelsPerUnit;
MaxTimer = Cooldown;
Timer = MaxTimer;
ResourceName = ImageEmbededResourcePath;
hasEffectDuration = true;
isEffectActive = false;
buttons.Add(this);
Start();
}
public CooldownButton(Action OnClick, float Cooldown, string ImageEmbededResourcePath, float pixelsPerUnit, Vector2 PositionOffset, Category category, HudManager hudManager)
{
this.hudManager = hudManager;
this.OnClick = OnClick;
this.pixelsPerUnit = pixelsPerUnit;
this.PositionOffset = PositionOffset;
this.category = category;
MaxTimer = Cooldown;
Timer = MaxTimer;
ResourceName = ImageEmbededResourcePath;
hasEffectDuration = false;
buttons.Add(this);
Start();
}
private void Start()
{
killButtonManager = UnityEngine.Object.Instantiate(hudManager.KillButton, hudManager.transform);
startColorButton = killButtonManager.renderer.color;
startColorText = killButtonManager.TimerText.Color;
killButtonManager.gameObject.SetActive(true);
killButtonManager.renderer.enabled = true;
Texture2D tex = GUIExtensions.CreateEmptyTexture();
Assembly assembly = Assembly.GetExecutingAssembly();
Stream myStream = assembly.GetManifestResourceStream(ResourceName);
byte[] buttonTexture = Reactor.Extensions.Extensions.ReadFully(myStream);
ImageConversion.LoadImage(tex, buttonTexture, false);
killButtonManager.renderer.sprite = GUIExtensions.CreateSprite(tex);
PassiveButton button = killButtonManager.GetComponent<PassiveButton>();
button.OnClick.RemoveAllListeners();
button.OnClick.AddListener((UnityEngine.Events.UnityAction)listener);
void listener()
{
if (Timer < 0f && canUse)
{
killButtonManager.renderer.color = new Color(1f, 1f, 1f, 0.3f);
if (hasEffectDuration) {
isEffectActive = true;
Timer = EffectDuration;
killButtonManager.TimerText.Color = new Color(0, 255, 0);
}
OnClick();
}
}
}
public bool CanUse()
{
if (PlayerControl.LocalPlayer.Data == null) return false;
switch (category)
{
case Category.Everyone:
{
canUse = true;
break;
}
case Category.OnlyCrewmate:
{
canUse = !PlayerControl.LocalPlayer.Data.isImpostor;
break;
}
case Category.OnlyImpostor:
{
canUse = PlayerControl.LocalPlayer.Data.isImpostor;
break;
}
}
return true;
}
public static void HudUpdate()
{
buttons.RemoveAll(item => item.killButtonManager == null);
for (int i = 0; i<buttons.Count; i++)
{
if(buttons[i].CanUse())
buttons[i].Update();
}
}
private void Update()
{
if (killButtonManager.transform.localPosition.x > 0f)
killButtonManager.transform.localPosition = new Vector3((killButtonManager.transform.localPosition.x + 1.3f) * -1, killButtonManager.transform.localPosition.y, killButtonManager.transform.localPosition.z) + new Vector3(PositionOffset.x, PositionOffset.y);
if (Timer < 0f)
{
killButtonManager.renderer.color = new Color(1f, 1f, 1f, 1f);
if (isEffectActive)
{
killButtonManager.TimerText.Color = startColorText;
Timer = MaxTimer;
isEffectActive = false;
OnEffectEnd();
}
}
else
{
if (canUse)
Timer -= Time.deltaTime;
killButtonManager.renderer.color = new Color(1f, 1f, 1f, 0.3f);
}
killButtonManager.gameObject.SetActive(canUse);
killButtonManager.renderer.enabled = canUse;
if (canUse)
{
killButtonManager.renderer.material.SetFloat("_Desat", 0f);
killButtonManager.SetCoolDown(Timer, MaxTimer);
}
}
internal delegate bool d_LoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
internal static d_LoadImage iCall_LoadImage;
public static bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
{
if (iCall_LoadImage == null)
iCall_LoadImage = IL2CPP.ResolveICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage");
var il2cppArray = (Il2CppStructArray<byte>)data;
return iCall_LoadImage.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
}
public enum Category
{
Everyone,
OnlyCrewmate,
OnlyImpostor
}
}
@gabriel-nsiqueira
Copy link
Author

I fixed a bug which the kill button didnt exist but the class did.

@gabriel-nsiqueira
Copy link
Author

Updated Button Class to work with reactor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment