public class Saving : MonoBehaviour
{
GameObject[] Towers;
GameObject[] Enemies;
int i, b, Level;
string coordX, coordY, coordZ, TowLevel, Enemyhp, wpindex, Enemyfullhp;
public GameObject TOWER, Dannie, MULTI, ENEMYE;
float X, Y, Z, EnemyHP, EnemyFullHP;
void Awake()
{
//TOWERS
i = 0;
b = PlayerPrefs.GetInt("CountTower")+1;
while (i != b)
{
GameObject proj = Instantiate(TOWER);
coordX = "Coordx" + i.ToString();
coordY = "Coordy" + i.ToString();
coordZ = "Coordz" + i.ToString();
TowLevel = "TowerLevel" + i.ToString();
X = PlayerPrefs.GetFloat(coordX);
Y = PlayerPrefs.GetFloat(coordY);
Z = PlayerPrefs.GetFloat(coordZ);
proj.transform.position = new Vector3(X,Y,Z);
proj.GetComponent<DiceLVL>().LVL = PlayerPrefs.GetInt(TowLevel);
i++;
}
//TOWER
//ENEMIES
i = 0;
b = PlayerPrefs.GetInt("CountEnemy")+1;
while (i != b)
{
coordX = "CoordENx" + i.ToString();
coordY = "CoordENy" + i.ToString();
coordZ = "CoordENz" + i.ToString();
Enemyhp = "EnemyHP" + i.ToString();
Enemyfullhp = "EnemyFullHP" + i.ToString();
wpindex = "WayPoint" + i.ToString();
X = PlayerPrefs.GetFloat(coordX);
Y = PlayerPrefs.GetFloat(coordY);
Z = PlayerPrefs.GetFloat(coordZ);
GameObject proj = Instantiate(ENEMYE);
proj.transform.position = new Vector3(X, Y, Z);
proj.GetComponent<Enemy>().HP = PlayerPrefs.GetFloat(Enemyhp);
proj.GetComponent<Enemy>().fullhp = PlayerPrefs.GetFloat(Enemyfullhp);
proj.GetComponent<Enemy>().waypointindex = PlayerPrefs.GetInt(wpindex);
i++;
Debug.Log("Энеми спавн");
}
//ENEMIES
}
void OnApplicationQuit()
{
//TOWERS
Towers = GameObject.FindGameObjectsWithTag("Tower");
i = 0;
b = Towers.Length;
while (i != b)
{
PlayerPrefs.SetInt("CountTower", i);
coordX = "Coordx" + i.ToString();
coordY = "Coordy" + i.ToString();
coordZ = "Coordz" + i.ToString();
TowLevel = "TowerLevel" + i.ToString();
Level = Towers[i].GetComponent<DiceLVL>().LVL;
PlayerPrefs.SetFloat(coordX , Towers[i].transform.position.x);
PlayerPrefs.SetFloat(coordY, Towers[i].transform.position.y);
PlayerPrefs.SetFloat(coordZ, Towers[i].transform.position.z);
PlayerPrefs.SetInt(TowLevel, Level);
i++;
}
//TOWERS
//ENEMIES
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
i = 0;
b = Enemies.Length;
while (i != b)
{
PlayerPrefs.SetInt("CountEnemy", i);
coordX = "CoordENx" + i.ToString();
coordY = "CoordENy" + i.ToString();
coordZ = "CoordENz" + i.ToString();
EnemyHP = Enemies[i].GetComponent<Enemy>().HP;
Enemyhp = "EnemyHP" + i.ToString();
EnemyFullHP = Enemies[i].GetComponent<Enemy>().fullhp;
Enemyfullhp = "EnemyFullHP" + i.ToString();
wpindex = "WayPoint" + i.ToString();
PlayerPrefs.SetInt(wpindex, Enemies[i].GetComponent<Enemy>().waypointindex - 1);
PlayerPrefs.SetFloat(coordX, Enemies[i].transform.position.x);
PlayerPrefs.SetFloat(coordY, Enemies[i].transform.position.y);
PlayerPrefs.SetFloat(coordZ, Enemies[i].transform.position.z);
PlayerPrefs.SetFloat(Enemyhp, EnemyHP);
PlayerPrefs.SetFloat(Enemyfullhp, EnemyFullHP);
i++;
Debug.Log("Выход");
}
//ENEMIES
}
}
Для сохранения данных самое простое это:
PlayerPrefs
И не используйте тяжеловестные операции в методе OnApplicationQuit,