Learn Technologies

Unity Unlock levels

In this video you will learn how to make Unlock levels system.

Binding buttons

This buttons will be binding on “LoadMission” method in MissionSelect.cs class. This method takes in parameter the mission’s name.

In the “Start” method, we can initialize the buttons icons and set them interactable or not.
The code looks like :

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

public class MissionSelect : MonoBehaviour
{
    public List<GameObject> MissionsImage;
    public List<GameObject> MissionsLockImage;
    public List<Button> MissionsButtons;

    // Use this for initialization
    void Start()
    {
        int lastMission = GameSettings.GetLastMission;

        for (int i = 0; i < MissionsImage.Count; i++)
        {
            if(lastMission >= (i+2))
            {
                MissionsImage[i].SetActive(true);
                MissionsLockImage[i].SetActive(false);
                MissionsButtons[i].interactable = true;
            }
            else
            {
                MissionsImage[i].SetActive(false);
                MissionsLockImage[i].SetActive(true);
                MissionsButtons[i].interactable = false;
            }
        }
    }

    public void LoadMission(string missionName)
    {
        StartCoroutine(LoadMissionAsync(missionName));
    }

    private IEnumerator LoadMissionAsync(string missionName)
    {
        var operation = SceneManager.LoadSceneAsync(missionName);
        while (!operation.isDone)
        {
            float progress = Mathf.Clamp01(operation.progress / .9f);
            yield return null;
        }
    }
}

Storage data

As you can see, in GameSettings.cs” class, I used “PlayerPrefs”. It one way to save data in Unity regardless of the platform is to use the PlayerPrefs class. Stores and accesses player preferences between game sessions. This class functions as a hash table, allowing you to store key-value pairs.

GameSettings.cs

using UnityEngine;
namespace Assets
{
    public class GameSettings: MonoBehaviour
    {
        public static int GetLastMission { get { return PlayerPrefs.GetInt("LastMission", 1); } }

        public static void SetLastMission(int value)
        {
            PlayerPrefs.SetInt("LastMission", value);
        }
    }
}

Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter or GitHub to be notified when I post new content.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x