Skip to content

How to save game levels progress?

Hasan Bayat edited this page Sep 16, 2017 · 2 revisions

Here we want to learn how to save our game levels progress.

It is too easy using Save Game Pro and it's powerful features.

Getting Started

First of all we need a collection of our completed and unlocked levels like the below:

public List<bool> completedLevels;
public List<bool> unlockedLevels;

Then we need to manage them somewhere of our game logic. so we create a new script called LevelManager and open it.

Now we want to initialize our completed and unlocked levels variables via MonoBehaviour awake method:

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

public class LevelManager : MonoBehaviour
{

	protected List<bool> m_CompletedLevels;
	protected List<bool> m_UnlockedLevels;

	public List<bool> completedLevels {
		get {
			return m_CompletedLevels;
		}
	}

	public List<bool> unlockedLevels {
		get {
			return m_UnlockedLevels;
		}
	}

	void Awake ()
	{
		m_CompletedLevels = new List<bool> ();
		m_UnlockedLevels = new List<bool> ();
	}

}

Now we have completed the first step. let us go forward and learn how to save and load the levels progress.

How to save and load levels?

Now we want to learn how to save our completed and unlocked levels progress using Save Game Pro.

It is too easy if you take a look at Save Game Pro API documentation and see it's powerful features.

We want to easily save data on quit and load it back on start as below:

Open up LevelManager script and put the below content to it:

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

using SaveGamePro;

public class LevelManager : MonoBehaviour
{

	protected List<bool> m_CompletedLevels;
	protected List<bool> m_UnlockedLevels;

	public List<bool> completedLevels {
		get {
			return m_CompletedLevels;
		}
	}

	public List<bool> unlockedLevels {
		get {
			return m_UnlockedLevels;
		}
	}

	void Awake ()
	{
		m_CompletedLevels = new List<bool> ();
		m_UnlockedLevels = new List<bool> ();
	}

	void Start ()
	{
		Load ();
	}

	void OnApplicationQuit ()
	{
		Save ();
	}

	void Save ()
	{
		SaveGame.Save<bool> ( m_CompletedLevels, "completedLevels" );
		SaveGame.Save<bool> ( m_UnlockedLevels, "unlockedLevels" );
	}

	void Load ()
	{
		m_CompletedLevels = SaveGame.LoadList<bool> ( "completedLevels" );
		m_UnlockedLevels = SaveGame.LoadList<bool> ( "unlockedLevels" );
	}

}

How to unlock a new level?

It is the easiest part of level management, just unlock next level when current level completed.

See the below to learn how to do it:

Open LevelManager script and put the below content to it to add unlock functionally:

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

using SaveGamePro;

public class LevelManager : MonoBehaviour
{

	protected List<bool> m_CompletedLevels;
	protected List<bool> m_UnlockedLevels;

	public List<bool> completedLevels {
		get {
			return m_CompletedLevels;
		}
	}

	public List<bool> unlockedLevels {
		get {
			return m_UnlockedLevels;
		}
	}

	void Awake ()
	{
		m_CompletedLevels = new List<bool> ();
		m_UnlockedLevels = new List<bool> ();
	}

	void Start ()
	{
		Load ();
	}

	void OnApplicationQuit ()
	{
		Save ();
	}

	void Save ()
	{
		SaveGame.Save<bool> ( m_CompletedLevels, "completedLevels" );
		SaveGame.Save<bool> ( m_UnlockedLevels, "unlockedLevels" );
	}

	void Load ()
	{
		m_CompletedLevels = SaveGame.LoadList<bool> ( "completedLevels" );
		m_UnlockedLevels = SaveGame.LoadList<bool> ( "unlockedLevels" );
	}

	public void SetLevelComplete ( int levelID, bool complete )
	{
		if ( m_CompletedLevels.Count > levelID )
		{
			m_CompletedLevels [ levelID ] = complete;
		}
		else
		{
			m_CompletedLevels.Add ( complete );
		}
	}

