์”ฌ๋„˜๊ธธ๋•Œ๋ฒ„ํ”„๋ฅผ 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
}
}
}