์ฌ๋๊ธธ๋๋ฒํ๋ฅผ JSON์ผ๋ก ์ ์ฅํ๊ณ ์๋ก์ด ์ฌ์์ ๋ก๋๋ฅผ ์ฌ๋ฌ๋ฒ ํ๋ฉด ์ด๋ฐ ์๋ฌ๊ฐ ๋จ๊ณ ์์์ง์
๋๋ค.
ArgumentException: JSON parse error: The document root must not follow by other values.
๊ทธ๋์ ๋ณด๋๊น
์๋ ์ด๋ ๊ฒ ์ ์ฅ๋์ด์ผํ๋ JSONํ์ผ์ด
{
"hp": 90,
"speed": {
"duration": 8.0,
"target": {
"instanceID": 25786
},
"buffCheck": true,
"effectCheck": true,
"IsPause": false,
"healHp": 10.0,
"debuffduration": 15.0,
"BuffProgressTime": 0.0,
"DebuffProgressTime": 0.0,
"speedIncrease": 6.0,
"speedDecrease": 2.0,
"JumpUp": 3.0,
"DashIncrease": 5.0,
"DashDecrease": 1.5,
"color": {
"r": 0.0,
"g": 1.0,
"b": 0.0,
"a": 1.0
}
}
}
์๋ํ์์ผ๋ก JSON์ด ๋ค์ "}"๊ฐ ์ฌ๋ฌ๊ฐ ๋ฌ๋ฆฌ๊ณ ์ ์ฅ๋์ด ์๋ฌ๋๋์ง ์๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
{
"hp": 90,
"speed": {
"duration": 8.0,
"target": {
"instanceID": 27562
},
"buffCheck": true,
"effectCheck": true,
"IsPause": false,
"healHp": 10.0,
"debuffduration": 15.0,
"BuffProgressTime": 0.0,
"DebuffProgressTime": 0.0,
"speedIncrease": 6.0,
"speedDecrease": 2.0,
"JumpUp": 3.0,
"DashIncrease": 5.0,
"DashDecrease": 1.5,
"color": {
"r": 0.0,
"g": 1.0,
"b": 0.0,
"a": 1.0
}
}
} }
}
}}
์ ์ฅํด๋์ค
[System.Serializable]
public class Playerdata
{
[Header("ํผ")]
public int hp = 90;
public SpeedBuff speed;
}
SpeedBuff ํด๋์ค
[System.Serializable]
public class SpeedBuff : StatusEffect
{
public float speedIncrease;
public float speedDecrease;
public float JumpUp = 3f;
public float DashIncrease = 5f;
public float DashDecrease = 1.5f;
public Color color = Color.green;
public SpeedBuff(float speedIncrease,float speedDecrease, GameObject target, float duration, float debuffduration, bool buffCheck) : base(target, duration, debuffduration, buffCheck)
{
this.speedIncrease = speedIncrease;
this.speedDecrease = speedDecrease;
this.buffCheck = buffCheck;
this.target = target;
}
public override void ApplyBuff()
{
// ๏ฟฝ๏ฟฝ๏ฟฝ+๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝลด
// ๏ฟฝ๏ฟฝ๏ฟฝรท๏ฟฝ Debug.Log๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝฯฟ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ, ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ+๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝัพ๏ฟฝ ๏ฟฝ๏ฟฝ
//target.GetComponent<HeroKnight>().m_speed += speedIncrease;
/*if (target.TryGetComponent(out HeroKnight hinge))
{
hinge.m_speed += speedIncrease;
}*/
if(target.TryGetComponent(out Player player))
{
player.m_speed += speedIncrease;
}
Debug.Log(target + "Speed buff applied: +" + speedIncrease + " Speed, +" + JumpUp + " JumpForce");
}
public override void ApplyDebuff()
{
//target.GetComponent<HeroKnight>().m_speed -= speedIncrease;
if (target.TryGetComponent(out Player player))
{
player.m_speed -= speedIncrease;
}
Debug.Log(target + "Speed debuff applied: -" + speedDecrease + " Speed");
}
public override void RemoveBuff()
{
//target.GetComponent<HeroKnight>().m_speed -= speedIncrease;
// ๏ฟฝ๏ฟฝ๏ฟฝรท๏ฟฝ Debug.Log๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝฯฟ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ, ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ+๏ฟฝ๏ฟฝ๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ+๏ฟฝ ๏ฟฝ๏ฟฝ
if (target.TryGetComponent(out Player hinge))
{
hinge.m_speed -= speedIncrease;
}
Debug.Log(target + "Speed buff removed: -" + speedIncrease + " Speed, -" + JumpUp + " JumpForce");
}
public override void RemoveDebuff()
{
//target.GetComponent<HeroKnight>().m_speed += speedIncrease;
if (target.TryGetComponent(out Player player))
{
player.m_speed += speedIncrease;
}
Debug.Log(target + "Speed debuff removed: +" + speedDecrease + " Speed");
}
}
speedBuffํด๋์ค์ ๋ถ๋ชจ ํด๋์ค
[System.Serializable]
public abstract class StatusEffect
{
public float duration;
public GameObject target;
public bool buffCheck;
public bool effectCheck = false;
public bool IsPause = false;
public float healHp = 10f;
public float debuffduration;
public float BuffProgressTime = 0f;//๋ฒํ ์งํ๋ ์๊ฐ ์ค์
public float DebuffProgressTime = 0f;//๋๋ฒํ ์งํ๋ ์๊ฐ ์ค์
public StatusEffect(GameObject target, float duration, float debuffduration, bool buffCheck)
{
this.duration = duration;
this.target = target;
this.buffCheck = buffCheck;
this.debuffduration = debuffduration;
}
public abstract void ApplyBuff();
public abstract void ApplyDebuff();
public abstract void RemoveBuff();
public abstract void RemoveDebuff();
}
ํด๋น ์ ์ฅํ๊ณ ๋ก๋ํ๋ ํจ์๋ค
void Awake()
{
LoadPlayerDataFromJson();
instance = this;
}
public Playerdata playerdata;
[ContextMenu("Save Data")]
void SavePlayerDataToJson()
{
FileStream stream = new FileStream(Application.dataPath + "/PlayerData.json", FileMode.OpenOrCreate);
string jsonData = JsonUtility.ToJson(playerdata, true);
byte[] data = Encoding.UTF8.GetBytes(jsonData);
stream.Write(data, 0, data.Length);
stream.Close();
}
[ContextMenu("Load Data")]
void LoadPlayerDataFromJson()
{
try
{
FileStream stream = new FileStream(Application.dataPath + "/PlayerData.json", FileMode.Open);
byte[] data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
stream.Close();
string jsonData = Encoding.UTF8.GetString(data);
JsonUtility.FromJsonOverwrite(jsonData,playerdata);
}
catch (FileNotFoundException)
{
SavePlayerDataToJson();
}
}
[ContextMenu("Delete Save File")]
void DeleteJson()
{
if (System.IO.File.Exists(Application.dataPath + "/PlayerData.json"))
{
try
{
System.IO.File.Delete(Application.dataPath + "/PlayerData.json");
}
catch (System.IO.IOException e)
{
// handle exception
}
}
}
์ ์ฅํ๋๋ฐ๋ ๋ฌธ์ ์์ด๋ณด์. ์ ์ฅํ ๋ Debug Log ์ฐ์์ ๋ ์ ๋๋ก ์ฐํ๋์ง ํ์ธํ๋ ๊ฒ ๋จผ์ ์ผ ๊ฒ ๊ฐ๊ณ . ๊ทธ๋ฅ ์ถ์ธกํ์ ๋ ์ง๊ธ Byte ๋ก ์ ์ฅํ๊ณ , Load ํ ๋ File Stream ์ผ๋ก ๋ค ๊ฐ์ ธ์ค๋๋ฐ, ์ฌ๊ธฐ์ ์ ์ฅํ๊ฑฐ๋ Load ํ ๋ ํ์ผ stream ๊ด๋ จํด์ ์ถ๊ฐ๋ก ์ ํ๊ฑฐ๋, EOF ๋ ๋ค๋ฅธ ํ์ผ ๊ด๋ จ๋ ๊ฒ๊น์ง ๋ค ๋ค๊ณ ์์ ๋ฌธ์ ์๊ธฐ๋ ๊ฒ ๊ฐ์
ํ๋ฒ ์ธ์ด๋ธ ๋ก๋๋ฅผ 5๋ฒ์ ๋ ํ ์๊ฐ์ ์๋ฌ๋๊ณ ๋๋ฒ๊ทธ๋ก ์ ์ฅํ ๋ JSON๊ฒ์ฌํ์๋ "}"๋๋ฌธ์ ์ด์ํ๊ฒ ์ ์ฅ ๋๋๊ฒ์ ๋ฐ๊ฒฌํ์์ต๋๋ค. ์ ๊ธฐํ๊ฒ๋ 4๋ฒ์ธ์ด๋ธ ํ์๋ "}"๊ฐ 4๊ฐ ๋์ค๋๋ผ๊ณ ์..
์ ์ฐฉ๊ฐํ์ต๋๋ค ์ฃ์กํด์.. ์ธ์ด๋ธ ํ์ผ ์ ์ฅํ ๋ stream๋๋ฌธ์ ์๋ฌ๋๋๊ฒ๊ฐ์์.
๋ฐ์ดํฐ๋ญ์ค ์ฒดํฌํด๋ณด์ฌ? json ์ ์ถ๋ ฅ ํ ๋ ์์์ค๋ธ๋ญ์ค ์ต์ ์ ์ด ์๋ ์ ์ค์ ๋ก ์ ์ฅ๋๋ ๊ธธ์ด๋ณด๋ค 1 ๊ธธ์ด์ ์ค๋ฒํ๋ก๋ ๊ธธ์ด ์ฑ์ด๋ค๊ณ ๋ง์ง๋ง ๋ฌธ์๊ฐ ๋ฐ๋ณต ๋๋๊ฑฐ ์ผ ์ ์์
๊ธ์์ ์ธ๊ธฐ๋ก ํด๋ดค๋๋ฐ ์๋ฌ๋ฌ์๋ ๋ฐ์ดํฐ ๋ญ์ค๋ 589 ๋ค๋ฅธ๊ฑธ๋ก ๊ฒ์ฌํ์๋ 589๋ก ๋์์.
์๋ฌ์๋๊ณ ์ผ๋ฐ์ ์ผ๋๋ 605 ๊ฒ์ฌํ์๋๋ 605๋ก๋์ต๋๋ค
ํ .. ์ฝ๋๊ฐ ์งง์์ ๋ญ ์๋ฌ๋ ๋งํ ๋ถ๋ถ์ ๋ชจ๋ฅด๊ฒ ๋๋ฐ.. ํน์ json๋ง ๊ทธ๋ฐ ํ์์ด ์ผ์ด๋๋ฉด ํด๋น json์ ์๋ณธ (์ฒ์ ๋ก๋ํ ๋)์ json๋ฌธ๋ฒ์ด ํ๋ฆฐ๊ฑด ์๋์ง ํ์ธํด๋ณด์
๊ธฐ์กด๊บผ ์ญ์ ํ๊ณ ์๋ก ์์ฑํ์๋ ํ์ผ์ JSON๋ฌธ๋ฒ์ ๋ฌธ์ ์๋ค๊ณ ๋น๋๋ค.
ํน์ streamํ ๋ float๋ณ์๊ฐ ์์๋ก ๋ณํ๋๋ฐ ๊ทธ๋ฐ๊ฑฐ์ ์ํฅ์ด ์๋์?
์คํธ๋ฆผ ์์ฒด๊ฐ ๊ธ๋ฅ์ฒ๋ผ ์ฐ์์ ์ผ๋ก ํ์ผ์ ์ฝ์ด์ค๋ ํ์์ผ๋ก ๋์ํ๋ฏ๋ก ์์ฐ์ค๋ฌ์ด ํ์์ผ๋ฏ
์ ๋ํฐ JsonUtility ์ฐ์ง ๋ง๊ณ Newtonsoft Json ์ค์นํด์ ํด๋ณด์
JsonUtility์ ์ฌ์ฉํด์ ์ง๋ ฌํํ๊ฑธ JSON ๋ฌธ๋ฒ๊ฒ์ฌ๊ธฐ๋ก ํ์ธํ์๋ ๋ฌธ์ ์๋๋ฐ ํ์ผ๋ก ์ ์ฅํ ๋ ๋ฌธ์ ์๊ธฐ๋๊ฒ๊ฐ์
JsonUtility๋ ์๋ง ๋์ ๋๋ฆฌ ์ง๋ ฌํ ์ง์ ์ ํ ํ ๋ฐ ์ฅ๊ธฐ์ ์ผ๋ก ๋ดํผ Json์ฐ๋๊ฒ ์ข์ ๊ฑฐ์
Newtossoft json ์ฐ์ธ์ JsonUtility ๋ฌธ์ ๋ง์
์๊ฐ๋ณด๋ค ์ฝ๋ ๊ณ ์น๊ธฐ๋ ์ฌ์ฐ๋๊น ์๋์ ์น๊ณ ๋ฐ๊ฟ์ ์จ๋ด์ ๋ด์๊ฐ์ ๋จ๋ฐฉ์ ํด๊ฒฐ๋ ๋ฏ