Saving randomly generated data into XML file

Hello there!

It’s my first thread here and sorry, if I seem a noob - cause I am one - but I need this for a project I’m making, and yes, it’s a little more advance than I should be doing, but partially it’s already there, so I want to finish it.

I’ve got this (code below) little set of cubes that I’m generating into a random map, a 10x10 grid. The thing is, I’ve seen a tutorial somewhere on generating random cubes and I customized it (mainly to parent to another object on the scene that I use as a rotation controller, to rotate by arrows on the keyboard), it’s working great, I assigned the generation to a button on my scene, so when I click it - new map arrives.

Point is, whenever I reload the scene, map disappears (which is logical, I have no saving script or anything like that) and generating creates a new one. I want my map - the set of 10x10 cubes - to be saved to XML after generating and I want Unity to check for existing XML file - if it’s there, it loads, if it’s not, a new map is generated. That will also make maps customable by users and XML is a nice extension to work with when you’re a user. But I have no darn idea how to make Unity save the information about position, rotation and type of an object (cause there’re now 4 different types of cubes, there will be like a 100, so…yeah). Right now I have an array of 4 objects I drag&dropped into Unity, and as you can see in the script, it generates clones of them and arranges them in a grid. I kinda know, that I have to use XML serialization and deserialization for saving and loading data, but I don’t know how to make those clones save, I mean how to write the script so that it knows “Hey, I’ve got generated clones of array content here, let’s save data of those!”. Also all XML serialization tutorials I’ve found are like saving simple strings and floats, while I need a little bit more complicated data to be stored.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class RandomCubeGenerator : MonoBehaviour
{
    public GameObject[] itemsToPickFrom;
    public int gridX;
    public int gridZ;
    public float gridSpacingOffset = 1f;
    public Vector3 gridOrigin = Vector3.zero;

    public void SpawnOnClick()
    {
        SpawnGrid();
    }

    public void SpawnGrid()
    {
        for (int x = 0; x < gridX; x++)
        {
            for (int z = 0; z < gridZ; z++)
            {
                Vector3 spawnPosition = new Vector3(x * gridSpacingOffset, 0, z * gridSpacingOffset) + gridOrigin;
                PickAndSpawn(spawnPosition, Quaternion.identity);
            }
        }
    }

    public void PickAndSpawn(Vector3 positionToSpawn, Quaternion rotationToSpawn)
    {
        int randomIndex = Random.Range(0, itemsToPickFrom.Length);
        GameObject clone = Instantiate(itemsToPickFrom[randomIndex], positionToSpawn, rotationToSpawn);
        clone.transform.parent = GameObject.Find("kontrolerMapy").transform;
       
    }
}

If anyone could help me it’d be great, I’ve been trying to solve this one for a while but I’m still too much of a newbie to make it work 100% like I need it too. And please, don’t tell me “stop making this, you’re not advanced enough to make stuff like this”, I’ve already put myself in this s…, so I need to finish this, as I need that for a project that needs this feature. Besides I want to understand and learn how this thing works and how to fix it up for my needs.

https://wiki.unity3d.com/index.php/Saving_and_Loading_Data:_XmlSerializer

//to check if file exists
If (File.Exists(fileName))
{
    //LoadXMLFile
}

And to save Vector3 for instance: just save x, y and z.
If you want to save rotation save eulerAngles

Thank you, I’ll try that out!