- Home /
UI Outline Enable / Disable via Script?
So I have a basic main menu and subsequent sub-menu. I have pointerEnter set to enable an outline on my text, to show that it's actively "selected". I have pointerExit set to disable outline on exit.
I have my menu script attached to the canvas, where all of the buttons / text live. I have the two panels (main menu, options) set to groups in my script so i can show / hide said panels when the appropriate button is clicked (options button, back button).
I want to deselect (turn off the outline) of a button when it's clicked, so that returning to that screen shows no button currently selected.
I have tried numerous things to see if I could get my script working. At this point my only option is to set each and every button to a variable, but I was hoping I could just grab all outlines (or grab all buttons and find their text / outline component), then turn them off either in a loop (foreach component outline in menugroup) or simply by telling unity to turn off all outlines (kind of like how jQuery can affect all objects in an array at once).
I'm strictly using C# for this learning project, so if you can point me in the right direction, it'd be very helpful.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class menuScript : MonoBehaviour
{
public GameObject mainPanel;
public GameObject optionsPanel;
private Outline outline;
void Awake()
{
mainPanel.SetActive(true);
optionsPanel.SetActive(false);
}
public void exitGame()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
public void showOptions()
{
mainPanel.SetActive(false);
optionsPanel.SetActive(true);
}
public void showMain()
{
mainPanel.SetActive(true);
optionsPanel.SetActive(false);
}
public void inactiveOutlines()
{
outline = mainPanel.GetComponentInChildren<Outline>();
Debug.Log(outline.IsActive());
Debug.Log(outline);
}
}
Answer by lazerblade01 · Nov 23, 2016 at 07:32 AM
OK, so after getting some sleep, seeing GilbertoBitt2's answer, and doing a little more playing around, I came up with the following:
private Outline[] outline;
private Outline[] outline2;
public void inactiveOutlines()
{
outline = mainPanel.GetComponentsInChildren<Outline>();
foreach(Outline o in outline)
{
o.enabled = false;
Debug.Log(o.name);
}
outline2 = optionsPanel.GetComponentsInChildren<Outline>();
foreach (Outline o in outline2)
{
o.enabled = false;
Debug.Log(o.name);
}
}
I just call my function onClick() of each button, and it takes care of its siblings. Hopefully this helps others who get stuck.
Answer by GilbertoBitt2 · Nov 22, 2016 at 02:38 PM
Outline outline;
outline.enabled = false;
Where would this go? After adding in the enabled = false, only 1 of the "buttons" is removing the outline. It would seem that the "Options" screen is properly reverting the outline to false, but the main menu is not. Does calling Outline outline; get all outlines in the currently active panel / group?
Answer by elenzil · Nov 23, 2016 at 12:02 AM
Gilberto is calling out the difference between GameObjects and Components.
A GameObject can have many components. Transform, Image, Button, Outline, etc.
To turn an entire GameObject on & off, use MyGameObject.SetActive()
. To turn just one component on & off, use MyComponent.enabled
.
As far as turning off the outline on all the buttons, that's sort of up to you. You might want to look into GetComponentsInChildren<>()
. note plural "Components".
That's basically what I ended up doing, then iterating over each one. I even posted my own code that got it working, but for some reason it was removed. Not sure why. I guess answering my own question through multiple iterations until success is found is not how things work here? No idea.