fix: 장착 스킬 업데이트 로직 오류 수정

This commit is contained in:
aube.lee
2025-02-11 21:01:15 +09:00
parent fd7627cd3f
commit c719103ee9
3 changed files with 81 additions and 42 deletions

View File

@@ -82,7 +82,6 @@ namespace TON
// 일반 객체는 그대로 JSON 변환 // 일반 객체는 그대로 JSON 변환
json = JsonUtility.ToJson(data, true); json = JsonUtility.ToJson(data, true);
} }
Debug.Log("SaveToFile ::: " + json);
File.WriteAllText(path, json); File.WriteAllText(path, json);
Debug.Log($"파일 저장 성공 ::: {fileName}.json"); Debug.Log($"파일 저장 성공 ::: {fileName}.json");
@@ -95,7 +94,7 @@ namespace TON
} }
/// <summary> Resources.Load 로 읽어온 파일을 persistentDataPath 경로에 저장 </summary> /// <summary> Resources.Load 로 읽어온 파일을 persistentDataPath 경로에 저장 </summary>
public static bool SaveJsonToPersistentData(string fileName) public static void SaveJsonToPersistentData(string fileName)
{ {
if (fileName.EndsWith(".json")) if (fileName.EndsWith(".json"))
{ {
@@ -104,35 +103,36 @@ namespace TON
string persistentPath = GetPersistentPath(fileName); string persistentPath = GetPersistentPath(fileName);
#if UNITY_ANDROID
// 📌 Step 1: persistentDataPath에 파일이 있는지 체크 // 📌 Step 1: persistentDataPath에 파일이 있는지 체크
// Android에서는 파일이 이미 존재하면 덮어쓰지 않도록 함
if (File.Exists(persistentPath)) if (File.Exists(persistentPath))
{ {
Debug.Log($"⚠ {fileName}.json 파일이 이미 존재합니다. 덮어쓰지 않습니다. ({persistentPath})"); Debug.Log($"⚠ {fileName}.json 파일이 이미 존재합니다. 덮어쓰지 않습니다. ({persistentPath})");
return false;
} }
#endif
// 📌 Step 2: Resources에서 JSON 불러오기 // 📌 Step 2: Resources에서 JSON 불러오기
string path = "GameData/" + fileName; // Resources 폴더 내 경로 string path = DATA_PATH + fileName; // Resources 폴더 내 경로
TextAsset jsonFile = Resources.Load<TextAsset>(path); TextAsset jsonFile = Resources.Load<TextAsset>(path);
if (jsonFile != null) if (jsonFile != null)
{ {
File.WriteAllText(persistentPath, jsonFile.text); File.WriteAllText(persistentPath, jsonFile.text);
Debug.Log($"✅ JSON 저장 완료 (처음 저장됨): {persistentPath}"); Debug.Log($"✅ JSON 저장 완료 (처음 저장됨): {persistentPath}");
return true;
} }
else else
{ {
Debug.LogError($"❌ Resources에서 JSON 파일을 찾을 수 없음: {path}"); Debug.LogError($"❌ Resources에서 JSON 파일을 찾을 s수 없음: {path}");
return false;
} }
} }
/// <summary> persistentDataPath 경로의 파일 읽어오기 </summary> /// <summary> persistentDataPath 경로의 파일 읽어오기 </summary>
public static T LoadJsonFromPersistentData<T>(string fileName) public static T LoadJsonFromPersistentData<T>(string fileName)
{ {
string path = GetPersistentPath(fileName); string path = GetPersistentPath(fileName);
Debug.Log($"LoadJsonFromPersistentData : {path}");
if (!File.Exists(path)) if (!File.Exists(path))
{ {
@@ -162,24 +162,37 @@ namespace TON
} }
/// <summary> persistentDataPath 경로의 파일 데이터 업데이트 </summary> /// <summary> persistentDataPath 경로의 파일 데이터 업데이트 </summary>
public static void SaveUpdatedJsonToPersistentData<T>(T updatedData, string fileName) public static bool SaveUpdatedJsonToPersistentData<T>(T updatedData, string fileName)
{ {
string path = GetPersistentPath(fileName); try
string json;
// 리스트인지 확인 후 JSON 변환
if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>))
{ {
Wrapper<T> wrapper = new Wrapper<T> { items = updatedData }; string path = GetPersistentPath(fileName);
json = JsonUtility.ToJson(wrapper, true); string json;
}
else
{
json = JsonUtility.ToJson(updatedData, true);
}
File.WriteAllText(path, json); // 리스트인지 확인 후 JSON 변환
Debug.Log($"✅ JSON 데이터 업데이트 완료: {path}"); if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>))
{
Wrapper<T> wrapper = new Wrapper<T> { items = updatedData };
json = JsonUtility.ToJson(wrapper, true);
}
else
{
json = JsonUtility.ToJson(updatedData, true);
}
Debug.Log($"SaveUpdatedJsonToPersistentData : {json}");
// 파일 저장
File.WriteAllText(path, json);
Debug.Log($"✅ JSON 데이터 업데이트 완료: {path}");
return true; // 저장 성공
}
catch (Exception ex)
{
Debug.LogError($"❌ JSON 저장 실패: {ex.Message}");
return false; // 저장 실패
}
} }

