Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PhoenixBlackReal · Jul 27, 2014 at 04:54 PM · scalesprites

How to have a sprite with fixed size and position relative to screen resolution?

I've been going through this for quite a while now. Say I have a sprite. And it looks just right in 1366x768 resolution. When I change the resolution - the sprite changes shape and is in a slightly different position. I've worked with OnGUI and already know how to keep the position, scale, etc. just right, but this is not the same deal. So, on to the main question - what would be the code that, once attached to the sprites, locks it's location, size and scale, and looks good on every resolution, plus - the sprite itself still looks good and not pixelated. I'm using C# and I know this is quite a big and complex code, but if anyone would at least go as far as lead me on the right track - I'd be grateful.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 27, 2014 at 06:29 PM 0
Share

I cannot think of a reason why the Sprite would be changing shape. In Unity, the camera see the same number of units vertically no matter what the resolution, and a Sprite is a world object. Unity scales the horizontal of what the camera sees to fit the aspect ratio. So the what the camera sees will be larger or smaller than what you originally authored for depending on the aspect ratio of the display. So your Sprite doesn't really move around in world space. If you are trying anchor the sprite relative to the camera view (i.e. bottom left corner for example), you will find many posts that tackle this problem. Usually they involve mapping either screen coordinates or viewport coordinates to world coordinates (see Camera.ScreenToWorldPoint() and Camera.ViewportToWorldPoint()). As for looking good, you can only author 'pixel perfect' for one resolution. That is in only one resolution will the size be right and the pixels of the image match the pixels of the display perfectly. Generally reducing the size of image has less visual problems than increasing the size of the image.

avatar image PhoenixBlackReal · Jul 27, 2014 at 06:43 PM 0
Share

How do I then have a fixed size of it? I tackled this again and the shape is not stretched and remains the same, but on lower resolution the sprite does appear bigger. It should remain constant in all resolutions.

avatar image robertbu · Jul 27, 2014 at 07:17 PM 0
Share

World objects should remain vertical size as a fraction of the screen size. That is, the vertical size seen by the camera should remain the same (barring any scripts that are making changes). Smaller screen will be smaller size of all world objects, but the vertical view should remain the same. So if you put a world object centered at the top and centered at the bottom of the screen, they should stay in the same place as you move from resolution to resolution. If you are not seeing this, then I'm at a loss. $$anonymous$$aybe you have some code making changes, or there is something special about sprites vs other world game objects that I'm not aware of.

P.S. There may be an issue with pixel density horizontal vs vertical across devices. That is, if you had two screens that were both 1366 x 768 but one was physically longer horizontally than the other, then you would see a bit of stretching. Just a theory, but if true, Screen.dpi might be something to look at.

avatar image PhoenixBlackReal · Jul 27, 2014 at 08:07 PM 0
Share

I do work with dual monitors, but I checked on both - the resolution change means that my sprite either gets stretched or it changes size. I'm making a main menu for the game, which is to be resolution independent. I made a HUD with OnGUI and it works just fine - all the textures in the same place, looking crisp as ever. But for the main menu, which might include even 3D objects, I tried using sprites - as I said, the height and position eventually remained the same, but they would start stretching. If the stretching would stop (random playing around with values), then the size would change. Now I've tried the NGUI, it seemed promising, yet with it I encountered pretty much the same problems... I hope this makes sense. In any case - I really value your help, thank you.

avatar image PhoenixBlackReal · Jul 27, 2014 at 08:11 PM 0
Share

$$anonymous$$aybe there isn't any real way to go around this? I mean, many games have rather bad looking menus if you play them on very low resolution, and then again - who would play a game on 600x400 or whatever these days. But maybe I'm just making excuses, it's been hell of a long day.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by PhoenixBlackReal · Jul 29, 2014 at 12:59 PM

To those who still look for an answer and this might help - I solved the problem. Add this line to the script on the sprite:

 sprite.transform.localScale = new Vector3(Screen.width/1366f, Screen.height/768f,  1);

The sprite should give pixels to units: 1.

The orthographic camera should have this script attached:

 camera.orthographicSize = Screen.height / 2;

Once played the sprite stays the same size and looks quite good even in the lowest resolution. Hope this helps those who need to have a fixed size sprite looking crisp and resolution independent!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefab GameObject is rendering(?) at a different scale per scene 2 Answers

2d Sprites automatically round their scale to the nearest hundred 1 Answer

How To Find Required Sprite Dimensions 0 Answers

Does the Z-Scale affect performance for sprites? 0 Answers

Perfectly scaled sprite menu in every resolution 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges