• We just launched and are currently in beta. Join us as we build and grow the community.

Creating Main Menu – Creating Scenes, Controllers, Main Menu Canvas, and Slicing Sprites

rose89543

Ranked Climber
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
97
Likes
150
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Operating System

Android

Creating Scenes

In the Unity environment you will notice an Untitled object in the Hierarchy area. That is the name of the scenes by default, to change it just simply press ctrl + s and save it as Main Menu.
6_1.png

Creating Controllers

Now that we have the main menu scene we will now create Controllers for the game. The Controllers are game objects that handle some methods that be needed in the game later. To do that just click GameObject tab, then a drop down will show up. Then in the sub menu click Create Empty, a GameObject will appear in the Hierarchy.
7_1.png


Before working on the GameObject make sure to reset its transform to avoid conflict in the future.
8_1.png

Rename your GameObject as Game Controller, then go to the Scripts directory creates a new folder called Game Controllers. Right click the mouse button and select create then choose C# script.
9_1.png

Save it as GameController. The Game Controller handles all the save data of our game.
Note: To avoid script error make sure the name of your script has no space, because C# is white space sensitive. And make sure to save all Controllers inside the Game Controllers folder to avoid misplaced.

Open you’re newly created Script; you notice that there is already a block code in the script. By default the unity engine already created that part to make the developer at ease.
  1. using

    System.Collections

    ;
  2. using

    System.Collections.Generic

    ;
  3. using

    UnityEngine

    ;

  4. public

    class

    GameController :

    MonoBehaviour {

  5. // Use this for initialization
  6. void

    Start (

    )

    {

  7. }

  8. // Update is called once per frame
  9. void

    Update (

    )

    {

  10. }
  11. }

Now that everything is ready, we will now do our coding thing. First import these important modules
  1. using

    System

    ;
  2. using

    System.IO

    ;
  3. using

    System.Runtime.Serialization.Formatters.Binary

    ;

Write these variables inside the GameController class:
  1. public

    static

    GameController instance;

  2. public

    bool

    isMusicOn;
  3. public

    bool

    isGameStartedFirstTime;
  4. public

    bool

    [

    ]

    levels;
  5. public

    int

    [

    ]

    highscore;
  6. public

    int

    score;
  7. public

    int

    currentLevel;

  8. private

    GameData data;

