using System; using TMPro; using UnityEngine; using UnityEngine.UI; namespace UVC.Factory.Alarm { /// /// 여러 개의 알람이 한 곳에 발생했을 때 이를 묶어서 표시하는 '클러스터' 아이콘을 관리하는 클래스입니다. /// 아이콘에는 총 알람 개수가 표시되며, 클릭 시 개별 알람을 펼쳐보는 기능을 트리거합니다. /// public class AlarmClusterIcon : MonoBehaviour { [Tooltip("클릭 가능한 UI 버튼입니다. 클러스터를 나타냅니다.")] [SerializeField] private Button button; // 알람 개수를 표시할 TextMeshPro 컴포넌트입니다. private TextMeshProUGUI buttonText; /// /// 클러스터 아이콘에 표시될 알람 개수 텍스트를 가져오거나 설정하는 프로퍼티입니다. /// public string AlarmCount { get => buttonText != null ? buttonText.text : string.Empty; set { if (buttonText != null) { buttonText.text = value; } } } /// /// 이 클러스터 아이콘이 클릭되었을 때 호출될 이벤트를 정의합니다. /// 상위 관리자인 AlarmIconManager가 이 이벤트를 구독하여 클러스터를 펼치는 동작을 수행합니다. /// public Action OnClickHandler; /// /// 컴포넌트가 생성될 때 가장 먼저 호출되는 Unity 생명주기 메서드입니다. /// private void Awake() { // 자식 오브젝트에서 TextMeshProUGUI 컴포넌트를 찾아 할당합니다. buttonText = GetComponentInChildren(); if (buttonText == null) { Debug.LogWarning("Text component not found in children.", this); } } /// /// 첫 번째 프레임 업데이트 전에 호출되는 Unity 생명주기 메서드입니다. /// private void Start() { if (button != null) { // 버튼의 onClick 이벤트에 HandleClick 메서드를 리스너로 등록합니다. button.onClick.AddListener(HandleClick); } else { Debug.LogWarning("Button is not assigned."); } } /// /// 버튼 클릭 이벤트가 발생했을 때 실행되는 메서드입니다. /// private void HandleClick() { // OnClickHandler 이벤트에 등록된 메서드가 있다면 호출합니다. OnClickHandler?.Invoke(); } /// /// 이 컴포넌트가 파괴될 때 호출되는 Unity 생명주기 메서드입니다. /// private void OnDestroy() { if (button != null) { // Start에서 등록했던 클릭 이벤트 리스너를 제거하여 메모리 누수를 방지합니다. button.onClick.RemoveListener(HandleClick); } else { Debug.LogWarning("Button is not assigned."); } // 이벤트 핸들러 참조를 정리합니다. OnClickHandler = null; } } }