Search code examples
unity-game-enginegame-developmentlevels

How to make a level selection page and levels act as individual with Unity 2D


I am currently making a 2D puzzle logic game and cannot find any sources on how to build the level homepage to select levels. What i mean by this is a system similar to cut the rope with chapters and levels enter image description here. What are the sources that you can recommend for this and good ones for game development in unity in general?

Thanks!

Have not yet tried to build it myself as i am sure there are sources or faster ways. Also as i am seeing it now, it looks like all levels will be loaded at the same time at the selection of one, but do correct me if i am wrong


Solution

  • Firstly you need to create a prefab for level buttons with LevelButton.cs script below attached on it. What this class does is holding button reference and defining what's going to happen when it's clicked. On button clicked, it's going to load the level by path. Assign a button component to button field.

        public class LevelButton : MonoBehaviour
        {
            public Button button;
            [HideInInspector] public string scenePath;
    
            private void OnButtonClicked()
            {
                button.onClick.RemoveAllListeners();
                SceneManager.LoadScene(scenePath);
            }
    
            public void Initialize(string scenePath)
            {
                this.scenePath = scenePath;
    
                button.onClick.RemoveAllListeners();
                button.onClick.AddListener(OnButtonClicked);
            }
        }
    

    And then you need to create a script which creates LevelButtons for all of your scenes. Check the class below;

        public class LevelSectionHandler : MonoBehaviour
        {
            public LevelButton prefab;
            public Transform levelSectionParentTransform;
            List<Scene> sceneList = new List<Scene>();
    
            private void Awake()
            {
                Initialize();
            }
    
            private void GetAllScenesInBuildSettings()
            {
                Scene tempScene;
                int sceneCount = SceneManager.sceneCountInBuildSettings;
    
                for (int i = 0; i < sceneCount; i++)
                {
                    tempScene = SceneManager.GetSceneByBuildIndex(i);
                    if (tempScene.IsValid())
                    {
                        sceneList.Add(tempScene);
                    }
                }
            }
    
            private void CreateLevelBoard()
            {
                LevelButton tempLevelButton = null;
    
                for (int i = 0; i < sceneList.Count; i++)
                {
                    tempLevelButton = Instantiate(tempLevelButton, levelSectionParentTransform);
                    tempLevelButton.Initialize(sceneList[i].path);
                }
            }
    
            public void Initialize()
            {
                GetAllScenesInBuildSettings();
                CreateLevelBoard();
            }
        }
    

    What this script does is it holds reference of LevelButton prefab and a Transform to place LevelButtons under it. Assign these components to script and then run the game. It's going to create LevelButtons in Awake. You can change when it's going to create them by calling LevelSectionHandler.Initialize().

    You can improve the algorithm. Hope it helps.