woonie30
woonie30

Reputation: 15

How to auto hide panel when click other button in Unity

I'm currently working on a Solar System AR for my final year project.

I created a menu with a button of 9 planets. For example, when I click on Sun button, there's an information panel open along with a 3D Sun. And then, there's a close button to close the panel.

But the problem came when I clicked all the 9 buttons, the 3D and the information panel overlapping each other and I have to close it manually using close button one by one, which is quite nuisance.

enter image description here

Therefore, is there any ways to auto hide/close panel when the other buttons is clicked. Note that I'm a beginner in Unity and not so good with coding. I would appreciate so much if you could left any tips.

Upvotes: 0

Views: 2479

Answers (2)

Golden Eagle
Golden Eagle

Reputation: 88

Yes, this is fairly easy!

For example, go to the "Sun" button and in the Button Component you will find On Click()

As Follows

Add (+) the amount of GameObjects or "Panels" you want to hide when clicking on other buttons.

Once you add them all, choose from the drop down menu on the right GameObject.SetActive and make sure the box below is set to false.

This way, whenever you click on any of the planets, the others will be de-activated.

Alternatively, you could also invoke a method that sets those panel's Alpha to 0 and disable their Raycast if you don't want to deactivate them.

Upvotes: 2

Malphegal
Malphegal

Reputation: 270

There is no code showing us how you open the panel window.

But one way to achieve what you want is to only have one panel window at all, and only change the text depends on the previous clicked planet.

Here is one example :

public class PlanetDisplayer : MonoBehaviour
{
    private bool isOn = false;

    [SerializeField] private Image planetImage;

    [SerializeField] private Text planetText1;
    [SerializeField] private Text planetText2;

    private void OnClick(Planet planet)
    {
        planetText1.text = planet.MainText;
        planetText2.text = planet.AnotherText;

        if (!isOn)
        {
            isOn = true;
            gameObject.SetActive(true);
        }
    }

    private void OnClose()
    {
        isOn = false;
        gameObject.SetActive(false);
    }
}

So, planetText and planetImage are examples, as I can see on your picture, there is some texts and I guess a picture for the name.

You can attach this class on the panel GameObject, and it will override the text on each click. This way you won't have to close every single panel window. There is only one.

Don't forget to link private text variables (and image, if any) in the inspector.

Upvotes: 1

Related Questions