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.
Related
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".
How can I make buttons work like pressing "w" or "d" because I want to make an easy way to move the car in my game, any ideas how? I can use it as two big buttons on the Screen as UI and I will also do a "esc" button so they can go back to the pause menu. I arlready tried and its a script i have to write and im bad at it thanks for help!
I suggest using other buttons than w,a,s,d because these are already used in the InputManager for Horizontal and Vertical.
You can just check if the key for example "K" is pressed and then move your car
if (Input.GetKeyDown(KeyCode.K)) {
//your movement script
You could also create a button on ther UI Canvas and add an OnClick function in which your movement script is executed whenever you press the button on the canvas
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.
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.
I used the
GetComponent<Button>().interactable = false;
to disable the button component, but find the UI gameobject become transparent. And I just want disable the button without changing transparent, just like remove the tick on button component.
Did I use it wrong? Or is there something else to do it?
GetComponent<Button>().enabled = false;