Guys i saw a thousand videos, i asked Ai tools and everything but all of this doesnt works, now lemme tell u ITS SUPERRR SIMPLE how i figured it out. So the thing is that unity follows the values in inspector, it doesnt follows the initial values in the script. So IN UR INSPECTOR, in ur canvas screenspace -overlay OR canvas - screen space camera, first set to scale with screen size, then u just have to set the REFERENCE RESOLUTION to what u want and screen match mode to match width or height and Match accordingly 0 to 1. Thats it, that reference resolution will already be then set to whatever screen u play the game on when exported. For example i set my reference resolution to 770x570 in inspector, now that i've exported the game i can play the game on 1920x1080 without having the game objects/ui elements change or go out of screen, they appear with perfect size and adjust appropriately.
@@CodeNinjasGaming58 u probably have canvases made in ur heirarchy, whenever u make a canvas unity automatically sets its reference resolution to 800x600. U have to go to canvas's inspector and select Ui scale mode to "scale with screen size", and then set the "reference resolution" to the resolution u want the game to be exported in, select screen match mode to "match width or height" and "match" 0.5. Apply this for all of the canvases that use render mode as screenspace overlay/camera and it'll all be fixed.
you sir reminded me a crucial step, i was forgetting when making my project, which i previously struggled with, i did everything but making it to scale with screen size
@@qazisaad5713 The problem with that, it scales the UI elements, in most of the cases like you explained, it is for Canvas. However, if you have a random gameObject with a sprite (for example), it will not be scaled. Currently I am working on 9:16, and Unity can scale the Canvas really good to different iOS devices. Yet, it does not scale my GameBoard, which is a GameObject with Board sprite. Therefore, you need to scale (with scripts) your objects. If I am wrong, please tell me because I want to be wrong so much, since it would help me to stop working on shitty ratio scripts.
your video really helped me a lot. i followed the concept but changed it per my needs. i developed my app using a display 1080 Wx2340 H so that becomes the ratio denominator. then if the New screen is wider than it is taller use the h_ratio otherwise use w_ratio and then scale the object you need accordingly float w_ratio = Screen.width / 1080f; float h_ratio = Screen.height / 2340f; float use_ratio = w_ratio; if (h_ratio < w_ratio) use_ratio = h_ratio; GameObject.Find("PopUp_Panel").transform.localScale = new Vector3(use_ratio, use_ratio, 1);
Doesn't work for me :( I have sprites and raw images on scene, portrait mode, scale with screen size 1080x1920 match with width and i cant setup it properly to scale my all objects to diffrent resolution :( I tried everything.
Its not working for me, plz help the problem is that I create a game object with this script but no gray bg appears on which I could put my child objects
Hello! First of all, I'm glad you're using my tutorial and I'd love to see your game once it comes out :) Now to your question - the gray bg is just a sprite renderer I attached to the fit to workground game object for visualization. It is not necessary, but if you want it you can simply add a sprite renderer with a sprite of just one pixel to your game object and it will appear as in my video. Hope it helps, and if not let me know!
@@idoasraff8513 Yeah thx I just did that but there is another problem and that is that my fitthescreen is ok in 16:9 but my objects do not fit in 18:9, what is the solution for that. Like the screen should get smaller and have some bits left at the top and bottom but instead my objects have their left and right parts cut. All I did was change the * 100 from the code
@@ghauswood9594 I'm not sure what's wrong. Is the workground itself going off bounds, or only the objects inside it? Of your script finds both corners correctly than the workground should be fine, and so should it's children objects. Try debugging to find if the corners are correctly detected
@@idoasraff8513 the corners are the problem actually, when I do 16:9 then it is ok but when I do 18:9 then the corners go out off bounds so what did I do wrong with my script
@@ghauswood9594 That's weird. Since I cannot run code on your machine, I can only offer suggestions. Since the code is so short and simple, try understanding exactly how the code works and play with it a bit, and try to figure out how your corners are detected and why do they come out wrong. Good luck!
It should come with the canvas when you add it in Use the Canvas Scaler: Unity's Canvas Scaler component allows you to adjust the scale of the canvas and its UI elements based on the size of the screen. You can set the scaling mode to "Scale with Screen Size" and set the reference resolution to the size of your canvas. This will ensure that the UI elements maintain their relative size on different screen sizes. Hope it helps friend ;)
Hello! I'm sorry but this solution is for world-space only, and does not work well for canvases. Canvases have their own API for fitting to every resolution, look it up ;)
i had to remove the multiplication by 100 otherwise it was becoming 1000 on scale. When i was trying to scale it on a landscape mode the width wasnt properly adjusted only the height was. but i found a solution, I hope it works anyone who had the same problem as me. using UnityEngine; public class Test : MonoBehaviour { [SerializeField]private Camera mainCam; private void Start() { transform.position = new Vector3(mainCam.transform.position.x, mainCam.transform.position.y, 0); Vector3 bottomLeft = mainCam.ViewportToWorldPoint(Vector3.zero); Vector3 topRight = mainCam.ViewportToWorldPoint(new Vector3(mainCam.rect.width, mainCam.rect.height)); Vector3 screenSize = topRight - bottomLeft; Debug.LogError(screenSize.ToString()); if(transform.localScale.x < screenSize.x || transform.localScale.x> screenSize.x) { transform.localScale = new Vector3(screenSize.x, screenSize.y, 1f); } } }
I'm sure you're not dumb, gamedev is just hard. If you need any help with anything specific ask me, and I would gladly check out your game once it's out
@@marmandopro2930 you are gonna have to use the anchor presets in the rect transform in the inspector. you will need to set that to each object relative to the screen and other objects I believe. for me I had to make sure it look well on 16:9 and 18:9. it is best also to test it on free aspect, just play with the aspect ratio and see how the objects react and change positions. I also recommend trying horizontal and vertical layouts, they might help. look for some tutorials on youtube.
the script does not change the size of your objects, so that makes sense. if it's problematic to you consider making smaller objects/put them farther apart
Vector3 bottomLeft = maingcamera.ViewportToWorldPoint(Vector3.zero) * 100; Vector3 topRight = maingcamera.ViewportToWorldPoint(new Vector3(maingcamera.rect.width, maingcamera.rect.height)) * 100; I had to multiply it by 1 instead of 100 for it to work properly, but if I multiply it by 100, the object becomes larger than the camera.
Very good script, exactly what I was looking for :) I had to remove the multiplication by 100 but besides that works perfectly.
I'm so glad I got to help someone! would be happy to see your game once it's ready!
Very cool, works for me just remove multiplication by 100, also you can make scale X: 28.125 and Y: 50
Guys i saw a thousand videos, i asked Ai tools and everything but all of this doesnt works, now lemme tell u ITS SUPERRR SIMPLE how i figured it out. So the thing is that unity follows the values in inspector, it doesnt follows the initial values in the script. So IN UR INSPECTOR, in ur canvas screenspace -overlay OR canvas - screen space camera, first set to scale with screen size, then u just have to set the REFERENCE RESOLUTION to what u want and screen match mode to match width or height and Match accordingly 0 to 1. Thats it, that reference resolution will already be then set to whatever screen u play the game on when exported.
For example i set my reference resolution to 770x570 in inspector, now that i've exported the game i can play the game on 1920x1080 without having the game objects/ui elements change or go out of screen, they appear with perfect size and adjust appropriately.
Please elaborate in full detail so I can understand and implement it into my game. Thank you
@@CodeNinjasGaming58 u probably have canvases made in ur heirarchy, whenever u make a canvas unity automatically sets its reference resolution to 800x600. U have to go to canvas's inspector and select Ui scale mode to "scale with screen size", and then set the "reference resolution" to the resolution u want the game to be exported in, select screen match mode to "match width or height" and "match" 0.5. Apply this for all of the canvases that use render mode as screenspace overlay/camera and it'll all be fixed.
you sir reminded me a crucial step, i was forgetting when making my project, which i previously struggled with, i did everything but making it to scale with screen size
@@qazisaad5713 The problem with that, it scales the UI elements, in most of the cases like you explained, it is for Canvas. However, if you have a random gameObject with a sprite (for example), it will not be scaled. Currently I am working on 9:16, and Unity can scale the Canvas really good to different iOS devices. Yet, it does not scale my GameBoard, which is a GameObject with Board sprite. Therefore, you need to scale (with scripts) your objects. If I am wrong, please tell me because I want to be wrong so much, since it would help me to stop working on shitty ratio scripts.
LOVE YOU BRO
your video really helped me a lot.
i followed the concept but changed it per my needs. i developed my app using a display 1080 Wx2340 H so that becomes the ratio denominator.
then if the New screen is wider than it is taller use the h_ratio otherwise use w_ratio and then scale the object you need accordingly
float w_ratio = Screen.width / 1080f;
float h_ratio = Screen.height / 2340f;
float use_ratio = w_ratio;
if (h_ratio < w_ratio) use_ratio = h_ratio;
GameObject.Find("PopUp_Panel").transform.localScale = new Vector3(use_ratio, use_ratio, 1);
That's awesome! Would love to see your game when it comes out
Doesn't work for me :( I have sprites and raw images on scene, portrait mode, scale with screen size 1080x1920 match with width and i cant setup it properly to scale my all objects to diffrent resolution :( I tried everything.
I'm sorry it didn't work for you, I hope you found another way. if not, try looking at modenfish's comment, it might help
so underrated video. Thank you very much!
Idk it doesnt work for me. Idk what i did wrong... Like my Ui its not shown on my game screen
3:11 "I am not very good at english..."
Proceeds to Code in english 😅
man, i love u, i simply love u
love you too
Thank you very much!
Thank you!
You're welcome! Would be happy to check out your game when it's out!
Its not working for me, plz help
the problem is that I create a game object with this script but no gray bg appears on which I could put my child objects
Hello! First of all, I'm glad you're using my tutorial and I'd love to see your game once it comes out :)
Now to your question - the gray bg is just a sprite renderer I attached to the fit to workground game object for visualization. It is not necessary, but if you want it you can simply add a sprite renderer with a sprite of just one pixel to your game object and it will appear as in my video. Hope it helps, and if not let me know!
@@idoasraff8513 Yeah thx I just did that but there is another problem and that is that my fitthescreen is ok in 16:9 but my objects do not fit in 18:9, what is the solution for that. Like the screen should get smaller and have some bits left at the top and bottom but instead my objects have their left and right parts cut. All I did was change the * 100 from the code
@@ghauswood9594 I'm not sure what's wrong. Is the workground itself going off bounds, or only the objects inside it? Of your script finds both corners correctly than the workground should be fine, and so should it's children objects. Try debugging to find if the corners are correctly detected
@@idoasraff8513 the corners are the problem actually, when I do 16:9 then it is ok but when I do 18:9 then the corners go out off bounds so what did I do wrong with my script
@@ghauswood9594 That's weird. Since I cannot run code on your machine, I can only offer suggestions. Since the code is so short and simple, try understanding exactly how the code works and play with it a bit, and try to figure out how your corners are detected and why do they come out wrong. Good luck!
Nice tutorial, what a pity that it is only for orthographic cameras
Great Video, thank you! Question: can this script be used on a Canvas object and still work??
It should come with the canvas when you add it in
Use the Canvas Scaler: Unity's Canvas Scaler component allows you to adjust the scale of the canvas and its UI elements based on the size of the screen. You can set the scaling mode to "Scale with Screen Size" and set the reference resolution to the size of your canvas. This will ensure that the UI elements maintain their relative size on different screen sizes.
Hope it helps friend ;)
Hey, great solution!
I'm having a problem where i place a canvas inside of it and the elements dissapear, do you know whats going on?
Hello! I'm sorry but this solution is for world-space only, and does not work well for canvases. Canvases have their own API for fitting to every resolution, look it up ;)
i had to remove the multiplication by 100 otherwise it was becoming 1000 on scale. When i was trying to scale it on a landscape mode the width wasnt properly adjusted only the height was. but i found a solution, I hope it works anyone who had the same problem as me.
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField]private Camera mainCam;
private void Start()
{
transform.position = new Vector3(mainCam.transform.position.x, mainCam.transform.position.y, 0);
Vector3 bottomLeft = mainCam.ViewportToWorldPoint(Vector3.zero);
Vector3 topRight = mainCam.ViewportToWorldPoint(new Vector3(mainCam.rect.width, mainCam.rect.height));
Vector3 screenSize = topRight - bottomLeft;
Debug.LogError(screenSize.ToString());
if(transform.localScale.x < screenSize.x || transform.localScale.x> screenSize.x)
{
transform.localScale = new Vector3(screenSize.x, screenSize.y, 1f);
}
}
}
dude microphone volume is ultra low please high your volume of mic beacuse i heard very queit what is you said
Im making my first game, and it doesnt really work for me, maybe im dumb lolz ill save the video, it might be that my game is just scuffed xD
I'm sure you're not dumb, gamedev is just hard. If you need any help with anything specific ask me, and I would gladly check out your game once it's out
That does not work with canvases, does you have any other way to deal with that?
did you find any solution? :c
@@marmandopro2930 you are gonna have to use the anchor presets in the rect transform in the inspector. you will need to set that to each object relative to the screen and other objects I believe. for me I had to make sure it look well on 16:9 and 18:9. it is best also to test it on free aspect, just play with the aspect ratio and see how the objects react and change positions. I also recommend trying horizontal and vertical layouts, they might help. look for some tutorials on youtube.
is this code work for 3d game ?
Does this work with tilemaps?
Where are u from?
היי עידו הסתבכתי קצת יש מצב אתה עוזר לי?
אשתדל לעזור, מה הבעיה?
perfect
mate you need to improve your voice, the sound is soo low,
When the screen is small they squish
the script does not change the size of your objects, so that makes sense. if it's problematic to you consider making smaller objects/put them farther apart
AAAAAAAAAAAAAAAAA Thank you!
Informational
i cant hear anything!!!!!!!
Класс
Vector3 bottomLeft = maingcamera.ViewportToWorldPoint(Vector3.zero) * 100;
Vector3 topRight = maingcamera.ViewportToWorldPoint(new Vector3(maingcamera.rect.width, maingcamera.rect.height)) * 100;
I had to multiply it by 1 instead of 100 for it to work properly, but if I multiply it by 100, the object becomes larger than the camera.
yy
Thank you a lot!!
You're welcome!