Select UI items on Start - unity3d

I'm working in Unity 5, and I have a UI panel with buttons. Attached to this panel is a script doing this
private List<Selectable> selectableItems;
void OnEnable()
{
EventSystem.current.SetSelectedGameObject(selectableItems[0]);
}
After hitting the 'Play' button it does not get selected (color is still normal, not highlighted). What is causing that? EventSystem has this button as current selected objects and it is reacts on Submit.

Related

Some Objects in Prefab instance not response the click event

I have multi-numbers browser windows in the Unity app, the newly opened window will popup to the front.
I want to call the clicked window to the front, so I attached a Hierarchy script.
public class Hierarchy : MonoBehaviour, IPointerDownHandler
{
[SerializeField] private RectTransform topWindow;
// Set clicked object to top
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
// Print debug message to the console
Debug.Log("Object clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
topWindow.SetAsLastSibling();
}
}
But windows only response to the front when it's title bar be clicked, click else where doesn't work. I checked it only detect click event when the title bar be clicked, but not detected click event when click browser area.
Here is the hierarchy in the Prefab, BrowserWindow is the entire are of the window. BrowserBackground is the dark background wraps browser. Browser2D is the Prefab for rendering browser. ControlButtons is the title bar include the page name, close button and detail button.
Anyone can figure out what is this problem comes from?
Try turning off the Raycast Target of an Image that is not related to a click event.
Unity Event takes into account the before/after relationship of objects.
Unity Event responds to Image and Collider.
Unity Event does not respond when Raycast Target is turned off.
If it does not work, it may work better if you use a click-only transparent Image for the foremost side.
BTW: Transparent images are heavy. You can solve this problem by turning off "Cull Transparent Mesh".

UI Button doesn't disappear when deactivating parent in hierarchy

I'm trying to write a simple main menu page with this hierarchy:
MainMenu (an empty game object)
↳ Canvas
↳ PlayButton (a TextMeshPro Button)
I want the button to deactivate the 'MainMenu' object in the hierarchy when clicked, thus hiding its Canvas child and the Button along with it. To do this, I'm providing the MainMenu object reference to the Button's OnClick() callback and calling 'GameObject.SetActive' to false when clicked.
My problem is that when I click the button, the MainMenu hierarchy get grayed out in the editor but the Button sticks around with its yellow Highlighted color shown. It does disappear if I change the display resolution while still in Play mode (maybe forcing a refresh?), but I can't figure out why deactivating the MainMenu wouldn't make it refresh in the same way.
I've tried calling SetActive(false) on the PlayButton and the Canvas themselves- I also tried setting enabled = false on them, but the Button appearance behaves the same. From everything I've read, calling SetActive(false) should hide whatever gameObject I call it on, so I can't figure out what I'm missing.
Alright, found my own mistake. The Camera's Clear Flags setting was Don't Clear, so even calling Destroy(gameObject) on it didn't remove it from the screen.
Setting the Camera to use ClearFlags: Solid Color fixed the problem.
It looks to me like you have a button in your grid that is displaying. When the game is playing, after you click to turn off the button, go to the scene tab, click the yellow button and see where that object is.
If in doubt (For testing purposes only) delete the button on click and if you see something else then it is obviously not that button.

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.
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.
Yes, this is fairly easy!
For example, go to the "Sun" button and in the Button Component you will find On Click()
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.
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.

Unity new UI button click able outside of button area, non clickable over button area

I have created new UI button in empty scene,
Whenever I click on button its not click-able , but when I take cursor slightly above the button area and click, it gets clicked.
I guess Something like offset problem occurring?? Any fix to that?
NOTE I have tried creating new scene same result
,also created new project though Same result.
Did you check the EventSystem gameObject, it displays which UI element you are hovering, and other useful information.
After lot of trial and errors, reinstalling Unity did the job

How to show GWT Hover text on a click

I have as question mark icon with hover text attached. If the user puts their mouse over the icon and waits a second, the hover text shows up. However, if the user gets impatient and clicks on the icon, nothing happens, ever.
How can I make it so the hover text shows up on a hover or on a click?
Thank you,
Greg
Currently you use "title" property to show a tooltip. If you add a click event, you run the risk of showing two "tooltips" at the same time. The clean solution is not to use "title" property at all and use a PopupPanel instead with a simple MouseOver handler:
myWidget.addDomHandler(new MouseOverHandler() {
#Override
public void onMouseOver(MouseOverEvent event) {
myTooltipPopupPanel.showRelativeTo(myWidget);
}
}, MouseOverEvent.getType());
Obviously, if you can add a MouseOverHandler directly to myWidget, there is no need to add it as a DomHandler.