How can I prevent UI raycast from going through another Canvas? - c#

I have a two canvas in a Scene and all Canvas are set to Screen Space mode camera render mode. The first Canvas is the main Canvas and has some buttons.The 'order in layer' property is 0.
The 2nd Canvas covers the whole area of the 1st Canvas . When you click some button on the first Canvas,the second Canvas will popup over the first Canvas (order in layer is 1).
The problem is that when I touch over 2nd canvas, the raycast also hit the 1st Canvas. How can I prevent the raycast from hitting the first Canvas while the 2nd Canvas (popup) is displayed on the screen?

If the second Canvas is a just a popup UI over the first one, you don't need to make it a Canvas. Make it a Panel instead. Select your first Canvas, right click then go to UI->Panel.You can also do this from GameObject->UI->Panel. Just make sure that your first Canvas is selected.
A new UI panel will be created inside the first Canvas. You can rename this to Popup panel then put all the UI components of the popup UI such as Buttons and Texts under the Popup panel. Use popupPanel.SetActive(true); to show the popup Window, and popupPanel.SetActive(false); to hide it.
Just drag the PopupPanel to the popupPanel slot in the script below, then you can use showPopup() and hidePopup() to control the popup panel.
public GameObject popupPanel;
void showPopup()
{
popupPanel.SetActive(true);
}
void hidePopup()
{
popupPanel.SetActive(false);
}
EDIT :
Make sure your popup Panel is at end of the canvas in hierarchy to make it render on top of everything else. This is the main idea of blocking interaction with others behind it.

Related

C# Winform - My panel background color keeps disappearing when i place it over other's right - bottom edge

Here is before I place it over another panel:
And here is after i place it over other panel:
Select your panel and click 'Bring to Front' button like in screen shot
https://prnt.sc/lurljw
You should draw new panel over place where it should be, it will work. Because when you draw panel on other panel it becomes it's child and when you move child to right or bottom side it try to stay inside parent.

Static position within Flow layout panel

I've been trying, for hours, using different implementations with background images and Paint handlers, to set a fade over a list of labels in a FlowLayoutPanel.
The list is horizontal (wrap=false), and automatically scrolls to the end (the last item is continually scrolled into view), which is why I wish to apply the fade at the start (so you can see that there are more items out of view).
I believe if I can fix a label at the start, having it hover over the other labels in the Panel, I can then draw the background over.
I tried adding the label over the Panel itself, but it is not transparent over the panel, just a white box...
Is this possible?

UI does not move when the window is resized

Somehow, and I can't get why, some parts of my UI don't move when I resize the window. It looks like they are not placed relative to the canvas borders, even though they are children of the Canvas. I tried to unparent and re-parent my UI components having this issue but nothing solves it. I restarted unity, deleted these parts and created them again adding new UI objects, and still the very same issue. While my older parts of the UI nicely resize with the canvas.
Do you have any idea of what could go wrong ?
before resizing the window:
after resizing the window using the mouse:
The white square is supposed to stay in the top left hand corner but just seems like fixed.
You have to set the pivots of your UI components from the transform component theres is a box click on it and press shift and alt to set a pivot . Also use a panel as parent for components you need to resize .

Move controls between multiple containers in a single form

The questions says it all.
How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.
I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.
Can someone provide a simple example, so I can extract the idea to apply to my situation.
NOTE: Runtime of course.
Assuming you need it runtime:
You can save the control as bitmap using Control.SaveToBitmap method
Create cursor from image.
Set the current cursor which we created from control.
Once drag and drop completed reset the cursor.
Let's take a simple example of dragging a button around.
Suppose you have two types of container controls:
1) an X-Y layout
2) a flow layout (assume left to right)
When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.
As the mouse moves, first check if the mouse has changed container controls.
If so, then remove the button from its current parent and add it to the new parent.
If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).
If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.
As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.
This should give you the basic idea that you could use to start coding something.

Allow child panel to popup outside of parent control/form

We have a custom timeline-esque control we're creating in C#/WinForms. In it, we have a bunch of keyframes that, when clicked, popup a properties dialog with specific and dynamic controls based on the type of keyframe it is.
This box is currently drawn within the timeline's clip rectangle, so if it's bigger than the available space only shows part of the panel. I wouldn't mind allowing this panel to break out of the parent's clip rectangle, and even overhang outside of the form itself.
The only problem is I don't know where to begin! Thanks!

Categories

Resources