Scale Player Size according to the screen size in Unity - unity3d

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/

Related

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.

How i can Display Unity Game on multi monitors in unity?

I want to display my unity game on multiple monitors, how I can do this ?
I have already tried the solution from unity documentation but it doesn't work.
How can I divide one scene in many displays and then play these on multi monitors?
solution from the documentation works (even if its a bit quirky). BUT in order to have the same scene seperated into multiple views, you need to either
1) Set up seperate cameras for each view. doing this correctly will require you to tweak the projection matrixes, but for some cases this might not be necessary
2) Set your camera to render to a rednertexture, display this texture on a worldspace canvas, set up cameras to view only parts of that render texture. This way is a bit easier to set up but (depending on the resolution) might get slow.
both ways will require additional work if you want anything to be clickable

Purpose of mipmaps for 2D sprites?

In current Unity,
For use in Unity.UI as conventional UI ..
for any "Sprite (2D and UI)", in fact it always defaults to having "Generate Mip Maps" turned ON. Every time you drop an image in, you have to turn that "off" and apply.
As noted in the comments, these days you can actually use world space UI canvasses, and indeed advanced users may indeed have (say) "buttons that float over the head of Zelda and they are in the far distance". However if you're a everyday Unity user adding a button, just turn it off :)
In Unity, "sprites" can still be positioned in 3D space. For example, on a world space canvas. Furthermore, mipmaps are used when the sprite is scaled. This is because the mipmap sampling is determined by the texel size rather than the distance.
If a sprite is flat and perfectly scaled then there is no reason to use mipmaps. This would likely apply to your icon example.
I suspect that it is enabled by default for 2D games where sprites will often not be perfectly scaled. To clarify, a sprite does not need to be on a canvas. Sprites can exist as their own GameObject with a Sprite Renderer (not on a canvas.) When this is the case, scaling the camera view will change the sprite's size on the screen resulting in mipmapping due to the texel size changing. This results in making the sprite always perfectly scaled challenging without a canvas.

Unity3D: Add text to a PreFab

I'm making a 2d board-game-style game in Unity 5, and I have a prefab made up of a couple of sprites which represents a game piece. I want some text in my prefab that I can update as the game progresses.
If i try to add text, it requires a canvas, but when I create a canvas, an extraordinarily enormous canvas is created, that looks to be at least 1000x times bigger than by camera area. If I try to place this canvas inside my prefab, my prefab is now made of an enormously huge canvas, and my tiny sprite images. This makes the prefab impossible to position, or calculate sizing or animate, or anything else I want to do.
How can I add text to a prefab, and make the text contained within the size of my prefab spites?
Here's what I have tried so far:
if I set the canvas for the text to "Render Mode: World Space" I'm able to make it's rect tranform smaller. However, if I get it as small as my sprites, the text becomes an unreadably blurry mess. I guess this happens because my sprites are literally at least 1000x smaller than the canvas, so when I zoom in enough to even see the sprites, the text has been zoomed into oblivion. My sprites are so much smaller than the canvas, that if I am zoomed out to see the full canvas, my sprites are not even visible.
I'm able to kind of make things work if I recreate my prefab using UI Images instead of sprites. This way, the UI Images, and the text are both UI elements contained in the enormous canvas, so the size disparity doesn't exist. However, I don't know what the pitfalls are going to be trying to build an entire game out of ui images instead of sprites. Do I get all the state capabilities of sprites?
The canvas Unity generates that is 1000x bigger is used for GUI related objects that are not meant to directly interact with the game world like the score or buttons. In order to have moving text, try using 3DText. Have the text face the camera so that the text appears to be 2D. You can find this option in the menu under
GameObject => 3D Object => 3D Text.
From what I understand, all you need to do is parent this GameObject to your prefab and do some script magic at runtime.
For high fidelity text, either decrease the character size or increase the font size or do both.
Note: A larger character size reduces fidelity, but will take less processing power.
Source: Unity 5.2.0f3

Develop 2D game Inside Canvas Scaler

I'm new in Unity and i've realized that it's difficult do a multi resolution 2d game on unity without paid 3rd plugins available on Asset Store.
I've made some tests and i'm able to do multi resolution support in this way:
1- Put everything from UI (buttons etc) inside a Canvas object in Render Mode Screen Space - Overlay with 16:9 reference resolution and fixed width.
2- Put the rest of the game objects inside a Game Object called GameManager with the Canvas Scaler component in Render Mode Screen Space - Camera with 16:9 reference resolution, fixed width and the Main Camera attached. After that, all game objects like player, platforms etc inside GameManager need to have a RectTransform component, CanvasRenderer component and Image Component for example.
Can i continue developing the game in that way, or this is a wrong way to do the things?
Regards
Also don't forget GUI, Graphics. It's a common misconception that GUI it's depreciated and slow. No it's not. The GameObject helpers for GUI were bad and are depreciated, but the API for putting in OnGUI works great when all you need is to draw a texture or some text on a screen. They're called legacy, but there are no plans as to remove them, as the whole Unity UI is made out of it anyway.
I have made a few games just on these, using Unity as a very overengineered multiplatform API for draw quad.
There is also GL if you want something more.
Just remember - there will be no built-in physics, particle effects, path finding or anything - just a simple way to draw stuff on the screen. You will have total control over what will be drawn - and this is both a good and bad thing, depending on what you want to do.
I will not recommend you using Canvas Scaler for developing a complete game. Intended purpose of the canvas scaler was to create menus and you should use it to create menus only.
The 2D games created without the canvas scaler don't create much problems (mostly they don't cause any problems) on multiple resolutions.
So, your step 1 is correct but for step 2 you don't need to have a canvas scaler component attached.
Do remember to mark your scene as 2D (not necessary) and your camera to orthographic (necessary) while developing 2D games.