0.들어가기에 앞서
제출까지 하루가 남았다. 이제는 왠만한 기능이 다 들어가고 빌드를 해야만 한다.
데이터 테이블의 제작 자체가 늦게 되기도 했고, 심지어 팀장님이 주말에 병합 작업도 안하셔서 아예 작업을 못했던 탓에 뒤늦게 테이블 두 개 분량 정도 작업을 더 했다.
하지만 이렇게 작업할 때가 아닌데, 이제는 진짜 게임 빌드 파일이 나와야하는 시점에서 이렇게 작업하는 것은 중단하고, 우선 데이터 자체는 엑셀 표를 보고 바로 찍어낸 다음 테스트부터 진행하기로 했다.
특히나 상자 아이템 확률 표의 경우, 다시금 연동하는데 필요한 코드를 설정할 필요가 있었다.
박스 아이템 중 A 아이템 확률에 대한 표는 아래와 같고, 이에 대한 데이터를 담기 위한 스크립터블 오브젝트를 아래와 같이 구성했다.
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New BoxSetUpA", menuName = "Assets/Create New BoxSetUpA")]
public class BoxSetUpASO : ScriptableObject
{
public List<BoxSetup> BoxSetUpA = new List<BoxSetup>();
}
[System.Serializable]
public class BoxSetup
{
public ItemSO ItemName;
public float ProbType1;
public float ProbType2;
public float ProbType3;
public int ItemCount;
}
해당 데이터를 변환하는 작업은 아래와 같은 방식으로 진행했다.
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TableBoxSetUpATableToSO : MonoBehaviour
{
[MenuItem("Utilities/Generate BoxSetUpA")]
public static void GenerateBoxSetUpA()
{
var boxSetUpATable = TableManager.Instance.GetTable<BoxSetUpTable>(TableType.BoxSetup);
List<BoxSetUpData> boxSetUpA = boxSetUpATable.TBoxSetup;
BoxSetUpASO boxSetUp = ScriptableObject.CreateInstance<BoxSetUpASO>();
foreach (BoxSetUpData s in boxSetUpA)
{
string boxSetUpName = s.ItemID;
ItemSO item = AssetDatabase.LoadAssetAtPath<ItemSO>($"Assets/08.ScriptableObjects/Item/{boxSetUpName}.asset");
float.TryParse(s.ProbType1, out float ProbType1);
float.TryParse(s.ProbType2, out float ProbType2);
float.TryParse(s.ProbType3, out float ProbType3);
int.TryParse(s.ItemCount,out int Count);
boxSetUp.BoxSetUpA.Add(new BoxSetup { ItemName = item, ProbType1 = 0.01f * ProbType1, ProbType2 = 0.01f * ProbType2, ProbType3 = 0.01f * ProbType3, ItemCount = Count });
}
AssetDatabase.CreateAsset(boxSetUp, $"Assets/08.ScriptableObjects/BoxSetUpA/BoxSetUpA.asset");
AssetDatabase.SaveAssets();
}
}
이와 같은 방식으로 스크립터블 오브젝트를 생성하면, 아래와 같이 만들어진다.
이를 확률표에 반영하면, 아래와 같이 반영할 수 있다.
/// <summary>
/// Select Item A.
/// Item A is definitely collecitible, and the number of A item is 4.
/// </summary>
private void ItemASelect()
{
if (_weightedRandomA.GetList() == null) return;
for (int i = 0; i < 4; i++)
{
_data.AddItemToBoxSlot(_weightedRandomA.GetRandomItem());
}
}
private void ItemAInit(BoxSetUpASO boxSetUpASO, BoxTier tier)
{
switch (tier)
{
case BoxTier.Tier1:
for(int i = 0; i < boxSetUpASO.BoxSetUpA.Count; i++)
{
ItemSO item = boxSetUpASO.BoxSetUpA[i].ItemName;
int inputNum = (int)(boxSetUpASO.BoxSetUpA[i].ProbType1 * 1000);
_weightedRandomA.Add(item, inputNum);
}
break;
case BoxTier.Tier2:
for (int i = 0; i < boxSetUpASO.BoxSetUpA.Count; i++)
{
ItemSO item = boxSetUpASO.BoxSetUpA[i].ItemName;
int inputNum = (int)(boxSetUpASO.BoxSetUpA[i].ProbType2 * 1000);
_weightedRandomA.Add(item, inputNum);
}
break;
case BoxTier.Tier3:
for (int i = 0; i < boxSetUpASO.BoxSetUpA.Count; i++)
{
ItemSO item = boxSetUpASO.BoxSetUpA[i].ItemName;
int inputNum = (int)(boxSetUpASO.BoxSetUpA[i].ProbType3 * 1000);
_weightedRandomA.Add(item, inputNum);
}
break;
default:
break;
}
}
이렇게 최종으로 나오게 되었지만, 처음 만들었을 때에는 실수를 했었다.
using Unity.Editor 에 해당하는 AssetData를 트래킹하는 방식을 해당 확률표에 사용했었는데, 이와 같이 작성하니 빌드가 안 되는 문제점이 생겼다.
using Unity.Editor에 해당하는 기능은 유니티 에디터에서만 적용되고 빌드 시에는 정상적으로 적용되지 않는다. 빌드 시에는 해당 내용과 관련된 컴포넌트가 포함되지 않도록 유의하도록 하자.
아이템 무게 관련 오류가 계속해서 발생했다. 특히 상자에서 아이템을 빼낼 경우 무게가 두 배 가량 더 증가하고 아이템을 버렸을 때에는 무게가 반밖에 버려지지 않아 무한정 무게가 늘어나는 문제가 발생했다.
이 부분에 대해서는 InventoryManager를 면밀히 살펴 무게가 증감되는 과정을 디버깅해야 했다. 되도록 이곳에서 외적으로 무게를 조절하는 일은 없도록 하며, 무게가 중첩되어 누적되는 부분을 잡아내어 해당 문제를 해결했다.
하지만 한 가지 문제가 더 있으며, 그건 무게가 float형이다 보니 소수점이 완전히 떨어지지 않아서 0.9999와 같이 찍히는 것을 확인했다.
해당 문제를 해결할 방법을 고민해봐야 할 것 같다.