0

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

2 Answers2

0

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.

msaiduraz
  • 151
  • 5
0

You should definitely try to build it yourself first, as that would be the best way to learn, and would ensure it performs the tasks which will likely be specific to your game properly.

Since you are a beginner, I will say the easiest way to get this done is to create the buttons in the editor, then create a level selection controller script.

In the controller script create a button list, add the buttons, create a level list, and the levels, and create an int.

If the player is on level 0, int = 0, level 1, int = 1 e.t.c. When player opens the level selection have a for loop run through the buttons. E.g:

public List<Button> myButtons = new List<Button>();
public List<GameObject> myLevels = new List<GameObject>();
public int levelAt;

public void Start()
{
   for(int i = 0; i < levelAt; i++)
   {
      myButtons[i].interactable = true;
      myButtons[i].onClick.AddListener(delegate{chooseALevel(i); });
   }
}

public void chooseALevel(int level)
{
    //This function is called to start the level
    myLevels.SetActive(level);
}

In the for loop we get the button, set it to interactable if the player is on, or has passed that level. And then we add an onClick event to the button, assigning the function with the i variable being used as the level count.

(If the player is at level 1, then button 1 will be set as interactable, and level 1 will be assigned to it's onClick).

Am at work, so I can't test the code. But it should work perfectly fine, and hopefully gives you an idea of one of the ways you can accomplish your goal.

Happy Coding!

DylanT 25
  • 233
  • 1
  • 7