How To Fit Your Game For Every Resolution In Unity

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024
  • НаукаНаука

Комментарии • 59

  • @kecajable
    @kecajable 2 года назад +10

    Very good script, exactly what I was looking for :) I had to remove the multiplication by 100 but besides that works perfectly.

    • @idoasraff8513
      @idoasraff8513  2 года назад +6

      I'm so glad I got to help someone! would be happy to see your game once it's ready!

  • @imbestpumpkin
    @imbestpumpkin Месяц назад

    Very cool, works for me just remove multiplication by 100, also you can make scale X: 28.125 and Y: 50

  • @qazisaad5713
    @qazisaad5713 2 месяца назад +2

    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
      @CodeNinjasGaming58 2 месяца назад

      Please elaborate in full detail so I can understand and implement it into my game. Thank you

    • @qazisaad5713
      @qazisaad5713 2 месяца назад

      @@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.

    • @mahersabit9265
      @mahersabit9265 2 месяца назад +1

      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

    • @rabomeister
      @rabomeister Месяц назад

      @@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.

    • @StudsterPh
      @StudsterPh 5 дней назад

      LOVE YOU BRO

  • @modemfish
    @modemfish 2 года назад +4

    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);

    • @idoasraff8513
      @idoasraff8513  2 года назад

      That's awesome! Would love to see your game when it comes out

  • @MrFaciio
    @MrFaciio 2 года назад +5

    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.

    • @idoasraff8513
      @idoasraff8513  Год назад

      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

  • @frantisek6560
    @frantisek6560 5 месяцев назад

    so underrated video. Thank you very much!

  • @darredYouTube
    @darredYouTube Год назад +1

    Idk it doesnt work for me. Idk what i did wrong... Like my Ui its not shown on my game screen

  • @dertobbe1176
    @dertobbe1176 3 месяца назад +1

    3:11 "I am not very good at english..."
    Proceeds to Code in english 😅

  • @aribsalehin7074
    @aribsalehin7074 4 месяца назад +1

    man, i love u, i simply love u

  • @Ferenc-Racz
    @Ferenc-Racz 2 месяца назад

    Thank you very much!

  • @GokdenizCetin
    @GokdenizCetin Год назад +2

    Thank you!

    • @idoasraff8513
      @idoasraff8513  Год назад +2

      You're welcome! Would be happy to check out your game when it's out!

  • @ghauswood9594
    @ghauswood9594 2 года назад +1

    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

    • @idoasraff8513
      @idoasraff8513  2 года назад

      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!

    • @ghauswood9594
      @ghauswood9594 2 года назад +1

      @@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

    • @idoasraff8513
      @idoasraff8513  2 года назад +1

      @@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

    • @ghauswood9594
      @ghauswood9594 2 года назад +1

      @@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

    • @idoasraff8513
      @idoasraff8513  2 года назад

      @@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!

  • @optus231
    @optus231 Год назад +1

    Nice tutorial, what a pity that it is only for orthographic cameras

  • @billbuckman4632
    @billbuckman4632 Год назад

    Great Video, thank you! Question: can this script be used on a Canvas object and still work??

    • @oddshorts-is5df
      @oddshorts-is5df Год назад +2

      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 ;)

  • @soyrandom1
    @soyrandom1 2 года назад +1

    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?

    • @idoasraff8513
      @idoasraff8513  2 года назад +3

      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 ;)

  • @alice0067
    @alice0067 2 месяца назад

    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);
    }
    }
    }

  • @abnormalaestheticsstudio
    @abnormalaestheticsstudio Год назад

    dude microphone volume is ultra low please high your volume of mic beacuse i heard very queit what is you said

  • @swepo5719
    @swepo5719 Год назад +1

    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

    • @idoasraff8513
      @idoasraff8513  Год назад

      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

  • @somerndguy
    @somerndguy 7 месяцев назад

    That does not work with canvases, does you have any other way to deal with that?

    • @marmandopro2930
      @marmandopro2930 3 месяца назад

      did you find any solution? :c

    • @somerndguy
      @somerndguy 3 месяца назад

      ​@@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.

  • @halsharif1984
    @halsharif1984 Год назад

    is this code work for 3d game ?

  • @Ilnur-v6z
    @Ilnur-v6z Год назад

    Does this work with tilemaps?

  • @SkorpionYassine
    @SkorpionYassine Год назад

    Where are u from?

  • @28Blessed
    @28Blessed 3 месяца назад

    היי עידו הסתבכתי קצת יש מצב אתה עוזר לי?

    • @idoasraff8513
      @idoasraff8513  3 месяца назад

      אשתדל לעזור, מה הבעיה?

  • @siRrk1337
    @siRrk1337 11 месяцев назад

    perfect

  • @TechWrathTV
    @TechWrathTV Год назад

    mate you need to improve your voice, the sound is soo low,

  • @KBforJesusChrist
    @KBforJesusChrist Год назад +1

    When the screen is small they squish

    • @idoasraff8513
      @idoasraff8513  Год назад

      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

  • @LuizGamingYT
    @LuizGamingYT Год назад +1

    AAAAAAAAAAAAAAAAA Thank you!

  • @MayapathiArts
    @MayapathiArts Год назад

    Informational

  • @Krystian-i1o
    @Krystian-i1o Год назад

    i cant hear anything!!!!!!!

  • @popik7918
    @popik7918 Год назад +1

    Класс

  • @sakaring
    @sakaring 10 месяцев назад

    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.

  • @TunggTomm
    @TunggTomm 15 дней назад

    yy

  • @itimesplitters1542
    @itimesplitters1542 Год назад +1

    Thank you a lot!!