How to change the Canvas Render Camera via C# - unity3d

I have two camera. One which is the main camera and another that follow an object.
The main camera is attached to a canvas with HUD and other GUI Text stuff. When I change the camera to the object that need to be followed, I loose the canvas. I clearly need to change the Render Camera inside the Canvas component.
I've tought that something like this would exist
canvas.GetComponent<Canvas>().renderCamera...
but it's not. So how can I change the render camera via script?

I think it's this one:
https://docs.unity3d.com/ScriptReference/Canvas-worldCamera.html
canvas.worldCamera = someOtherCamera;

Assign to the Canvas.worldCamera
Doesn't make sense but that's the way it is!

Related

Unity AR UI not showing up

I have created a simple Unity AR Foundation app which places objects on a plane whenever the screen is touched. I would like to add some UI so the user can press a button rather than anywhere on the screen.
I have followed several different tutorials which seem to be doing mostly the same thing. I right-click the Hierarchy -> UI -> Button. I have scaled it so it should fit my mobile screen and anchored it to the center so it should be easy enough to find.
These are the canvas settings:
Might the UI somehow be hidden behind the camera feed from the AR Session Origin -> AR Camera? Am I missing any steps to anchor the UI to the screen?
As you can probably tell, I am very new to Unity but I feel like I have followed the tutorials for creating a UI, but it simply won't show. If you need more information, please just ask and I will provide.
Not sure but sounds like you might need to have that Canvas Scalar to scale with the screen size. Change the UI scale mode to Scale with Screen Size.
I was compiling the wrong scene. I had two very similar scenes, so when I compiled I didn't realize there were no changes and that I was inspecting the entirely wrong scene.
Once I changed to the correct scene the setup above worked as expected.

How to show a text instead of 3d object in unity5 and vuforia?

Hope there's a response. :( What I want to is: Object recognition. Using Unity and Vuforia. Is it possible to make a text or a toast that will pop out or show in the camera when the objects is detected, instead of a 3d object? For example. If I detected ace of heart card. It will a show a text "ACE OF HEART"
yes it is possible, use UI component and put it under Image target, I think its very enough and If it is not then let me know I will put the entire content here, but belive me it is very simple try it just once.
Yes Its Possible.
You Just Have to Add Unity UI (You Just Could Use a Image with the text, in that case add an Image or you just could use a Text, so just add a Text, it's your choice) To that Object and set the Canvas in WorldSpace Mode.
And just that should do the trick.

Get Texture2D from a Canvas in Unity?

I am trying to post an image of my game hero to facebook. So I instantiate the prefab which contains number of UI elements, icons and texts.
I'd like to get a Texture2D or image data from it so I could send that as a payload to my facebook plugin.
If it was visible in camera and it wasn't a UI element I would get its texture from the camera, if I let it appear on the screen the full screen camera won't be looking at it, because it is rendered using canvas. Also canvas is also looking at other objects around it because this prefab doesn't occupy full screen.
I may be wrong but I don't think that you can render the UI on a texture. It doesn't seem ideal, but you could use CaptureScreenshot, then load the image and cut the part you want based on the resolution of Screen.

Unity sorting layer

I am developing a small 2d game. I have used sorting layers to my game objects. But when I add new ui text to the scene, it applies to the bottom layer of the scene. I even can not find a sorting layer option there. How can I bring the ui text to front. Thanx
It depends, it could be that you have your canvas set to a particular render mode. So try clicking on the canvas and where it has a drop down menu called render mode select screen-space camera, attach your camera to it and then set your sorting layers from there until you get what you need. Should work with 'World Space' as well. Hope that helps.

Touchable screen while using camera

I'm really stuck on my work. I read a lot of questions&answers on stackoverflow but still I can't find the exact answer that would suit me. Please help me or even just point me in the right direction...
I'm trying to write an application that would let me choose an object on the screen while using the camera.
I'll try to explain it in a more simple way:
I turn on the application, the camera live screen appears as if I was recording something. I use my finger and select 3 specific points on the screen (these would be small color paper squares I've prepared earlier). My choice is saved to the phone.
(I'll stop here because I think further explanation of what happens next is not important here).
I don't know how to create a program/code that would let me select those points on the screen. All I have now is a simple code that only turns on the camera and lets you record videos.
Please help.
You're code to start the video feed, I would assume, gets a Camera object, starts it and connects it to a SurfaceView maybe a SurfaceTexture. The frames from the Camera are directly displayed to the SurfaceView if you have set:
myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myCamera.setPreviewDisplay(myHolder);
This is a fast and easy way to display the camera frames, but it means you cannot control how the frames are displayed. For instance, if you wanted to process the frames and then display them yourself, you would need to subscribe to the onPreviewCallback() which gives you a copy of each frame. If you then omitted the above two surfaceview lines, the frames would not be automatically displayed and you could draw them yourself, however this would be alot slower. https://stackoverflow.com/a/4367250/514531
You can draw on top of a surfaceview using a Canvas. A canvas has many methods for drawig shapes/rectangles/lines/circles/words etc. You use Paint objects to specfify color/width etc.
Im sorry if this is wrong now but I have been using an older Android, but, if you do display via the surfaceview you cannot lock a canvas to the surfaceview. https://stackoverflow.com/a/12565208/514531 To get around this I found some code a while ago which creates an OverlayView class. In Android you can place views on top of each other in the Z direction. Using an empty transparent surfaceview that doesnt use myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); myCamera.setPreviewDisplay(myHolder); you can lock a canavs to it and start drawing.
The OverlayView class is simply a class extending SurfaceView and is used as a custom display. To do this you need to declare it in your layout file for your first/camera activity, like this:
<Your.package.OverlayView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/overView"
/>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/surfaceView"/>
In this layout file R.id.surfaceView is your first surfaceview you use with the camera and set up like you probably already have. The R.id.overView is the custom surfaceview for drawing.
You can create it like any other view component:
OverLayView overView = (OverlayView)findViewById(R.id.overView);
This acts like a normal class as well so you can send some parameters to it as normal and call methods on it. For instance I have a method:
overView.setCamera(myCamera, cameraHeight, cameraWidth, getResources(), isFrontCamera);
Now you have a camera displaying to a surfaceview and a separate surfaceview you can draw to. So you now need a TouchEvent handler.
In the OverlayView class override the method onTouchEvent(MotionEvent e). The MotionEvent tells you what event occured, touch on/off. First check that it is a touch down(press on the screen) and then you can get the X and Y coordinates from the MotionEvent e.getX;
This could be combined with the canavs to draw where a touch as occurred.
On thing to note: The dimensions of the camera preview will probably be different to that of the surfaceview. You know the camera dimensions from the camera object and the surfaceview sizes are from the surfaceView onchange method. So you need to calculate a ratio to convert the two, something like: (viewWidth/touch)*camerawidth.
Once you have the coordinates you could use crop the camera frame where you want.