178 lines
5.9 KiB
C#
178 lines
5.9 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace EnglewoodLAB.UI
|
|
{
|
|
/// <summary>
|
|
/// 클릭, 호버, 포인터 종료와 같은 포인터 이벤트에 대한 응답으로 대상 <see cref="Image"/> 구성 요소의 색상 변경을 처리합니다.
|
|
/// 이 동작은 연결된 <see cref="Button"/>의 상호 작용 가능 상태를 고려합니다.
|
|
/// </summary>
|
|
public class CustomImageColorChangeBehaviour : MonoBehaviour, IPointerDownHandler, IPointerUpHandler,
|
|
IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
public Color originalColor = Color.white; // 기본 컬러
|
|
public Color hoverColor = Color.gray; // 마우스 오버 시 컬러
|
|
public Color clickColor = Color.gray; // 클릭 시 컬러
|
|
public Color disabledColor = Color.gray; // 비활성화 시 컬러
|
|
|
|
[SerializeField]
|
|
private Image[] targetImages; // 컬러를 변경할 이미지 컴포넌트
|
|
[SerializeField]
|
|
private TextMeshProUGUI[] targetTexts; // 컬러를 변경할 텍스트 컴포넌트
|
|
[SerializeField]
|
|
private Selectable button; // 버튼 컴포넌트
|
|
|
|
private bool _isSelected = false;
|
|
|
|
private void Awake()
|
|
{
|
|
if (targetImages == null || targetImages.Length == 0)
|
|
{
|
|
var img = GetComponent<Image>();
|
|
if(img != null) targetImages = new Image[] { img };
|
|
}
|
|
|
|
if (targetTexts == null || targetTexts.Length == 0)
|
|
{
|
|
var txt = GetComponentInChildren<TextMeshProUGUI>();
|
|
if(txt != null) targetTexts = new TextMeshProUGUI[] { txt };
|
|
}
|
|
|
|
if (button == null)
|
|
{
|
|
button = GetComponent<Button>();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (button != null)
|
|
{
|
|
button.transition = Selectable.Transition.None; // 버튼의 전환 효과를 비활성화
|
|
|
|
Color color = button.enabled && button.interactable ? originalColor : disabledColor;
|
|
if (button is Toggle toggle)
|
|
{
|
|
color = toggle.isOn ? clickColor : originalColor; // 토글 상태에 따라 컬러 변경
|
|
|
|
toggle.onValueChanged.AddListener(onValueChangedToggle);
|
|
}
|
|
|
|
ChangeColor(color);
|
|
}
|
|
else
|
|
{
|
|
ChangeColor(enabled ? originalColor : disabledColor);
|
|
}
|
|
}
|
|
|
|
private void ChangeColor(Color color)
|
|
{
|
|
if (targetImages != null)
|
|
{
|
|
foreach (var img in targetImages)
|
|
{
|
|
if(img != null) img.color = color;
|
|
}
|
|
}
|
|
if (targetTexts != null)
|
|
{
|
|
foreach (var txt in targetTexts)
|
|
{
|
|
if(txt != null) txt.color = color;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void onValueChangedToggle(bool isOn)
|
|
{
|
|
ChangeColor(isOn ? clickColor : originalColor); // 토글 상태에 따라 컬러 변경
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (button != null && button is Toggle toggle)
|
|
{
|
|
toggle.onValueChanged.RemoveListener(onValueChangedToggle);
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if ((button != null && button.interactable) || (button == null && enabled))
|
|
{
|
|
ChangeColor(clickColor); // 클릭 시 컬러 변경
|
|
}
|
|
else
|
|
{
|
|
ChangeColor(disabledColor); // 비활성화 상태일 때 컬러 변경
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if ((button != null && button.interactable) || (button == null && enabled))
|
|
{
|
|
if (_isSelected) return;
|
|
if (button is Toggle toggle && toggle.isOn) return;
|
|
ChangeColor(hoverColor); // 마우스 오버 시 컬러 변경
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if ((button != null && button.interactable) || (button == null && enabled))
|
|
{
|
|
if (_isSelected)
|
|
{
|
|
ChangeColor(clickColor);
|
|
return;
|
|
}
|
|
if (button != null && button is Toggle toggle)
|
|
{
|
|
ChangeColor(toggle.isOn ? clickColor : originalColor); // 토글 상태에 따라 컬러 변경
|
|
}
|
|
else
|
|
{
|
|
ChangeColor(originalColor); // 마우스가 벗어날 때 원래 컬러로 변경
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ChangeColor(disabledColor); // 비활성화 상태일 때 컬러 변경
|
|
}
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if ((button != null && button.interactable) || (button == null && enabled))
|
|
{
|
|
if (_isSelected)
|
|
{
|
|
ChangeColor(clickColor);
|
|
return;
|
|
}
|
|
ChangeColor(hoverColor); // 클릭 후 마우스 오버 컬러로 변경
|
|
}
|
|
else
|
|
{
|
|
ChangeColor(disabledColor); // 비활성화 상태일 때 컬러 변경
|
|
}
|
|
}
|
|
|
|
public void SetSelected(bool isSelected)
|
|
{
|
|
_isSelected = isSelected;
|
|
if ((button != null && !button.interactable) || (button == null && !enabled))
|
|
{
|
|
ChangeColor(disabledColor);
|
|
return;
|
|
}
|
|
|
|
ChangeColor(isSelected ? clickColor : originalColor);
|
|
}
|
|
}
|
|
}
|