	public void SetLevelUnlock ( int levelID, bool unlock )
	{
		if ( m_UnlockedLevels.Count > levelID )
		{
			m_UnlockedLevels [ levelID ] = unlock;
		}
		else
		{
			m_UnlockedLevels.Add ( unlock );
		}
	}

	public bool IsCompleted ( int levelID )
	{
		if ( m_CompletedLevels.Count > levelID )
		{
			return m_CompletedLevels [ levelID ];
		}
		return false;
	}

	public bool IsUnlocked ( int levelID )
	{
		if ( m_UnlockedLevels.Count > levelID )
		{
			return m_UnlockedLevels [ levelID ];
		}
		return false;
	}

	public void OnLevelCompleted ( int levelID )
	{
		SetLevelComplete ( levelID, true );
		SetLevelUnlock ( levelID + 1, true );
	}

}

How to display levels?

For displaying levels on the ui, you should create a class for your levels button, for example LevelButton, and then you should check for level is completed and level is unlocked and do you functionally there and change your button image or color.

See this example to learn How to do it:

Create a LevelButton script and open it and put the below content to it:

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

public class LevelButton : MonoBehaviour
{

	/// <summary>
	/// The level ID.
	/// You should set it manually by placing levels on screen or creating an automated script.
	/// </summary>
	public int levelID = 0;

	public Button button;

	void Start ()
	{
		if ( LevelManager.Instance.IsCompleted ( levelID ) )
		{
			button.image.color = Color.green;
		}
		else if ( LevelManager.Instance.IsUnlocked ( levelID ) )
		{
			button.image.color = Color.grey;
		}
		else
		{
			button.image.color = Color.black;
		}
	}

}

Now we need to make our LevelManager class singleton:

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

using SaveGamePro;

public class LevelManager : MonoBehaviour
{

	public static LevelManager Instance {
		get {
			return m_Instance;
		}
	}

	private static LevelManager m_Instance;

	protected List<bool> m_CompletedLevels;
	protected List<bool> m_UnlockedLevels;

	public List<bool> completedLevels {
		get {
			return m_CompletedLevels;
		}
	}

	public List<bool> unlockedLevels {
		get {
			return m_UnlockedLevels;
		}
	}

	void Awake ()
	{
		if ( m_Instance == null )
		{
			m_Instance = this;
		}
		else
		{
			Destroy ( gameObject );
		}
		m_CompletedLevels = new List<bool> ();
		m_UnlockedLevels = new List<bool> ();
	}

	void Start ()
	{
		Load ();
	}

	void OnApplicationQuit ()
	{
		Save ();
	}

	void Save ()
	{
		SaveGame.Save<bool> ( m_CompletedLevels, "completedLevels" );
		SaveGame.Save<bool> ( m_UnlockedLevels, "unlockedLevels" );
	}

	void Load ()
	{
		m_CompletedLevels = SaveGame.LoadList<bool> ( "completedLevels" );
		m_UnlockedLevels = SaveGame.LoadList<bool> ( "unlockedLevels" );
	}

	public void SetLevelComplete ( int levelID, bool complete )
	{
		if ( m_CompletedLevels.Count > levelID )
		{
			m_CompletedLevels [ levelID ] = complete;
		}
		else
		{
			m_CompletedLevels.Add ( complete );
		}
	}

	public void SetLevelUnlock ( int levelID, bool unlock )
	{
		if ( m_UnlockedLevels.Count > levelID )
		{
			m_UnlockedLevels [ levelID ] = unlock;
		}
		else
		{
			m_UnlockedLevels.Add ( unlock );
		}
	}

	public bool IsCompleted ( int levelID )
	{
		if ( m_CompletedLevels.Count > levelID )
		{
			return m_CompletedLevels [ levelID ];
		}
		return false;
	}

	public bool IsUnlocked ( int levelID )
	{
		if ( m_UnlockedLevels.Count > levelID )
		{
			return m_UnlockedLevels [ levelID ];
		}
		return false;
	}

	public void OnLevelCompleted ( int levelID )
	{
		SetLevelComplete ( levelID, true );
		SetLevelUnlock ( levelID + 1, true );
	}

}

All done. now we have a complete Level Management with UI button.

Clone this wiki locally