What is the relation between the Camera and the Canvas in Unity? - unity3d

I'm not understanding the relationship between these two objects. I'm searching for a good explanation but I can't find anything simple.

We need to define in a simple way what is Canvas and Camera.
The Canvas is the area that all UI elements (buttons, panels, text...) should be inside.(https://docs.unity3d.com/Manual/UICanvas.html)
A Camera is a device through which the player views the world (player, enemies, props, buttons...).
(https://docs.unity3d.com/ScriptReference/Camera.html)
Everything the camera sees is the screen space or world space. The canvas is overlapped on the Camera. So the relation between them are Render modes of Canvas, they allow you to represent the user interface in different ways (screen space - overlay, screen space - camera or world space).
I hope it's helpful.
Regards!

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/

Unity VideoPlayer with Subtitles

I was going to use the VideoPlayer to render to Camera Near Plane, but I also want to display subtitles for the video for the sake of accessibility. I'm wondering what the best way to do that is.
I can't see anything on a canvas if I render to Near Plane. I'd like the video to appear in front of the scene so that I can have the scene there once the video is complete.
Do I need to be using a render texture to achieve this? Seems like a render texture might incur some unnecessary overhead for my purposes, but I could be wrong.
The idea is this:
Far Background - Scene
Background - Black Image (so i can fade to scene)
Middleground - Video
Foreground - Subtitles
More info:
This is a 2D point and click adventure game with a pre-rendered cutscene.
You could do this with a render texture, place it in front of the camera at an exact distance and size, but I wouldn't. Probably would be a different camera anyway for lighting or clipping purposes.
I would use a second Camera, rendering over top of the Main Camera, with the subtitle UI's canvas targeting the second camera's screen space, and clearing depth only. It will render what it sees, but with a totally transparent background. Then, you can render your video on either the main camera's near plane or the new subtitle camera's far plane.
You could put your black square in front of this camera, too, though it would be in front of the video. It could be UI on the main camera, or stick a third camera in between them. You might have to worry about performance if there are too many cameras, but I have used two or three before to no noticeable performance hit.
Robert Mocks's answer is perfectly tenable and makes sense to me. Thank you for that!
What I decided to do instead was use a RawImage so that I wouldn't have to deal with extra cameras. This way I can use the canvas as I normally would and don't have to deal with render textures.
This involves using the API Only setting along with the following code:
rawImage.texture = videoPlayer.texture;
That seems to work well for me.

Screen Space - Overlay Canvas for VR in Unity

How can I get the effect of Screen Space - Overlay Canvas on VR?
I want both of the eye cameras to have menus in the exact same positions.
I don't want to mix it with world space, so the only option I can think of is having a duplicate of each UI element, one for each camera.
Am I missing a simpler solution?

how to render static 3d object in certain coordinates unity

This is my first time with unity, I need to make a 3d object in the camera of the device (vr) in certain coordinates of a map, do you know any links that can help me? Is only a static object will always be in the same position and users will be able to see it on their devices
If this is to be overlayed above a 3D scene use a renderTexture, then add the rendered texture to the UI using a Raw Image component.
Another way to do this is to use a canvas set to screen space overlay rendering. You can create a canvas by going to GameObject > UI > Image.
(see page for UI>canvas on Unity manual site, I can only post 2 links as a new user)
The canvas in this rendering mode is described this way on the unity manual page for the UI Canvas:
Screen Space - Overlay
This render mode places UI elements on the screen rendered on top of the scene. If the screen is resized or changes resolution, the Canvas will automatically change size to match this.
I am assuming this is a UI element over a 3D scene, because you mentioned VR.
Credit: I got some of my links and info from the unity forum thread "What is the best way to display 3D models as UI elements?"

Implement Zoom(using UI Canvas) in Unity 2d game

I want to implement as showed in the below image in 2D game, I can find so many tutorials for 3D like mini map concept but for 2D i couldn't find anything. In my game i want to show a secondary
camera as in below image and also i need to zoom the content that will show through it. I developed one concept but it can be done with sprites or with Canvas in World Space mode. So you can see
they won't resize or positioned according to the screen resolution. If you guys have any idea how to do this task,it will be very helpful for me. And i also tried with depth mask shader .Thanks in advance.
Use a camera with target texture
Follow my tutorial here: Particles with Dynamic Text but disregard the parts about the particle system, they are irrelevant for you.
Once you've made a material, stop following the tutorial, and instead:
Create a sprite renderer on a canvas that is "Screen Space - Overlay" and set the material to be the one that you created.