164 lines
5.8 KiB
C#
164 lines
5.8 KiB
C#
using DG.Tweening;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class ButtonActivateColorChanger : MonoBehaviour
|
|
{
|
|
public List<Button> clickableButton = new List<Button>();
|
|
public List<Button> unclickableButton = new List<Button>();
|
|
|
|
public Color32 normalTextColor = new Color32(236, 236, 236, 255);
|
|
public Color32 normalBGColor = new Color32(255, 255, 255, 0);
|
|
public Color32 highLightTextColor = new Color32(41, 41, 41, 230);
|
|
public Color32 highLightBGColor = new Color32(255, 255, 255, 103);
|
|
public Color32 pressedTextColor = new Color32(0, 0, 0, 240);
|
|
public Color32 pressedBGColor = new Color32(255, 255, 255, 160);
|
|
public Color32 disableTextColor = new Color32(180, 180, b: 180, 80);
|
|
public Color32 disableBGColor = new Color32(255, 255, b: 255, 0);
|
|
public float colorChangeSpeed = 0.1f;
|
|
|
|
public bool clickable = true;
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
foreach (Button btn in clickableButton)
|
|
{
|
|
InitializeEventTriggers(btn, true);
|
|
}
|
|
|
|
foreach (Button btn in unclickableButton)
|
|
{
|
|
InitializeEventTriggers(btn, false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes the event triggers for a button to handle its interactive behavior.
|
|
/// </summary>
|
|
/// <param name="btn">The button object to initialize.</param>
|
|
/// <param name="isClickable">Indicates whether the button is clickable.</param>
|
|
private void InitializeEventTriggers(Button btn, bool isClickable)
|
|
{
|
|
EventTrigger eventTrigger = btn.GetComponent<EventTrigger>();
|
|
if (eventTrigger == null)
|
|
{
|
|
eventTrigger = btn.gameObject.AddComponent<EventTrigger>();
|
|
}
|
|
|
|
AddEventTrigger(eventTrigger, EventTriggerType.PointerEnter, (eventData) => OnPointerEnter(btn));
|
|
AddEventTrigger(eventTrigger, EventTriggerType.PointerExit, (eventData) => OnPointerExit(btn));
|
|
if (isClickable)
|
|
{
|
|
AddEventTrigger(eventTrigger, EventTriggerType.PointerDown, (eventData) => OnPointerDown(btn));
|
|
AddEventTrigger(eventTrigger, EventTriggerType.PointerUp, (eventData) => OnPointerUp(btn));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an event trigger entry to an event trigger.
|
|
/// </summary>
|
|
/// <param name="trigger">The event trigger object.</param>
|
|
/// <param name="type">The event trigger type.</param>
|
|
/// <param name="action">The event handler method to execute.</param>
|
|
private void AddEventTrigger(EventTrigger trigger, EventTriggerType type, UnityEngine.Events.UnityAction<BaseEventData> action)
|
|
{
|
|
EventTrigger.Entry entry = new EventTrigger.Entry();
|
|
entry.eventID = type;
|
|
entry.callback.AddListener(action);
|
|
trigger.triggers.Add(entry);
|
|
}
|
|
|
|
private void OnPointerEnter(Button btn)
|
|
{
|
|
if (btn.interactable)
|
|
{
|
|
btn.image.DOColor(highLightBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(highLightTextColor, colorChangeSpeed);
|
|
}
|
|
}
|
|
|
|
private void OnPointerExit(Button btn)
|
|
{
|
|
if (btn.interactable)
|
|
{
|
|
btn.image.DOColor(normalBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(normalTextColor, colorChangeSpeed);
|
|
}
|
|
}
|
|
|
|
private void OnPointerDown(Button btn)
|
|
{
|
|
if (btn.interactable)
|
|
{
|
|
btn.image.DOColor(pressedBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(pressedTextColor, colorChangeSpeed);
|
|
}
|
|
}
|
|
|
|
private void OnPointerUp(Button btn)
|
|
{
|
|
if (btn.interactable)
|
|
{
|
|
btn.image.DOColor(highLightBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(normalTextColor, colorChangeSpeed);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the interactable state of a button and modifies its color based on the state.
|
|
/// </summary>
|
|
/// <param name="btn">The button object to change the state and color of.</param>
|
|
/// <param name="changeTo">Indicates the interactable state to change to.</param>
|
|
public void ChangeInteractableColor(Button btn, bool changeTo)
|
|
{
|
|
btn.interactable = changeTo;
|
|
if (changeTo)
|
|
{
|
|
btn.image.DOColor(normalBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(normalTextColor, colorChangeSpeed);
|
|
}
|
|
else
|
|
{
|
|
btn.image.DOColor(disableBGColor, colorChangeSpeed);
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().DOColor(disableTextColor, colorChangeSpeed);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a button to the button list and initializes the button's event triggers based on its clickability.
|
|
/// </summary>
|
|
/// <param name="btn">The button object to add to the button list.</param>
|
|
/// <param name="clickable">Indicates whether the button is clickable.</param>
|
|
public void AddButtonToColorChangerButtonList(Button btn, bool clickable)
|
|
{
|
|
if (clickable)
|
|
{
|
|
clickableButton.Add(btn);
|
|
InitializeEventTriggers(btn, clickable);
|
|
}
|
|
else
|
|
{
|
|
unclickableButton.Add(btn);
|
|
InitializeEventTriggers(btn, clickable);
|
|
}
|
|
}
|
|
|
|
public void InitializeAllButtonColor()
|
|
{
|
|
foreach (Button btn in clickableButton)
|
|
{
|
|
btn.image.color = normalBGColor;
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().color = normalTextColor;
|
|
}
|
|
foreach (Button btn in unclickableButton)
|
|
{
|
|
btn.image.color = disableBGColor;
|
|
btn.GetComponentInChildren<TextMeshProUGUI>().color = disableTextColor;
|
|
}
|
|
}
|
|
}
|