The question is actually what: 1. I saved the triangle, for example, using Vector3. But the rotation of the triangle was not saved into the array; when all the triangles are restored, they will be restored to the prefab position. How to keep them rotate position? Vector 3 has only 3 variables. If they are in a separate array, then how will they choose the coordinates when restoring? 2. Really everyone does this and there is no way to simplify saving, well, it’s obscurantism to add code and search by tag for each object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
public class SaveGameToM : MonoBehaviour
{
public GameObject Cube1Pref;
public GameObject Triangle1Pref;
[System.Serializable]
public struct myVector3
{
public float x, y, z;
}
[System.Serializable]
public struct myVector4
{
public float j, k, l;
}
[SerializeField] public List<myVector3> ObjectsPositions = new List<myVector3>();
[SerializeField] public List<myVector4> ObjectsPositionsT = new List<myVector4>();
public void Awake()
{
if (!File.Exists(Application.persistentDataPath + "saves"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/saves");
}
}
public void Start()
{
if (File.Exists(Application.persistentDataPath + "/saves/save.sav")& File.Exists(Application.persistentDataPath + "/saves/saveT.sav"))
{
BackMyCubes();
BackMyTriangles();
}
}
public void BackMyCubes()
{
ObjectsPositions = Load();
foreach (var pos in ObjectsPositions)
{
Instantiate(Cube1Pref, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
}
ObjectsPositions.Clear();
}
public void BackMyTriangles()
{//n
ObjectsPositionsT = LoadT();
foreach (var posT in ObjectsPositionsT)//n
{//n
Instantiate(Triangle1Pref, new Vector3(posT.j, posT.k, posT.l), Quaternion.Euler(-90f,0.0f,0.0f));//n
}//n
ObjectsPositionsT.Clear();//n
}//n
public void ManualSaving()
{
if (File.Exists(Application.persistentDataPath + "/saves/save.sav"))
{
File.Delete(Application.persistentDataPath + "/saves/save.sav");
}
foreach (var obj in GameObject.FindGameObjectsWithTag("Cube1"))
{
myVector3 i = new myVector3();
i.x = obj.transform.position.x;
i.y = obj.transform.position.y;
i.z = obj.transform.position.z;
ObjectsPositions.Add(i);
}
Save(ObjectsPositions);
}
public void ManualSavingT()//n
{//n
if (File.Exists(Application.persistentDataPath + "/saves/saveT.sav"))//n
{//n
File.Delete(Application.persistentDataPath + "/saves/saveT.sav");//n
}//n
foreach (var objT in GameObject.FindGameObjectsWithTag("Triangle1"))//n
{//n
myVector4 t = new myVector4();//n
t.j = objT.transform.position.x;//n
t.k = objT.transform.position.y;//n
t.l = objT.transform.position.z;//n
ObjectsPositionsT.Add(t);//n
}//n
Save(ObjectsPositionsT);//n
}//n
public List<myVector3> Load()
{
List<myVector3> objects;
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
{
objects = (List<myVector3>)formatter.Deserialize(stream);
}
return objects;
}
public List<myVector4> LoadT()//n
{//n
List<myVector4> objectsT;//n
BinaryFormatter formatter = new BinaryFormatter();//n
using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/saveT.sav", FileMode.Open))//n
{//n
objectsT = (List<myVector4>)formatter.Deserialize(streamT);//n
}//n
return objectsT;//n
}//n
void Save(List<myVector3> positions)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Create))
{
formatter.Serialize(stream, positions);
stream.Close();
}
positions.Clear();
}
void Save(List<myVector4> positionsT)//n
{//n
BinaryFormatter formatter = new BinaryFormatter();//n
using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/saveT.sav", FileMode.Create))//n
{
formatter.Serialize(streamT, positionsT);//n
streamT.Close();//n
}
positionsT.Clear();//n
}//
}