Unity sprite not visible when dragged - unity3d

I am using Unity on a 2D game(I am very amateur on it still) and while using Create->UI->Image to add a new sprite works fine, whenever I drag and drop an image from my assets folder, it always lies behind the background that I have set up for my canvas, regardless of whether i use a higher sort order, if the image is a child of the canvas or the layer is the same for all my canvas elements(UI or default). Also getting the same when attempting to drag an animation.
I was reading somewhere that the canvas is kinda like another UI element and that the image would have to be a child of it in order to display above it, but even when i dragged it as a child to the canvas, I am still getting the same result. Other ideas were about using the world space mode, which appears to be fixing it for the sprite, but I am just not sure on why.
Can somebody explain what is happening ? I wasn't really able to find a good resource that explains it in such a way that would help me understand. Thanks !

In screenspace mode, the Canvas always renders on top of the normal scene objects (by design). Dropping types that inherit from UnityEngine.Object (such as sprites) into the Canvas hierarchy do not place them on the screenspace canvas, but rather in the (2D)3D scene (even though they are in the Canvas hierarchy). In order to get an image to adhere to the Canvas visual sorting depth (order in hierarchy), you'd have to place a UnityEngine.UI.Image and then add the resource sprite to it (as you appear to have discovered). At this point you can control the 'depth' of the sprite resource only with respect to other Canvas UI objects and it will always be on top of other scene-based objects.
In other words, the Canvas and its UnityEngine.UI objects (in screenspace mode) will always render on top of your normal scene objects (i.e. like a HUD). In worldspace mode, each Canvas object (and its UnityEngine.UI children) can be positioned in the 3D scene as though it is a normal scene object (more or less).

Related

Scale Player Size according to the screen size in Unity

I have an issue with different resolutions.
Everything perfectly works on 1920x1080 however when i set it to tablet size like 10x10 aspect ratio 'Player' isn't resizing.
My platforms created under Canvas due to scalement and correct positioning. However my player created outside of the canvas.
Should i create my character under canvas or should i create my platforms outside of it? Currently I am not sure how to solve the issue.
Since player is created outside the canvas, there's no way for canvas to affect it (also player is probably using SpriteRenderer not Image component).
One way would be to put player as Image inside a canvas, but to be honest, canavs is created for UI, not gameplay. Putting all gameplay into UI might (and probably will) create a lot of issues. I'm already surprised that player and platforms interact in your game well as they use different systems.
What you probably want to do is to put all gameplay elements (character, platforms, projectiles, etc.) outside the canvas as sprite renderers and leave canvas for what it's meant to be (UI, maybe backgrounds).
Then, you might come across a problem, where on different resolutions, you have smaller or larger area of gameplay. Your options will be to: live with that, create system that restricts gameplay and fills empty space with background or black bars, or something in between (which is for eg. let vertical gameplay area be different, but horizontal the same).
Here's idea how you could achieve it:
https://forum.unity.com/threads/maintain-the-game-content-area-on-different-types-of-screen-sizes.905384/

How to bring forward an object in unity?

I've a simple project with a few objects.
I tried to move "player" object (Marked in the picture) to top of other objects but still it is in backward!
How to bring it to forward?
We need some more information to give an accurate answer for your exact situation, such as the properties of the Sprite Renderer (assuming Player is a sprite) and the properties of your Canvas and UI elements (especially if it is a world space canvas), and any code you might be using to instantiate, render, or move the player.
In general though, for sprites, you can bring a sprite in front of other sprites by either changing the Z value of the transform (negative Z is closer to the camera), or better by changing the Order in Layer of the player sprite renderer, and make sure it is in a layer that is in front of the other sprites.
As for Canvas UI elements, the element at the bottom of the hierarchy will be rendered last, meaning it will show in front of everything. Think of a UI hierarchy like stacking papers on top of each other. The first piece of paper placed down will be at the bottom of the pile.

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.

Canvas Rect Transform is gray and can't be moved change position. How can I make it to be able to change it's position in the editor?

And the parent of it is just a prefab.
This is the parent prefab screenshot :
Canvas are game objects that usually can't be moved, one reason to move one is because you want it in World Space instead of Screen Space.
Canvas is the container of all of your UI, what you probably want is to move an image, a text, or any other type of UI component, those components must be children of the Canvas and they will be able to move.
You can also have an empty child containing more game objects and that one will be able to move.
Please read more about Unity's UI and Canvas.

UI elements moving with the HoloLensCamera prefab by default

I have a basic scene with 3 prefabs from the HoloToolkit-Unity package:
HololensCamera + InputManager + CursorWithFeedback
I then add a Canvas (and subsequently a Panel and a Button) to my scene, with the Canvas being in Screen Space - Camera and the HololensCamera being the Render Camera and of course everything was fine, until I noticed that when I run on device, the cube is there as a fixed game object (as one would expect!) but my whole Canvas seems like it is attached to the camera.
Why is this? How do I place a fixed Canvas in my scene?
Read through this
https://docs.unity3d.com/Manual/UICanvas.html
Notice the differences between Overlay/Screenspace/Worldspace
And also try to understand the relation between different camera's and UI elements. If afterwards you still don't understand i'll try to help you.