Search Unity

How do i make a button that will change view into like a new canvas

Discussion in 'Game Design' started by ekeeny, Jan 19, 2020.

  1. ekeeny

    ekeeny

    Joined:
    Jan 19, 2020
    Posts:
    1
    so basically i want to click a button then a new screen will pop up and i can make more buttons on that screen and i can make a back button i already know how to make the back button all i need to know is how to make a button that will change view into like a new canvas or whatever
     
  2. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    198
    Sounds to me like you want to get organized with canvasses! I personally like to use Panels for this on a single Canvas.

    Then you can put a script (Monobehaviour) on the Canvas, which you can invoke from any Panel. Just use the inspector to make buttons fire a method in the Canvas above them via their onClick() method. And have these method show / hide the individual Panels. You can invoke more then one method per button if needed.

    With only a few screens I don't mind making them class variables, but you could make it as complex as you want. This example just alternates between two Panels when the pause button is pressed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PauseMenuScript : MonoBehaviour
    6. {
    7.     public static bool GameIsPaused = false;
    8.     public GameObject panelA;
    9.     public GameObject panelB;
    10.  
    11.     private void Start()
    12.     {
    13.         panelA.SetActive(true);
    14.         panelB.SetActive(false);
    15.         Cursor.visible = false;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetButtonDown("Cancel"))
    21.         {
    22.             if (GameIsPaused==true)
    23.             {
    24.                 Resume();
    25.             } else
    26.             {
    27.                 Pause();
    28.             }
    29.         }
    30.     }
    31.  
    32.     public void Pause()
    33.     {
    34.         panelA.SetActive(true);
    35.         panelA.SetActive(false);
    36.         Time.timeScale = 0f;
    37.         Cursor.visible = true;
    38.         GameIsPaused = true;
    39.     }
    40.  
    41.     public void Resume()
    42.     {
    43.         panelA.SetActive(false);
    44.         panelB.SetActive(true);
    45.         Time.timeScale = 1f;
    46.         GameIsPaused = false;
    47.         Cursor.visible = false;
    48.     }
    49.  
    50.     public void Quit()
    51.     {
    52.         SaveDataSystem.Quit();
    53.     }
    54. }
     
unityunity