View File

@@ -35,7 +35,8 @@ namespace TON
skillDatas.Clear(); skillDatas.Clear();
} }
skillDatas = JSONLoader.LoadFromResources<List<SkillData>>("skill"); JSONLoader.SaveJsonToPersistentData("skill");
skillDatas = JSONLoader.LoadJsonFromPersistentData<List<SkillData>>("skill");
if (skillDatas == null) if (skillDatas == null)
{ {
@@ -43,6 +44,24 @@ namespace TON
} }
} }
public void UpdateSkillData(string skillId, int slotNumber)
{
foreach (var skill in skillDatas)
{
if (skill.id == skillId)
{
skill.slotNumber = slotNumber;
}
if (skill.slotNumber == slotNumber && skill.id != skillId)
{
skill.slotNumber = 0;
}
}
Assert.IsTrue(JSONLoader.SaveUpdatedJsonToPersistentData(skillDatas, "skill"));
Initalize();
}
public void SetSkillInstances() public void SetSkillInstances()
{ {
skillInstances = new SerializableDictionary<string, SkillBase>(); skillInstances = new SerializableDictionary<string, SkillBase>();
@@ -159,21 +178,6 @@ namespace TON
return result; return result;
} }
public void UpdateSkillData(string skillId, int slotNumber)
{
foreach (var skill in skillDatas)
{
if (skill.id == skillId)
{
skill.slotNumber = slotNumber;
}
if (skill.slotNumber == slotNumber && skill.id != skillId)
{
skill.slotNumber = 0;
}
}
JSONLoader.SaveToFile(skillDatas, "skill");
}
} }
} }

View File

@@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor.PackageManager; using UnityEditor.PackageManager;
using UnityEngine; using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
@@ -61,6 +62,7 @@ namespace TON
// 스킬 버튼을 생성 // 스킬 버튼을 생성
List<SkillBase> activatedSkills = SkillDataManager.Singleton.GetEquippedSkills(); List<SkillBase> activatedSkills = SkillDataManager.Singleton.GetEquippedSkills();
int maxEquipSkillCount = SkillDataManager.Singleton.GetActiveSkillCount();
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
SkillSettingUI_SkillSlot newSkillSlot = Instantiate(skillSlotPrefab, skillSlotGroup); SkillSettingUI_SkillSlot newSkillSlot = Instantiate(skillSlotPrefab, skillSlotGroup);
@@ -72,8 +74,15 @@ namespace TON
} }
else else
{ {
// 복제 됐을때 기본 상태가 잠금 상태 if (i == maxEquipSkillCount - 1)
newSkillSlot.GetComponent<Button>().interactable = false; {
// 만약 스킬을 배치할 수 있는 슬롯이지만 지정된 스킬이 없다면
newSkillSlot.Initalize(null, i);
}
else
{
newSkillSlot.GetComponent<Button>().interactable = false;
}
} }
createSkillSlots.Add(newSkillSlot); createSkillSlots.Add(newSkillSlot);
} }
@@ -81,6 +90,20 @@ namespace TON
private void SetSkillInfoItem() private void SetSkillInfoItem()
{ {
// 이미 기존에 UI가 생성되어 있다면 삭제
if (createSkillInfo.Count > 0)
{
foreach (var button in createSkillInfo)
{
Destroy(button.gameObject);
}
createSkillInfo.Clear();
}
if (uiPrefabList.Count > 0)
{
uiPrefabList.Clear();
}
List<SkillData> skillDatas = SkillDataManager.Singleton.skillDatas; List<SkillData> skillDatas = SkillDataManager.Singleton.skillDatas;
float y = 0; float y = 0;
@@ -140,7 +163,6 @@ namespace TON
public void OnClickSettingButton() public void OnClickSettingButton()
{ {
Debug.Log($"OnClickSettingButton() : {selectedSkillId} , {selectedSlotIndex}");
// 스킬 데이터 업데이트 할때 selectedSlotIndex +1 해서 넘겨줘야함 // 스킬 데이터 업데이트 할때 selectedSlotIndex +1 해서 넘겨줘야함
SkillDataManager.Singleton.UpdateSkillData(selectedSkillId, selectedSlotIndex + 1); SkillDataManager.Singleton.UpdateSkillData(selectedSkillId, selectedSlotIndex + 1);