- Home /
[Partially Solved] OnClick of one button, show different set of buttons.
I have figured out how to make different button menus show up based on an OnClick event. (The code is very long and there is definitely a better way to do do it. But, it works. (Code is located down below)) Thanks to all those who helped. All I did was set up a script for OnClick event sets a button menu active depending on the button clicked. Now, however, I am at another dilemma. I need the player to be able to add new buttons. For example, in the continent button category, when clicked it activates the right side menu. This menu lists all of the continent maps (each button displays a different map onClick). I need to allow the player to add new buttons that they can then attach a map image to. I know how to do an image attachment, I just need help understanding how to create new buttons that will be remembered each time the player opens the game. I imagine i'd use PlayerPrefs. Anyway, any help will be much appreciated. Thank you.
[Original Post] I have been trying to figure out this problem for hours, and I haven't gotten anywhere. What I am making is a menu system. There are several buttons in the menu system that each, when clicked, need to change the buttons of another menu to a different set of buttons. Make sense?
As you can see in the pick, the left menu, under category, has several buttons. I need each of those buttons, OnClick, to list their own set of buttons on the right menu. I have a vertical grid layout for the menus, as well as a scroll rect. What is the best way for me to go about doing this.
Right now all I need is an understanding of how to show different buttons based on button clicked. However, later, I need to allow players to add and delete buttons based on what map categories they want to use. Sorry if this question is confusing, It's hard to even describe what my problem is. Feel free to ask questions if you don't understand my question.
Thanks you all!
[Code for Menu Set active]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapMenus : MonoBehaviour {
public GameObject[] rightMenus;
void Start() {
foreach (GameObject rm in rightMenus) {
rm.SetActive(false);
}
}
// ****** !! THIS IS HORRIBLE CODE AND NEEDS WORK !! ******
public void WorldMap() {
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[0].SetActive(true);
}
public void ContinentMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[1].SetActive(true);
}
public void CountryMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[2].SetActive(true);
}
public void ProvinceMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[3].SetActive(true);
}
public void CountyMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[4].SetActive(true);
}
public void CityMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[5].SetActive(true);
}
public void BuildingMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[6].SetActive(true);
}
public void DungeonMap()
{
foreach (GameObject rm in rightMenus)
{
rm.SetActive(false);
}
rightMenus[7].SetActive(true);
}
}
Easiest way to go would be to create all buttons you may need, deactivate the second layer ones and when you click one of the active ones enable the second ones and disable the current.
void OnClick(){
foreach(GameObject go in CurrentButtons)
go.SetActive(flase);
foreach(GameObject go in ResultButtons)
go.SetActive(true);
}
But i don't remember if inactive UI elements are considered by the layout grid, if yes this would require to move them in/out of the group (change parent). On the other hand, personally i would create the max amount of buttons possibly active at once, and change their parameters according to what is needed, meaning i would keep in a script names (title of the button) and associated fuctions with OnClick event and set them via script on runtime. Cheers.
I'm so glad this is the case. Otherwise it would be a total pain!
Thank you for your help. This is exactly what I needed, and it worked well. However, rather than setting each button inactive/active, I set the entire menu inactive/active.
Answer by unity_G2yrNlWYdpJKuw · Oct 15, 2017 at 04:43 PM
Well if you want to show buttons when you click a button, then just use the GameObject.SetActive
That would probably work, I just think all of those overlapping buttons would get very confusing and crowded. I'll keep that in $$anonymous$$d though.
Well, you can create a pannel, then add buttons to that pannel as a child, then you can just turn the pannel on and off with the setactive. And name that pannel $$anonymous$$enu1 or something that will make it organized.
That is a good idea. It makes more sense to deactivate an entire menu then each seperate button. Thank you for your help!