2d ordering in unity - popup

i am making a 2d game in unity.
i am missing a feature that cocos2d has that when you create something in the parent it keeps its z ordering inside that parent.
the problem that i am experiencing is with popups, when i create a popup or menu i want to make sure that it gets displayed above everything else in the scene or any other popup, but i cant make sure that it does because some elements in the scene may have z value that even though they are inside a parent object may appear above my popups if i am not careful. same goes for popups if i would like to have a popup on top of another popup i will have to make sure that there are no intersecting children between them.
does anyone know is there any way of containing an object to be rendered only inside its parent z order like cocos2d does? or should this problem be approached differently.

When working in 2D mode in Unity, you should just focus on setting the Order In Layer value of the sprites to have them display as you want. Leave all the z values in 0.

Related

Is it possible to get position in 2D scene WITHOUT script?

I have placed a png background to my game, and i would like to know the position of certain element in the background (the top on the tree, i have pointed with red arrow)
I would like to know the 2D position of this tree top. I want to click with mouse on the tree top and get the position.
I don't want to use script.
I need to know a lot of locations in the background and i want to get them while the game is not played, but rather when i am in editor mode in the scene view, as shown in the picture.
Is this possible?
Pretty simple. Click the + in the top left of the Hierarchy, and add an Empty GameObject. Make sure it's Z position is the same as your background, and move it using the handles in the scene to the top of the tree. Once there, look at the position on the right, and that is the position of that point.

How to create a a gameobjective that is only visible in scene view, not game

I would like to create a bunch of gameobjects to be used as spawn points and path finding targets, I’d like these to be visible on the scene view, even while the game is running, but not visible in the game. Seems like this should be a thing, but I’m failing to find it.
I’ve tried googling this, but all I find are people that are experiencing this behavior, but it undesired.
If you just want to see them, you can use OnDrawGizmos.
Inside this MonoBehaviour function, you can use Gizmos.DrawSphere(position, radius) to draw a sphere that can only appear in scene view. You just need to use the position of your GameObject as the position argument and you can draw as many spheres as you want. There are other shapes as well, see what fits your needs better.
Read the documentation for an example. Make sure to have your Gizmos activated in the scene mode, otherwise it doesn't appear.

Unity2d: Why does this sprite act like it is behind the background even though its Z-value is set to be in front of the canvas?

Showing the general setup of the scene and where the sprite is
the sprite is "behind" the background but the Z-value should be correct
I have tried solutions such as changing the render mode of the canvas; the different options do not work and "World Space" turns the game view into the default background color. I could not figure this out after a day of reading up manuals/guides and looking at other questions. Thank you for helping me!
The issue is you are trying to use a SpriteRenderer on a Canvas object. Remove the SpriteRenderer and either use an Image or RawImage component instead.
Another note regarding UI that you will inevitably run into is unless specified otherwise, objects further down in a Canvas hierarchy will render on top of those above it.
I would also avoid changing the z-axis on all of your objects. Make Canvas groups that have different sorting layers to render groups on top of one another. Place objects in a specific order in the hierarchy to get the draw order you desire. Messing with the z-axis in a 2D scene can be a headache down the line and is not very modular. If you want to change sort order later, the specific z offset you set to each object will need to change.

How to create a custom height snapping tool in Unity Editor

I am making a strategy game and I need to have a tool which places the objects above the terrain while I am dragging them in Unity Editor when I work on level design.
Basically I want to get result like here:
https://www.youtube.com/watch?v=YI6F1x4pzpg
but I need it to work before I hit the Play button in Unity Editor.
Here is a tutorial
https://www.youtube.com/watch?v=gLtjPxQxJPk
where the author of it made a tool which snaps the object to the terrain height when a key is pressed. I need the same to happen automatically whenever I place an object over my terrain. And I want my tool to adjust the Y position of the object automatically even while I am dragging it inside of the editor.
Also just to clarify: I don't need grid snapping and I don't need this functionality during the gameplay. I just need to have a tool for my level design work.
Please give me a clue where to start with it.
Thanks!
There is this tag you can apply to classes so they do call their regular events during editor mode already: https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
A trivial way then would be to apply this to a special class/object which regularly "finds" all objects from the game object hierarchy. Then it shall filter that list for the ones you want to snap to the axis and enforce their Y.

Unity - MRTK - HoloLens: Modify Collider of 2D-Buttons so the Cursor gets closer

My current problem is that the cursor appears too far away from a button. You can see in the screenshot what I mean. Hovering over a button from the list lookes like this:
Question: What can I do so the cursor get closer to the button, because on the HoloLens you see the distance?
Looking somewhere else on the canvas except the buttons, the cursor gets closer:
--Edit--
I should mention that the scene has a scaled cube (the gray thing in the screenshot) and in front of that a world canvas (white thing), which contains the scrollview/list.
I saw the same behaviour for UI elements.
I can only provide you a workaround. It is a bit hacky but it works:
Go through all UI elements especially Text and Image and disable the option RayCast Target.
This makes the Cursor be sitting right on top of them ... but you will notice your Buttons are now non-responsive and you can not interact with them anymore.
This happens because the Physics system requires either a RayCastTarget or a Collider in order to fire it's pointer events like e.g. PointerEnter, PointerDown etc.
Therefore now add a BoxCollider (not BoxCollider2D!) to your Buttons and scale it to the correct size. It looks like you are using a VerticalLayoutGroup so you can simply correct the positioning of the BoxCollider by setting the RectTransform to centered once (the VerticalLayoutgroup will anyway re-enforce the Top-Left anchoring). In my case the BoxCollider needs with 0.8 and height 0.1 ... and for the z I choose 0.01 but it can be smaller if you whish
Hurray, now the buttons are interactable again and the Cursor only has it's usual distance + the half of the choosen z thikness of the BoxColliders.
Since the Background cube has it's own BoxCollider anyway we don't need to add further Colliders for the ScrollView and UI panels.
You might have to add some though for the ScrollBars as well if you need them!
As said this is more like a quick workaround and might not be a final solution since whenever a size of the Button or the ScrollRect is changed you have to rework those hardcoded BoxCollider dimesnions as well ...
I had similar issue on 3D objects. This could happen because the objects collider definition. I mean, you can import a render mesh but the mesh collider could be different (bigger, smaller, ...)
I hope this solves your problem ;)