#nullable enable using System; using TMPro; using UnityEngine; using UnityEngine.UI; namespace UVC.UI.List.Draggable { /// /// UI 요소와 데이터를 연결하는 컴포넌트입니다. /// 데이터 변경 시 UI를 자동으로 업데이트합니다. /// /// 사용 방법: /// 1. 리스트 아이템 프리팹에 이 컴포넌트를 추가합니다. /// 2. Inspector에서 Text와 Image 컴포넌트를 연결합니다. /// 3. BindData 메서드로 데이터를 설정합니다. /// /// 예시: /// var binder = listItem.GetComponent(); /// binder.BindData(new MyItemData { DisplayName = "아이템 1" }); /// /// // 나중에 데이터 가져오기 /// var data = binder.GetData(); /// public class ListItemView : MonoBehaviour { [Header("UI 기본 요소 연결")] [SerializeField] [Tooltip("아이템의 제목을 표시할 TextMeshProUGUI 컴포넌트")] protected TextMeshProUGUI? titleText; [SerializeField] [Tooltip("아이템의 아이콘을 표시할 Image 컴포넌트 (선택사항)")] protected Image? iconImage; // 현재 바인딩된 데이터를 저장 protected ListItemData? boundData; public Action? OnChangeData; /// /// 데이터를 UI에 바인딩합니다 /// /// 바인딩할 데이터 객체 public virtual void BindData(ListItemData data) { boundData = data; UpdateUI(); //Debug.Log($"[ListItemView] 데이터 바인딩 완료 - {gameObject.name}"); } /// /// 바인딩된 데이터를 기반으로 UI를 업데이트합니다 /// protected virtual void UpdateUI() { // IListItemData 인터페이스를 구현한 경우 if (boundData != null) { // 제목 텍스트 업데이트 if (titleText != null) { titleText.text = boundData.DisplayName; } // IHasIcon 인터페이스도 구현한 경우 아이콘 업데이트 if (iconImage != null && boundData.Icon != null) { iconImage.sprite = boundData.Icon; iconImage.gameObject.SetActive(true); } else if (iconImage != null) { // 아이콘이 없으면 숨깁니다 iconImage.gameObject.SetActive(false); } } } /// /// 현재 바인딩된 데이터를 특정 타입으로 가져옵니다 /// /// 가져올 데이터의 타입 /// 바인딩된 데이터, 타입이 맞지 않거나 없으면 null public T? GetData() where T : ListItemData { return boundData as T; } /// /// 바인딩된 데이터가 있는지 확인합니다 /// public bool HasData => boundData != null; /// /// UI를 수동으로 새로고침합니다 /// public void RefreshUI() { if (boundData != null) { UpdateUI(); } } /// /// 바인딩을 해제하고 UI를 초기화합니다 /// public virtual void ClearBinding() { boundData = null; if (titleText != null) { titleText.text = ""; } if (iconImage != null) { iconImage.sprite = null; iconImage.gameObject.SetActive(false); } //Debug.Log($"[ListItemView] 바인딩 해제됨 - {gameObject.name}"); } protected virtual void OnDestroy() { // 바인딩 해제 시 추가 작업이 필요하면 여기에 작성 ClearBinding(); OnChangeData = null; //Debug.Log($"[ListItemView] 컴포넌트 파괴됨 - {gameObject.name}"); } } }