Then write these blocks of code inside the GameController class.
  1. void

    Awake(

    )

    {
  2. CreateInstance (

    )

    ;
  3. }

  4. // Use this for initialization
  5. void

    Start (

    )

    {
  6. InitializeGameVariables (

    )

    ;
  7. }

  8. void

    CreateInstance(

    )

    {
  9. if

    (

    instance !=

    null

    )

    {
  10. Destroy (

    gameObject)

    ;
  11. }

    else

    {
  12. instance =

    this

    ;
  13. DontDestroyOnLoad (

    gameObject)

    ;
  14. }
  15. }

  16. void

    InitializeGameVariables(

    )

    {
  17. Load (

    )

    ;


  18. if

    (

    data !=

    null

    )

    {
  19. isGameStartedFirstTime =

    data.

    GetIsGameStartedFirstTime

    (

    )

    ;
  20. }

    else

    {
  21. isGameStartedFirstTime =

    true

    ;
  22. }

  23. if

    (

    isGameStartedFirstTime)

    {
  24. isGameStartedFirstTime =

    false

    ;
  25. isMusicOn =

    true

    ;
  26. levels =

    new

    bool

    [

    15

    ]

    ;
  27. highscore =

    new

    int

    [

    levels.

    Length

    ]

    ;

  28. levels [

    0

    ]

    =

    true

    ;
  29. for

    (

    int

    i =

    1

    ;

    i <

    levels.

    Length

    ;

    i++

    )

    {
  30. levels [

    i]

    =

    false

    ;
  31. }

  32. for

    (

    int

    i =

    0

    ;

    i <

    highscore.

    Length

    ;

    i++

    )

    {
  33. highscore [

    i]

    =

    0

    ;
  34. }

  35. data =

    new

    GameData (

    )

    ;

  36. data.

    SetIsMusicOn

    (

    isMusicOn)

    ;
  37. data.

    SetIsGameStartedFirstTime

    (

    isGameStartedFirstTime)

    ;
  38. data.

    SetHighScore

    (

    highscore)

    ;
  39. data.

    SetLevels

    (

    levels)

    ;

  40. Save (

    )

    ;

  41. Load (

    )

    ;
  42. }

    else

    {
  43. isGameStartedFirstTime =

    data.

    GetIsGameStartedFirstTime

    (

    )

    ;
  44. isMusicOn =

    data.

    GetIsMusicOn

    (

    )

    ;
  45. highscore =

    data.

    GetHighScore

    (

    )

    ;
  46. levels =

    data.

    GetLevels

    (

    )

    ;
  47. }

  48. }

  49. public

    void

    Save(

    )

    {
  50. FileStream file =

    null

    ;

  51. try

    {
  52. BinaryFormatter bf =

    new

    BinaryFormatter(

    )

    ;
  53. file =

    File.

    Create

    (

    Application.

    persistentDataPath

    +

    "/data.dat"

    )

    ;

  54. if

    (

    data !=

    null

    )

    {
  55. data.

    SetIsMusicOn

    (

    isMusicOn)

    ;
  56. data.

    SetIsGameStartedFirstTime

    (

    isGameStartedFirstTime)

    ;
  57. data.

    SetHighScore

    (

    highscore)

    ;
  58. data.

    SetLevels

    (

    levels)

    ;
  59. bf.

    Serialize

    (

    file, data)

    ;
  60. }

  61. }

    catch

    (

    Exception e)

    {
  62. Debug.

    LogException

    (

    e, this

    )

    ;
  63. }

    finally

    {
  64. if

    (

    file !=

    null

    )

    {
  65. file.

    Close

    (

    )

    ;
  66. }
  67. }
  68. }

  69. public

    void

    Load(

    )

    {
  70. FileStream file =

    null

    ;

  71. try

    {
  72. BinaryFormatter bf =

    new

    BinaryFormatter(

    )

    ;
  73. file =

    File.

    Open

    (

    Application.

    persistentDataPath

    +

    "/data.dat"

    , FileMode.

    Open

    )

    ;
  74. data =

    bf.

    Deserialize

    (

    file)

    as

    GameData;

  75. }

    catch

    (

    Exception e)

    {
  76. Debug.

    LogException

    (

    e, this

    )

    ;
  77. }

    finally

    {
  78. if

    (

    file !=

    null

    )

    {
  79. file.

    Close

    (

    )

    ;
  80. }
  81. }
  82. }
  83. }

  84. [

    Serializable]
  85. class

    GameData{
  86. private

    bool

    isGameStartedFirstTime;
  87. private

    bool

    isMusicOn;
  88. private

    bool

    [

    ]

    levels;
  89. private

    int

    [

    ]

    highscore;

  90. public

    void

    SetIsGameStartedFirstTime(

    bool

    isGameStartedFirstTime)

    {
  91. this

    .

    isGameStartedFirstTime

    =

    isGameStartedFirstTime;
  92. }

  93. public

    bool

    GetIsGameStartedFirstTime(

    )

    {
  94. return

    this

    .

    isGameStartedFirstTime

    ;
  95. }

  96. public

    void

    SetIsMusicOn(

    bool

    isMusicOn)

    {
  97. this

    .

    isMusicOn

    =

    isMusicOn;
  98. }

  99. public

    bool

    GetIsMusicOn(

    )

    {
  100. return

    this

    .

    isMusicOn

    ;
  101. }

  102. public

    void

    SetHighScore(

    int

    [

    ]

    highscore)

    {
  103. this

    .

    highscore

    =

    highscore;
  104. }

  105. public

    int

    [

    ]

    GetHighScore(

    )

    {
  106. return

    this

    .

    highscore

    ;
  107. }

  108. public

    void

    SetLevels(

    bool

    [

    ]

    levels)

    {
  109. this

    .

    levels

    =

    levels;
  110. }

  111. public

    bool

    [

    ]

    GetLevels(

    )

    {
  112. return

    this

    .

    levels

    ;
  113. }

To make this work attach the script to the GameController GameObject as a component, by dragging this to the Game Controller Inspector or by clicking add component then go to script and locate the GameController script.

Creating Music Controller

The Music Controller handles the background sounds of the game; this is where you put your game sound clip as an array. Just like what you did on the Game Controller, create a new GameObject then name it as Music Controller. Then create a new C# script name it as MusicController.
Note: All the needed sounds are already in the Sounds folder.
Inside the Music Controller script, create a certain variable that we will use for our sounds

  1. public

    static

    MusicController instance;

  2. public

    AudioClip background, gameplay, loseSound, winSound;

  3. [

    HideInInspector]
  4. public

    AudioSource audioSource;

Create an instance of the script; this will prevent the script to stop running when going to other scenes.
  1. void

    CreateInstance(

    )

    {
  2. if

    (

    instance !=

    null

    )

    {
  3. Destroy (

    gameObject)

    ;
  4. }

    else

    {
  5. instance =

    this

    ;
  6. DontDestroyOnLoad (

    gameObject)

    ;
  7. }
  8. }

Then create an awake method, a built in method of unity that call any script first when the game started first, call the CreateInstance() inside the awake method.

Create also ang method that initialize all the needed components InitializeVariables(), then write this code inside. After that call the method inside the Awake() method.
  1. void

    InitializeVariables(

    )

    {
  2. audioSource =

    GetComponent<

    AudioSource>

    (

    )

    ;
  3. }



  4. void

    Awake(

    )

    {
  5. CreateInstance (

    )

    ;
  6. InitializeVariables (

    )

    ;
  7. }

Then write these certain methods to make the sounds work perfectly in the game
  1. // Use this for initialization
  2. void

    Start (

    )

    {

  3. }

  4. // Update is called once per frame
  5. void

    Update (

    )

    {

  6. }

  7. void

    CreateInstance(

    )

    {
  8. if

    (

    instance !=

    null

    )

    {
  9. Destroy (

    gameObject)

    ;
  10. }

    else

    {
  11. instance =

    this

    ;
  12. DontDestroyOnLoad (

    gameObject)

    ;
  13. }
  14. }

  15. void

    InitializeVariables(

    )

    {
  16. audioSource =

    GetComponent<

    AudioSource>

    (

    )

    ;
  17. }


  18. public

    void

    PlayBgMusic(

    )

    {
  19. if

    (

    background)

    {
  20. audioSource.

    clip

    =

    background;
  21. audioSource.

    loop

    =

    true

    ;
  22. audioSource.

    Play

    (

    )

    ;
  23. }
  24. }

  25. public

    void

    GameplaySound(

    )

    {
  26. if

    (

    gameplay)

    {
  27. audioSource.

    clip

    =

    gameplay;
  28. audioSource.

    loop

    =

    true

    ;
  29. audioSource.

    Play

    (

    )

    ;
  30. }
  31. }

  32. public

    void

    StopAllSounds(

    )

    {
  33. if

    (

    audioSource.

    isPlaying

    )

    {
  34. audioSource.

    Stop

    (

    )

    ;
  35. }
  36. }

Next you need to attach the script to the Music Controller as a component; it is just the same like you did on Game Controller. After that add a new component called AudioSource. It manages the sounds of the game, and uncheck the Play On Wake because it will be done automatically with the script. Then from the Sounds directory drag all the sound into the corresponding script variables.
10_1.png

Creating Main Menu Controller

The Main Menu Controller is the one that handles where you start the game, and also trigger some events that necessary needed in the game. To do this just simply creates a new GameObject name it as Main Menu Controller. Create a script and name it as MainMenuController.

Inside the Main Menu Controller script, create a certain variable that we will use. Write this code inside the Main Menu Controller class.
  1. public

    GameObject settingsPanel, exitPanel;
  2. public

    Toggle soundToggle;

  3. // Use this for initialization
  4. void

    Start (

    )

    {
  5. if

    (

    GameController.

    instance

    !=

    null

    &&

    MusicController.

    instance

    !=

    null

    )

    {
  6. if

    (

    GameController.

    instance

    .

    isMusicOn

    )

    {
  7. MusicController.

    instance

    .

    PlayBgMusic

    (

    )

    ;
  8. soundToggle.

    isOn

    =

    true

    ;
  9. }

    else

    {
  10. MusicController.

    instance

    .

    StopAllSounds

    (

    )

    ;
  11. soundToggle.

    isOn

    =

    false

    ;
  12. }
  13. }
  14. }

  15. // Update is called once per frame
  16. void

    Update (

    )

    {
  17. if

    (

    Input.

    GetKeyDown

    (

    KeyCode.

    Escape

    )

    )

    {
  18. if

    (

    exitPanel.

    activeInHierarchy

    )

    {
  19. exitPanel.

    SetActive

    (

    false

    )

    ;
  20. }

    else

    {
  21. exitPanel.

    SetActive

    (

    true

    )

    ;
  22. }

  23. if

    (

    settingsPanel.

    activeInHierarchy

    )

    {
  24. settingsPanel.

    SetActive

    (

    false

    )

    ;
  25. }

  26. }
  27. }

  28. public

    void

    PlayButton(

    )

    {
  29. SceneManager.

    LoadScene

    (

    "Level Menu"

    )

    ;
  30. }

  31. public

    void

    SettingsButton(

    )

    {
  32. settingsPanel.

    SetActive

    (

    true

    )

    ;
  33. }


  34. public

    void

    CloseSettingsButton(

    )

    {
  35. settingsPanel.

    SetActive

    (

    false

    )

    ;
  36. }

  37. public

    void

    SoundToggle(

    )

    {
  38. if

    (

    soundToggle.

    isOn

    )

    {
  39. GameController.

    instance

    .

    isMusicOn

    =

    true

    ;
  40. MusicController.

    instance

    .

    PlayBgMusic

    (

    )

    ;
  41. GameController.

    instance

    .

    Save

    (

    )

    ;
  42. }

    else

    {
  43. GameController.

    instance

    .

    isMusicOn

    =

    false

    ;
  44. MusicController.

    instance

    .

    StopAllSounds

    (

    )

    ;
  45. GameController.

    instance

    .

    Save

    (

    )

    ;
  46. }
  47. }

  48. public

    void

    YesButton(

    )

    {
  49. Application.

    Quit

    (

    )

    ;
  50. }

  51. public

    void

    NoButton(

    )

    {
  52. exitPanel.

    SetActive

    (

    false

    )

    ;
  53. }

Next you need to attach the script to the inspector of Main Menu Controller as a component.

Note: When using a UI Components in the script you will just need to import some modules
  1. using

    UnityEngine.SceneManagement

    ;

And for loading some scenes you will also need this
  1. using

    UnityEngine.UI

    ;

Slicing Sprites

When slicing the sprite sheet into a several images you need to select first your image, then in Sprite Mode choose Multiple after that click Sprite Editor. A new Pop up window will appear from here click the Slice on the menu bar, and then Slice. It will automatically slice the sheet of a sprite.
11_1.png

12_1.png

Note: All the needed sprite in the game are already in Sprites folder.

Main Menu Canvas

The Main Menu UI is a GameObject that hold a several Children component. To Create a UI click the GameObject menu then choose UI then Image. You notice that it automatically create a UI Canvas then your image as a child of it, and also a new object will be created automatically called EventSytem.
13_1.png

Then rename Canvas as Main Menu Canvas, and also Image as Background. In the Main Menu Canvas set the values same as the image shown below.
14_1.png

To make the Background Image locate your sprite from the Sprites directory then add it to the Background GameObject as a component.
15_1.png

Now that we have the image we will now add a button Play and Settings. Then remove the child text of button, then locate and add a sprite to their components.
Then attach the Main Menu Controller script to the Play and Settings Button in the RuntimeController.
16_1.png

17_1.png

Main Menu Canvas - Exit Panel

We will now create the Exit Panel, exit panel take care of the confirmation prompt that closes the app. Create a panel then name it as Exit Panel Background. And create another panel by just right clicking the GameObject then set its name as Exit Panel and set the components as shown in the inspector below.

Exit Panel Background

18_1.png

Exit Panel

19_1.png

Then add a several children (Text, Buttons) for the Exit Panel. Then set the values of each component as shown below.

Text

20_2.png

Buttons

21_1.png

Main Menu Canvas - Settings Panel

We will now create the Settings Panel; it takes care of displaying the settings options to change the gameplay. Create a panel then names it as Settings Panel Background. And create another panel by just right clicking the GameObject then set its name as Settings Panel and set the components as shown in the inspector below.

Settings Panel Background

22_3.png

Settings Panel

23_1.png


Then add a several children (Text, Toggle, Button) for the Settings Panel. Then set the values of each component as shown below.

Text

24_2.png

Button

25_2.png

Toggle

26_2.png

27_2.png

28_2.png


Now that you’re done will the components there is one thing we need to do. Attach all the needed components to the MainController to be able to run the application successfully.

29_2.png


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top