I'm fairly new to Unity and I'm trying to embed a 3d view inside a 2d one.
I'm working on an emulation app that has a 2d UI for controls and a preview of the result in a 3d box that should be embedded in the 2d one, sort of as a player.
What's the right approach to doing that in Unity? Is there a way of "embedding" one scene in another?
Thanks!
If you want to create 3D effect with UI canvas, you should look to this link.
If you are using 2D project, it is basically 3D scene with camera set to use ortographic projection, not perspective. So you can use 3D models as well.
You should look into Render Texture. Those allows to render a camera view onto a texture in a scene. Say you have a part of a scene an dyou want to render onto a TV screen in your game. You would place the TV scene somewhere and place your camera to view it. Then you create a render texture and apply onto the mesh that is making your tv screen.
Now if you wish to make a UI system, like a radar with a top view, you would modify the viewport of your top view camera (0,0,.2,.2 would place it bottom left corner with 20% height and width) and make the depth higher so that it renders on top of the main camera.
Related
hierarchy
2d sprite
panel UI
in game
Fist make sure your canvas' Render Mode is not set to Screen Space - Overlay.
If it is, then switch it to Screen Space - Camera and then change its Render Camera property to point to your main camera.
You can then use the following canvas' properties to control where it appears in relation to other sprites (which appears on top of which), the same way you control any other 2D Sprite:
Sorting Layer
Order in Layer
Also worth noting that 2D Sprites with same Sorting Layer and Order in Layer will use the distance to the camera to decide which one appears on top of which, and the same is true for the canvas:
If your canvas' Render Mode is set to Screen Space - Camera, it will always be positioned in front of your camera and you need to use its Plane Distance property in order to position it correctly.
If your canvas' Render Mode is set to World Space, you can position it freely in the world in the same way you position any other sprite.
First of all the hierarchy window in unity is completely irrelevant when its about positioning.
In Unity everything is 3D, even if you switch to 2D perspective.
So in your case you still have your camera, your sprite and your canvas in a 3D Dimension.
To set your sprite in front of the canvas you only need to increase the positioning stats (seen in your "panel UI" screenshot) at least slightly closer to your camera than the canvas is.
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!
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?"
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.
I am using Vuforia and Unity to do AR. So far I have gone through the ImageTarget example, and am able to see the teapot using my Samsung Galaxy S4. I've also managed to overlay some UI elements on the screen.
Now, I'm trying to place a persistent 3d object (capsule for example) in the world.
However, when I place a capsule say, next to the teapot, I see only a white screen when I start the android app.
Why is this so? How can I resolve this?
My scene hierachy is like this
ARCamera
Directional Light
ImageTargetChips
ImageTargetStones
Canvas <-- for UI overlay
EventSystem
Capsule <-- adding this into the world causes screen to turn white
Even if I shift Capsule to under ARCamera, I only see white when I start up the app.
Thanks.
You have to learn about the Unity hierarchy. Everything, i.e., 3D objects, image, audio, etc., i.e content associated to specific image target must be Hierarchically child of ImageTarget.
Here, you need to put your 3DObject (Capsule) inside the image target (Stones/Chips).
Like
> +ARCamera
>
> -ImageTargetChips
>
> Teapot
> Capsule
And then adjust the position & Scale of capsule (3D Object)
Your 3D object must be in a camera view:
eg. Position of camera: x:0 y:0 z:0
Position of image target: x:0 y:0 z:-60
Here, Z axis is "Depth"