2026-06-01 씬전환시 무기 사라지는 현상 수정

This commit is contained in:
2026-06-01 13:06:31 +09:00
parent 964383c59c
commit 11a6a4f983
7 changed files with 67 additions and 13 deletions

View File

@@ -6,8 +6,16 @@
// PlayerWeaponInventory
// ----------------------------------------------------------------------------
// 플레이어가 보유한 무기 목록 + 현재 장착 무기 관리.
// 씬 전환 시에도 데이터를 유지하기 위해 DontDestroyOnLoad 싱글톤으로 동작.
// PlayerController가 OnWeaponChanged 이벤트를 구독해서 애니메이션/공격 콤보 교체.
//
// 사용법:
// - 어디서든 PlayerWeaponInventory.Instance 또는 EnsureInstance()로 접근.
// - 인스턴스가 없으면 EnsureInstance()가 새 GameObject로 자동 생성 → 첫 씬에
// 수동 배치하지 않아도 됨.
// - Player 프리팹에는 이 컴포넌트를 더 이상 붙이지 않는다 (중복 시엔 자동 폐기되긴 하지만,
// 프리팹과 같은 GameObject가 영속화되는 부작용을 피하려면 제거 권장).
//
// 슬롯 매핑 (외부 코드의 EquipSlot 인덱스):
// -1 → 맨손 (CurrentWeapon == null)
// 0 → 첫 번째로 픽업한 무기
@@ -21,9 +29,40 @@
// ============================================================================
public class PlayerWeaponInventory : MonoBehaviour
{
// ─── 싱글톤 ──────────────────────────────────────────────────────────
public static PlayerWeaponInventory Instance { get; private set; }
// 어디서든 호출하면 인스턴스를 보장한다. 없으면 새 GameObject로 생성.
// (PlayerController.Awake에서 호출해 첫 진입을 보장하는 용도)
public static PlayerWeaponInventory EnsureInstance()
{
if (Instance != null) return Instance;
GameObject go = new GameObject(nameof(PlayerWeaponInventory));
return go.AddComponent<PlayerWeaponInventory>();
// AddComponent가 Awake를 동기 호출 → Instance 세팅 + DontDestroyOnLoad 적용 완료 후 반환.
}
private readonly List<WeaponData> _weapons = new();
private int _currentIndex = -1; // -1 = 맨손
private void Awake()
{
// 중복 생성 방지 — 이미 Instance가 있으면 이 컴포넌트만 폐기.
// (GameObject 전체를 지우지 않는 이유: Player 프리팹 등에 잘못 붙어 있어도 Player가 사라지면 안 되니까.)
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
}
private void OnDestroy()
{
// 앱 종료 등으로 영속 인스턴스가 파괴될 때 정적 참조 정리.
if (Instance == this) Instance = null;
}
// 무기 교체 시 발화. null이면 맨손.
public event Action<WeaponData> OnWeaponChanged;