MonoBehaviour를 상속받는 클래스 사용 시 생성자 대신 초기화 메소드를 사용하여 외부에서 해당 메서드를 호출하는 방식 필요
Character
초기화 메소드 Init()
public class Character : MonoBehaviour
{
// 캐릭터 기본 정보
public string Title { get; private set; } = "뉴비";
public string Name { get; private set; } = "Lee";
public int Level { get; private set; } = 10;
public int MaxExp { get; private set; } = 12;
public int CurExp { get; private set; } = 3;
public string Gold { get; private set; } = "20,000";
public int Atk { get; private set; } = 35;
public int Def { get; private set; } = 40;
public int Hp { get; private set; } = 10;
public int Cri { get; private set; } = 25;
public Image CurExpBar; // 경험치 바 UI
public List<Item> Inventory { get; private set; } // 인벤토리 아이템 리스트
private List<Item> equippedItems = new List<Item>(); // 장착 중인 아이템 리스트
[SerializeField] UISlot uiSlot; // UI 슬롯 참조 (현재 미사용)
// 캐릭터 초기화 함수
public void Init(string title, string name, int level, int maxExp, int curExp, string gold, int atk, int def, int hp, int cri, List<Item> inventory)
{
Title = title;
Name = name;
Level = level;
MaxExp = maxExp;
CurExp = curExp;
Gold = gold;
Atk = atk;
Def = def;
Hp = hp;
Cri = cri;
Inventory = new List<Item>(inventory);
// UI 업데이트
UIManager.Instance.AlwaysUI.SetCharacterInfo(this);
UIManager.Instance.UIStatus.SetStatus(this);
CurExpBar.fillAmount = (float)CurExp / MaxExp;
}
}
GameManager
Init()을 호출하는 메소드인 SetData()를 Start()에서 호출
public class GameManager : MonoBehaviour
{
// 싱글톤 인스턴스
public static GameManager Instance;
// 캐릭터 정보 접근용 프로퍼티
public Character character { get; private set; }
// 플레이어 프리팹
public GameObject player;
private void Awake()
{
// 싱글톤 설정
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
// 프레임 고정 및 초기 데이터 설정
Application.targetFrameRate = 60;
SetData();
}
// 초기 데이터 설정
void SetData()
{
// 아이템 리스트 생성
List<Item> items = new List<Item>
{
new Item(
"검", // 이름
LoadIcon("Sword"), // 아이콘
new Dictionary<StatType, int> // 능력치
{
{ StatType.Atk, 10 }
}),
new Item(
"갑옷",
LoadIcon("Armor"),
new Dictionary<StatType, int>
{
{ StatType.Def, 10 },
{ StatType.Hp, 50 }
}),
new Item(
"활",
LoadIcon("Bow"),
new Dictionary<StatType, int>
{
{ StatType.Atk, 5 },
{ StatType.Cri, 5 },
})
};
// 플레이어 오브젝트 생성 및 캐릭터 초기화
GameObject playerObj = Instantiate(player);
character = playerObj.GetComponent<Character>();
// 캐릭터 정보 초기화
character.Init("뉴비", "Lee", 10, 12, 3, "20,000", 35, 40, 100, 25, items);
}
'게임 개발 공부 기록' 카테고리의 다른 글
46일차 - RPGProject 팀 프로젝트 2 (0) | 2025.03.28 |
---|---|
45일차 - RPGProject 팀 프로젝트 1 (0) | 2025.03.27 |
43일차 - 최적화 1 (0) | 2025.03.25 |
42일차 - UI 복습 2 (0) | 2025.03.24 |
41일차 - 컴퓨터 구조와 메모리 / 최적화 (1) | 2025.03.21 |