简体   繁体   中英

Saving and loading instances of an object in Unity

I'm trying to make a program in Unity that consists of a user interface where I can create teams with several stats (health, mana, and exp), and manage the stats for each team. The team management itself seems to be working, but I'm struggling to create a save system.

New teams are created by instantiating a prefab object of the class Team , which contains all the info on how a team works. With the help of a friend, I made the next save function, that adds the variables I want to keep from all the objects of the class Team to a list I save in a file via json:

public void Save()
    {
        List<Team> teams = getTeams();

        StreamWriter sw = new StreamWriter("save.json");
        sw.WriteLine(JsonConvert.SerializeObject(teams, new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        }));
        sw.Close();
    }

I'd like now to make a load function that sets the program to the same status it was before closing it, with the same teams and the same values each team had, but when reopening the program it starts with only the prefab team, and I don't know how to use the variables saved for each team in the list to create new instances with the same values the old teams had.

I hope the explanation is good enough for you to understand, and thank you in advance.

Save the stats of the team, then re-assign them when you load in the instances.


public class Team : MonoBehaviour {
    // ...
    public TeamStats Stats { get; private set; }

    public void Initalize(TeamStats stats) {
        Stats = stats;
        // Do something with the new stats...
    }
    // ...
}
// ...

[System.Serializable]
public class TeamStats {
    public string Name { get; set; }
    public int TheStats { get; set; }
    // ...
}

Then save TeamStats into the Json file.

When the game is loaded, fetch all TeamStats from the Json File, then iterate through the list, creating a new Team prefab and assigning it the respective TeamStat , like so:

[SerializeField]
private Team teamPrefab;

public void Save() {
    // We save the team stats instead
    List<TeamStats> teamStats = GetTeamStats();

    StreamWriter sw = new StreamWriter("save.json");
    sw.WriteLine(JsonConvert.SerializeObject(teamStats, new JsonSerializerSettings {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    }));
    sw.Close();
}

public void Load() {
    // Some function to load team stats from JSON
    List<TeamStats> teamStats = LoadTeamStatsFromJson();

    foreach (var teamStat in teamStats) {
        // Create a new prefab and assign it with the respective stats.
        Team newTeam = Instantiate(teamPrefab);
        newTeam.Initalize(teamStat);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM