How to find the dimensions of screen in Flutter Flame Game without Camera? - flutter

I have a Flame Game app that I want to adjust to make it available on all kinds of screens, but my app doesn't use a world or camera. All of the solutions that I've found for finding screen size in Flutter seem to use these items. I don't need a world or camera, but I don't know if there's a way that I can find the screen size without it?
I've tried researching this, but can only find solutions using world or camera.

tYou can get the size of the canvas by just checking the size variable in your FlameGame class. (gameRef.size if you want to use it from a Component that has the HasGameRef mixin)
There is a built-in camera in the FlameGame class if you want to apply a FixedResolutionViewport, which sounds like what you want to do? This is the only line you have to add to do that, preferably in your game class's onLoad method:
camera.viewport = FixedResolutionViewport(Vector2(800, 600));

Related

How to get manual camera focus in Flutter

Currently I'm using this library:
https://pub.dev/packages/camera and it has setFocusMode to either Auto or locked, but I need a way to be able to get manual focus mode for camera, where user can tap in camera feed and the focus should be adjusted accordingly.
How do I go about implementing this in my app?
I found this plugin https://pub.dev/documentation/manual_camera/latest/. Does this work? you could use focus distance. If you could get the distance of the object you could set it that way. It's almost like shooting out a ray in game programming. I don't know if this is possible to do but maybe using the size of the objects in the image you could get the distance. someone else has probably already figured this out.

Unity MapBox with custom width and height

I am making a Unity app for Android and iOS and have a requirement to show map in a kind of frame.
For Showing map I am using Mapbox unity assets.
So I tried creating a parent game object and make the map child of it padding on all sides but when I run the scene map still loads fullscreen with no padding at all. I tried changing the camera extent option of the abstract map class but the results were not good and when zoom in the map it actually goes out of bounds of my frame.
I also noticed that when I zoom in and out it actually changes the z axis position to do the same.
So I want to ask how to keep the width and height of the map fixed and make it zoom in and out in that particular area.
Below is the attached image of how I want to style the map.
yes sure !
first you watch video :
https://www.youtube.com/watch?v=28JTTXqMvOU
Then I'll explain if you have a problem !

How to change the Canvas Render Camera via C#

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!

how to create dynamic UI that adjust itself to current screen dimensions after loading?

I have created a level editor using new UI system in Unity3D.
Using Level Editor:
I can drag the ui elements,
I am able to save them in scriptable objects
But how could i save and load them at runtime?
I mean, if i create a level with some {width x hight} resolution and if i load it on different resolution, then the whole UI positioning get distorted.
How could i anchor them correctly programmatically?
Any help would be appreciated.
There are good video tutorials about the 4.6.x UI here:
http://unity3d.com/learn/tutorials/modules/beginner/ui.
Especially for positioning the elements I recommend watching: http://unity3d.com/learn/tutorials/modules/beginner/ui/rect-transform.
I suggest you learn how to use the anchor correctly first. If you want to change the position and size of UI element in a script you can do that through the RectTransform component. It offers functions like offsetMax,offsetMin and sizeDelta for position your UI element. The functions you need to use depend on your anchor setting.
as LokiSinclair said. You just need to Adjust the Scale that the new UI provided. It is the four Arrow on the Canvas of each Object and every UI object is inheriting their scale behavior from their Parent.

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.