How to add a Camera to a GameObject - unity3d

I am new to Unity and am working on a UI project. I want to add a Camera targeted on a GameObject in the Canvas. There is a Camera with the same hierarchy of the Canvas and when I clicked it the preview is blank but when I play the project everything is shown, including all the buttons texts images.
How does the Camera work with UI and is it possible to add a camera targeted on a GameObject?

For your main Canvas, you do not need a Camera. Please see the Unity Manual for information regarding the Canvas. Take note of the Render Mode option on the Canvas Component. If this is set to Screen Space - Overlay then you will not need a camera to view UI Items or GameObjects that are placed as children to this GameObject. It will also inherently not appear in your Camera Preview if it is located under a Canvas of this type.
If you would like to have other GameObjects show up in the Camera Preview, add them to your scene and ensure that they are not under the Canvas GameObject. Then ensure that they are in range of your Camera.
Hope this helps!

Related

How to set Camera View a Specific Area in UI

My all project works in UI. One of my scene has videoPlayer and I would like to set my MainCamera to that specific Area. This scene is my Recording scene but for recording I have to use canvas Render Mode and has to be "Screen Space - Camera". How I can set my camera view to that specific area.
I tried 2nd camera with culling mask but didn't work because videoplayer is already my UI's child when camera Renders UI showing all children objects.
Is it possible show to camera whereever I want in UI?
enter image description here
Yessir it sure is. I literally had to do this a couple of days ago. Here is a tutorial/video I followed to figure out how to do it for my application. From the sounds of it, all you need to do is follow the video up to ~3 minutes. Then utilize the end product of getting a camera to showcase something in a UI window for your application.
For reference if the video gets deleted the process is as follows:
Create a new GameObject within the Canvas
Add a Raw Image as a child to to the new GameObject
We want a raw image because Raw Images in particular support the addition of a 2d Texture and this is what we need in order to display the Camera view.
Add a new Camera as a child to the GameObject
Create a new Render Texture in you Project Folder
Add the Render Texture to your Camera's Camera Component in the Target Texture field
Add the Render Texture to you Images Raw Image Component in the Texture field
Move the Camera to what you want to view and it should project to your UI GameObject.
If it doesn't do this correctly or the image has weird aspect ratios, check the dimensions in the Render Texture and your Image. It took me a while to get everything to look normal but it really just takes dimension adjustments.
Hope this helps!

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.

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?"

Unity3D UGUI Canvas Gizmo

Is it possible to draw Gizmos (https://docs.unity3d.com/ScriptReference/Gizmos.html) in the scene view.. but overlaying the UGUI Canvas (within the scene view) and not particularly in world space? I'm looking to outline some UI elements in the scene view more fully than the vanilla Unity RectTransform is shown there.
We looked at using https://docs.unity3d.com/ScriptReference/Gizmos.DrawGUITexture.html but that appears to fall-back on old-style UI layout and doesn't account for the multiple different Render Modes the UI Canvas can be set to.
No.
You need to create the gizmos yourself in the game. There are some plugins in the asset store that provide re-implemented gizmos. You can have a look at them.
To show a 3D object in a texture, you need another camera and a RenderTexture asset. Let the camera look at the gizmos you created and assign the RenderTexture to Camera.targetTexture.
To show the texture on the UGUI Canvas, create a RawImage, then assign the RenderTexture to it.

Unity canvas rect shows in game mode

After adding a GameObject -> UI -> button to my Unity canvas, when in game mode the canvas rectangle is showing up when I don't want to see it. It should only be visible in scene mode. How do I ensure that it is not visible in game mode?
Check if Gizmos is On. Turn it Off and outlining rectangle would not be visible in GameMode. Or it can be an Image component attached to canvas if you are talking about filled rectangle.
it turned out that when i built the game on android the canvas rect is not invisible (it is only visible in unity editor).