2026-05-20 무기슬롯
This commit is contained in:
61
Assets/02_Scripts/UI/WeaponSlot.cs
Normal file
61
Assets/02_Scripts/UI/WeaponSlot.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
// ============================================================================
|
||||
// WeaponSlot
|
||||
// ----------------------------------------------------------------------------
|
||||
// 무기 인벤토리 UI의 슬롯 한 칸. 데이터는 들고 있지 않고 표시만 한다.
|
||||
// WeaponInventory가 슬롯을 생성/갱신할 때 SetWeapon / SetSelected를 호출한다.
|
||||
// ============================================================================
|
||||
public class WeaponSlot : MonoBehaviour
|
||||
{
|
||||
[Header("Ref")]
|
||||
[SerializeField] public Image Weapon_Img; // 무기 아이콘
|
||||
[SerializeField] public TMP_Text Weapon_Name; // 무기 이름
|
||||
[SerializeField] private GameObject _slotPosObj; // 장착 중일 때 위치를 밀어줄 대상
|
||||
[SerializeField] private float _selectedOffsetX = -100f; // 장착 시 _slotPosObj를 x로 미는 양 (음수 = 왼쪽)
|
||||
|
||||
// _slotPosObj의 기준 위치. 최초 SetSelected 호출 시 한 번만 캡처해서 이동 누적 방지.
|
||||
private RectTransform _slotPosRect;
|
||||
private Vector2 _slotPosBase;
|
||||
private bool _slotPosCaptured;
|
||||
|
||||
// 슬롯에 표시할 무기를 채운다. null이면 빈 슬롯으로 표현.
|
||||
public void SetWeapon(WeaponData weapon)
|
||||
{
|
||||
if (Weapon_Img != null)
|
||||
{
|
||||
Weapon_Img.sprite = weapon != null ? weapon.PickupSprite : null;
|
||||
// 스프라이트가 없으면 아이콘 자체를 숨겨 빈 칸이 깨져 보이지 않게 한다.
|
||||
Weapon_Img.enabled = weapon != null && weapon.PickupSprite != null;
|
||||
}
|
||||
|
||||
if (Weapon_Name != null)
|
||||
Weapon_Name.text = weapon != null ? weapon.DisplayName : string.Empty;
|
||||
}
|
||||
|
||||
// 현재 장착 중인 무기 슬롯이면 _slotPosObj를 x로 _selectedOffsetX만큼 밀어준다.
|
||||
// 해제되면 기준 위치로 복귀. 기준 위치는 처음 한 번만 캡처해서 이동이 누적되지 않게 한다.
|
||||
public void SetSelected(bool selected)
|
||||
{
|
||||
CaptureSlotPosBase();
|
||||
if (_slotPosRect == null) return;
|
||||
|
||||
Vector2 pos = _slotPosBase;
|
||||
pos.x += selected ? _selectedOffsetX : 0f;
|
||||
_slotPosRect.anchoredPosition = pos;
|
||||
}
|
||||
|
||||
// _slotPosObj의 기준 anchoredPosition을 최초 1회 저장 (오프셋 적용 전 값).
|
||||
private void CaptureSlotPosBase()
|
||||
{
|
||||
if (_slotPosCaptured) return;
|
||||
_slotPosCaptured = true;
|
||||
|
||||
if (_slotPosObj != null)
|
||||
_slotPosRect = _slotPosObj.transform as RectTransform;
|
||||
if (_slotPosRect != null)
|
||||
_slotPosBase = _slotPosRect.anchoredPosition;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user