How to Make a Main Menu in Unity

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

Комментарии • 15

  • @AhmedG-uv5rx
    @AhmedG-uv5rx 20 дней назад +1

    Very useful video tysm!

    • @DammLabs
      @DammLabs  20 дней назад

      I am so glad that you found my video useful! Thanks for watching!

  • @camerontully957
    @camerontully957 26 дней назад +1

    what about when you click on the play button the what can you make a game when you click on the play button do you have to make a game when you push play for a game you are making.

    • @DammLabs
      @DammLabs  26 дней назад

      Can you rephrase this? I would love to help, but I am a little confused with the question.

  • @eshankhan2408
    @eshankhan2408 20 дней назад +1

    Hey Sir, for my UI, instead of scale, I made 3 different buttons for different resolution scales, and 3 different buttons for difficulty as well, how do I code this in visual studio?
    Also I made a seperate 'pause canvas' with a 'save' button' , how can I go about this?

    • @DammLabs
      @DammLabs  20 дней назад +2

      Hello!
      For the resolution buttons, you can make public methods for each that just have a line of code that set the resolution of the game to that set resolution. The method that you want is Screen.SetResolution(). This method looks for three parameters, width, height, and if you want to be in full screen or not. You can just manually put in the numbers for each of your buttons and you can chose whether you want your game to be in full screen or not, but I would recommend that you keep it consistent across the resolution values.
      For the difficulty buttons, you can make public methods for each of those and inside of the methods, you would need to update the thing that changes the difficulty. If you were making a platforming game for example, the difficulty buttons could set your maximum health lower, or if you were making a top down shooter, the buttons could change the amount of enemies that spawn per wave.
      For the pause canvas and save button, you have two choices. The first choice is easier and can be used for smaller, simpler games. But, if you have a large game that you are planning to publish or sell, you should create a save and load system. The save and load system is much more complicated and I will link another video to try to explain it.
      ruclips.net/video/aUi9aijvpgs/видео.html
      Be warned though, as this is much more complicated than the videos I make.
      If you want to be able to display the canvas to the player when they push escape or another key, you can get an if statement in your player controller to check if they pushed a certain button. If they did, you can stop the time while the menu is open by typing Time.timeScale = 0 and set the canvas game object to true by pauseCanvas.SetActive(true). Once the player has pushed a button to get out of the menu, set time back to full by typing Time.timeScale = 1 and set the pause canvas to be false by typing pauseCavnas.SetActive(false).
      The choice that i will be talking about is "PlayerPrefs". This is something that stores data about the player. To use this, you would type PlayerPrefs dot something. The something would be either Get/Set and then a type of input, such as float, int, or string. Set sets the data and will set it with a key, which is a string. So if I wanted to set the player's health, I would write: PlayerPrefs.SetFloat("PlayerHealth", 100f) or something like that. Get gets the data and only looks for a key. You can then set that data into a variable and later change another value. For example, if you wanted to get the player's health after they saved, you would write something like: playerHealth = PlayerPrefs.GetFloat("PlayerHealth")
      You could set all your PlayerPrefs in your save button and when you load your game back up, you can set all your variables to the already set PlayerPrefs.
      I realize that this was a lot, and if you need any help or want a video on this, just comment here! Happy coding!

    • @eshankhan2408
      @eshankhan2408 19 дней назад

      Hey thanks man, you know for the pause canvas, I made another gameobject and wrote this:
      void Update()
      {
      // Check if the Escape key is pressed
      if (Input.GetKeyDown(KeyCode.Escape))
      {
      if (isPaused)
      {
      ResumeGame(); // Exit pause menu
      }
      else
      {
      PauseGame(); // Enter pause menu
      }
      }
      }
      void PauseGame()
      {
      Time.timeScale = 0; // Stop the game
      pauseCanvas.SetActive(true); // Show pause menu
      isPaused = true;
      }
      void ResumeGame()
      {
      Time.timeScale = 1; // Resume the game
      pauseCanvas.SetActive(false); // Hide pause menu
      isPaused = false;
      would this be correct way of implementing the pause? i saw other videos and they made new scripts entirely for the pause

    • @DammLabs
      @DammLabs  19 дней назад +1

      @eshankhan2408 I think that's a good way to do it, and probably the way I would do it too. Great work on making this yourself!

    • @eshankhan2408
      @eshankhan2408 16 дней назад +1

      @DammLabs hey dude, I wanted to test my final UI but every time I run it, the system immediately shows the pause canvas only instead of the main menu, ive been trying to fix this for hours now, its because during void update it sets it to pause canvas but how do I fix this logic error. I wanted the game to check for the escape key only when the actual game is being run which is after they press play :(
      public class MenuManager : MonoBehaviour
      {
      public GameObject MainMenuCanvas;
      public GameObject SettingsCanvas;
      public GameObject PauseCanvas;
      private bool isPaused = false;
      void Start()
      {

      }

      void Update()
      {
      if (Input.GetKeyUp(KeyCode.Escape))
      {
      if (isPaused)
      {
      OnResumeButtonClicked();
      }
      }
      else
      {
      OnPauseButtonClicked();
      }
      }
      public void OnPlayButtonClicked()
      {
      //stuff
      }

      public void OnPauseButtonClicked()
      {
      PauseCanvas.SetActive(true);
      MainMenuCanvas.SetActive(false);
      SettingsCanvas.SetActive(false);
      Time.timeScale = 0;
      isPaused = true;
      }
      public void OnBackButtonClicked()
      {
      MainMenuCanvas.SetActive(true);
      SettingsCanvas.SetActive(false);
      PauseCanvas.SetActive(false);
      isPaused= false;
      }
      public void OnQuitButtonClicked()
      {
      Application.Quit();
      }
      public void OnSettingsButtonClicked()
      {
      SettingsCanvas.SetActive(true);
      MainMenuCanvas.SetActive(false);
      PauseCanvas.SetActive(false);
      Time.timeScale = 0;

      }
      public void OnResumeButtonClicked()
      {
      MainMenuCanvas.SetActive(true);
      SettingsCanvas.SetActive(false);
      PauseCanvas.SetActive(false);
      Time.timeScale = 1;
      isPaused = false;
      }
      this is a big ask but i appreciate any help 🙏

    • @DammLabs
      @DammLabs  15 дней назад +1

      @eshankhan2408 To fix the problem of the pause canvas showing at the start of the game, you have to change your Update function a bit. You have it checking for input, and if the input is not true, calling the OnPauseButtonClicked method. You want to move the else statement that has calling the OnPauseButtonClicked method in it to inside of the input if statement. The code should look like this:
      void Update()
      {
      if (Input.GetKeyUp(KeyCode.Escape))
      {
      if (isPaused)
      {
      OnResumeButtonClicked();
      }
      else
      {
      OnPauseButtonClicked();
      }
      }
      }
      If this doesn't make sense, I would recommend you put it into a code visualizer of something like that to see the lines of code being executed.
      To make it so that you can only have the pause canvas open if the game is running, create another bool that is true when the game is running. Then you can put that bool into your if statement. The code would look something like this:
      bool GameIsRunning = false;
      void Update()
      {
      if (Input.GetKeyUp(KeyCode.Escape) && GameIsRunning ==true)
      {
      if (isPaused)
      {
      OnResumeButtonClicked();
      }
      else
      {
      OnPauseButtonClicked();
      }
      }
      }
      I hope this helps and if you need more help on anything else, just ask!

  • @camerontully957
    @camerontully957 24 дня назад +1

    what a tutorial about making a game in unity about when you push the play button then a game will start are you going to do a tutorial on that.

    • @DammLabs
      @DammLabs  24 дня назад

      I will definitely make a tutorial on that. Thank you for the comment and stay tuned for next Wednesday for the video!