75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
using WL.Player;
|
||
|
|
|
||
|
|
namespace WL.UI
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 좌측 상단 캐릭터 교체 토글 버튼 (PD #711).
|
||
|
|
/// 탭할 때마다 목록의 다음 캐릭터로 바꾸고, 라벨에 현재 캐릭터 이름을 표시한다.
|
||
|
|
/// 아이콘은 더미(기본 UI 스프라이트 단색)이며 색은 CharacterRoster 에서 읽는다.
|
||
|
|
/// </summary>
|
||
|
|
[RequireComponent(typeof(Button))]
|
||
|
|
public class CharacterToggleButton : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("참조 (비우면 씬에서 찾는다)")]
|
||
|
|
[SerializeField] private PlayerSwitcher switcher;
|
||
|
|
[Tooltip("현재 캐릭터 이름을 표시할 텍스트")]
|
||
|
|
[SerializeField] private Text label;
|
||
|
|
[Tooltip("더미 아이콘 이미지 — 캐릭터마다 색을 바꾼다")]
|
||
|
|
[SerializeField] private Image icon;
|
||
|
|
|
||
|
|
private Button _button;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
_button = GetComponent<Button>();
|
||
|
|
_button.onClick.AddListener(OnClick);
|
||
|
|
Resolve();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
Resolve();
|
||
|
|
if (switcher != null) switcher.Switched += OnSwitched;
|
||
|
|
Refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDisable()
|
||
|
|
{
|
||
|
|
if (switcher != null) switcher.Switched -= OnSwitched;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDestroy()
|
||
|
|
{
|
||
|
|
if (_button != null) _button.onClick.RemoveListener(OnClick);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Resolve()
|
||
|
|
{
|
||
|
|
if (switcher == null) switcher = Object.FindFirstObjectByType<PlayerSwitcher>();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start() { Refresh(); }
|
||
|
|
|
||
|
|
private void OnSwitched(int index) { Refresh(); }
|
||
|
|
|
||
|
|
private void OnClick()
|
||
|
|
{
|
||
|
|
Resolve();
|
||
|
|
if (switcher == null) { Debug.LogWarning("[CharacterToggleButton] PlayerSwitcher 가 씬에 없습니다", this); return; }
|
||
|
|
int idx = switcher.SwitchNext();
|
||
|
|
Refresh();
|
||
|
|
Debug.Log("[CharacterToggleButton] 캐릭터 -> " + (idx + 1) + " (" + switcher.CurrentDisplayName + ")");
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>라벨·아이콘 색을 현재 캐릭터에 맞춘다.</summary>
|
||
|
|
public void Refresh()
|
||
|
|
{
|
||
|
|
if (switcher == null) return;
|
||
|
|
if (label != null) label.text = switcher.CurrentDisplayName;
|
||
|
|
if (icon != null) icon.color = switcher.CurrentIconColor;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|