Search Unity

Changing menu layout in a script

Discussion in 'Scripting' started by DoomDude99, May 20, 2019.

  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    I'm still new to unity and am not sure if I'm going about this the right way; I want to create an app that has two menus that can belong to two different scenes but am not sure if they have to.

    In the below canvas:



    buttons "START GAME" and "QUIT" should appear once the app is started. The remaining buttons (SMTH #2) should either appear after the user clicks "START GAME" in a different scene or in the same scene. In the latter case buttons "START GAME" and "QUIT" should be disabled so that only two buttons may be present in a scene at any one time.


    How do I do this in C#? and how can I change the background image once the user clicks "START"?

    I asked about background image changing:

    https://forum.unity.com/threads/cha...f-a-menu-canvas-in-unity.676417/#post-4561210

    but am not sure how the image can be read from a file, stored into a sprite and than that sprite changed with the current background image?

    The documentation related to LoadImage* seems obsolete:

    https://docs.unity3d.com/2017.1/Documentation/ScriptReference/ImageConversion.LoadImage.html

    Also, is it possible to move buttons designed in one canvas to another?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you do it in the same scene, I'd just keep the GameObjects of the second set of buttons disabled at the start. After the user clicks Start Game you disable the GameObjects of the first set of buttons, and enable the second set. If you instead did this as two separate scenes you would just keep the first set of buttons in the first scene, the second set of buttons in the second scene, and just load the second scene (making the first set of buttons disappear and the second set of buttons appear).

    If the scene(s) aren't all that complicated I recommend just keeping it a single scene because loading the second scene will introduce some delay while it loads.

    If the background image is a skybox, you just swap out the skybox. If the background image is a UI texture then I'd just use the same enable/disable of GameObjects as the buttons.

    Example:

    Code (csharp):
    1. public List<GameObject> StartObjects;  //Put all the GameObjects which are active when the game starts here
    2. public List <GameObject> AfterStartObjects;  //Put all the GameObjects which need to activate when the Start Game button is press here
    3. //In the inspector, deactivate all objects in the AfterStartObjects list
    4.  
    5. //Have the Start Button call this method
    6. public void StartButtonPressed()
    7. {
    8.     //Deactivate all StartObjects
    9.     foreach (GameObject startObject in StartObjects)
    10.     {
    11.         startObject.SetActive(false);
    12.     }
    13.  
    14.     //Activate all AfterStartObjects
    15.     foreach (GameObject afterStartObject in AfterStartObjects)
    16.     {
    17.         afterStartObject.SetActive(true);
    18.     }
    19.  
    20.     //Add code here about actually starting the game, or whatever you want to happen when
    21.     //the player presses Start Game beyond just playing with buttons and the background
    22. }
     
    Last edited: May 20, 2019
    DoomDude99 likes this.
  3. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    This may sound dumb, but how do I group the game objects (buttons for now) into those arrays?
     
  4. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Alright, I managed to get the buttons into GameObject arrays but don't need the two from the bottom to be shown when the scene is loaded (app starts). Is there a way to hook the "OnLoad" function of a scene?
     
    Last edited: May 22, 2019
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    DoomDude99 likes this.