Sorting game object in front of canvas - c#

how do I sort game object in front of canvas?
notice that Render Mode set to screen space - overlay
hope you have another option instead of change render mode to camera

UI elements in the Canvas are drawn in the same order they appear in the Hierarchy. The first child is drawn first, the second child next, and so on. If two UI elements overlap, the later one will appear on top of the earlier one.
In case you need to render a 3d gameobject you cannot use the screen space overlay, and need to switch to screen sapace camera.
Everything is very well explained here.
If you could share more details of the concrete problem, what is the outcome of your attempt and what is the result expected, you will be able to obtain more accurate help.

Related

Linerenderer on canvas doesn't show properly

I am currently working on a game, and i've run into the following problem:
I want to draw some results as a graph on top of my Canvas and so far i've found out that my Canvas needs to be in Screenspace-Camera for this to work, as the LineRenderer is a 3D-Object and will be covered by Screenspace-Overlay otherwise.
And i've actually got it to work with this, looks like the following:
But the problem i've encountered is that if i increase the screen size, e.g. by stretching the GameView or maximizing it, the line disappears, even though it has a negative z compared to all my UI elements and therefore appears in front of them in the EditorView:
If i try to fix this by applying a greater negative z-Value relative to the screen size the lines get distorted, as they are getting closer and closer to the camera, and changing their alignment from view to z-Axis didn't help either.
What makes this even more confusing is that this happen to lines that are drawn lower (smaller y-value) first, meaning a line at the bottom of my graph disappears earlier. I really don't know why this is happening. Any help would be appreciated.
For 3D objects mixed with UI elements I recommend using a separate camera with greater depth than the camera drawing standard ui elements. This way your 3D objects will always be rendered on top of the UI elements and you wont have to worry about Z positions.

Animating an image not playing in game scene

I am trying to make an animation of a countdown.
I first made a Gameobject, created a canvas for it, and inside that canvas I created a UI Image, attached an image to it, and finally created an animation with that image.
Everything is working fine except the animation is not showing in game scene but only in normal scene, but when I go to that UI Image component and start the game I can see the image changing which means that its not the animation but something else.
Does anyone have an idea of what's going on?
There are several possibilities. Depending on if the image is showing up at all, you can check if your UI image is anchored correctly (four triangle marks) or pivot position is where you want it (circle mark). Depending on how this is done, if you Game window resizes in play-mode with the display set to Free Aspect, your image may move off the visible screen or, in some cases, get crushed which usually shows up as a red X in the editor. I can see this happening if your Game window is set to Maximize on Play.

Gaze Over, display Tooltip in Unity with GoogleVR

Does any know or can possibly point me to some instructions or a github repository on how I can create a script where I have an object and in GoogleVr (Cardboard) if I was to gaze over an object, a tooltip would appear?
If anyone is familiar, in the Cardboard Demos under Under Arctic Journey > Learn, when you click on the fox, a tooltip appears to showcase that item along with like a brief description on it. I want to have something similar (maybe even the same thing) except just having a gaze over will automatically show it. Is this possible?
I want to have this done on multiple objects in my project so I want it created so I can easily substitute out text and whatnot.
Have a script with a reference to a World Space Canvas (WSC). The WSC will be your tooltip and be activated when you hover over the object and disabled when you don't.
You can set images and texts of the WSC through the inspector or through code if you make a reference to them.
The script should also always have its rotation set to face the player.
You can use the SetActive(bool) method to show or hide the WSC.
The UI system makes it easy to create UI that is positioned in the world among other 2D or 3D objects in the Scene.
Start by creating a UI element (such as an Image) if you don’t already have one in your scene by using GameObject > UI > Image. This will also create a Canvas for you.
Set the Canvas to World Space
Select your Canvas and change the Render Mode to World Space.
Now your Canvas is already positioned in the World and can be seen by all cameras if they are pointed at it, but it is probably huge compared to other objects in your Scene. We’ll get back to that.
https://docs.unity3d.com/Manual/HOWTO-UIWorldSpace.html

ScrollRect not scrolling when canvas Render Mode is set to Screen Space - Camera

Did you find out why this was happening? I'm having the same issue here.
I had a canvas with a scroll bar working normally, I needed to set the canvas render mode to "Screen Space - Camera", and now the bar doesn't move anymore.
When you change the Canva's Render Mode from Screen Space - Overlay to Screen Space - Camera, an new slot called Render Camera will appear under the Canvas. Drag your main camera into this slot. That camera will be used to send events to the UI elements.
If you've already done this but the problem is still there, delete the ScrollRect and create a new one. Finally, if the ScrollRect is still not moving, make sure that no other UI object is blocking it.
Maybe a hidden panel or image with 0 alpha... Any UI object that is below the ScrollRect in the Hierarchy tab will block the ScrollRect if on top of it.
EDIT:
Once you drag the main camera to the Render Camera slot, Plane Distance slot will appear. Also make sure that it's not set to 0. Mine is set to 100 as shown in the screenshot above and that should work fine.
There might be multiple reasons why your scrollview doesn't work
I encountered with a similar problem. My canvas had a background image so i had to put a Image element without a sprite and set the alpha to "0" within Content of scrollview.
when your viewport field is not referring to viewport object in scroll rect
Try altering the movement type
I also had this problem due to not having an EventSystem in the scene.

Wrapping my head around "snap to" guideline(s)

So I created this window editor in WPF that helps me create Forms quickly. Now, one feature I've worked on was create a guideline tool. At its core it's just creates lines to help keep my UI elements organized on the screen. I will show you an example. The long black lines are the guidelines I spoke about earlier.
Now, I noticed that in a lot of art programs (i.e Photoshop) and popular IDEs that implement Forms that they have a "snap-to" feature where a UI element will snap to a line UI or to another UI element in order to maintain alignment. Something like this:
I already have the guidelines showing up in my editor. Now, what I would like help understanding is, how would I go about implementing the "snap to" feature? I'n not asking for code, just a breakdown (a visual breakdown will be most welcomed).
These are my questions:
How does an object know if one of its edges (top, bottom, left, right) touched a line?
How would I know how to unsnap the UI element if the user keeps moving the mouse past the guideline?
If I have (say) 10 lines how do I make sure that the object attaches to the nearest line(s)?
UPDATE
When an object moves or is resized, keep track of its actual size/location relative to the mouse, and separately keep track of a snapped version of the same information. If a given actual edge is within some arbitrary distance of a line -- say 4 pixels (arbitrary WPF units, really). If it's within that distance, set it to the value for the line it's close to. You still have the actual mouse-relative values as well, so you know to unsnap it if the the user keeps on dragging it and it leaves that 4-unit zone.
When an object is being resized, at most two edges of the bounding box will be changing position (assuming you can drag corners as well as edges). When you're moving an object, all four edges of the bounding box will move.
So you need to keep track of which edges are moving, and only do snap-line proximity testing on those edges. When you're moving an object, snapping the left or top edge to a line is easy. That's just the position of the object. But if you snap the right or top edge to a line, you're setting
snappedPos.X = nearestVerticalSnapLine.X - draggedObject.Width;
or
snappedPos.Y = nearestHorizontalSnapLine.Y - draggedObject.Height;
You may also have cases where opposite edges will both be in proximity to lines: Say you're dragging a seven-unit square across a ten-unit grid. When it's inside a grid box, all four sides will be in proximity to a grid line. Which wins? The closer one.
Locating the snap lines is easy -- %.

Categories