Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by sndark50 · Jan 22, 2019 at 10:35 PM · spriterenderingspritessizespriterenderer

How to set sprite size in pixels?

I'd like to set sprite size in pixels (not in world units) by using scripts. I switched a camera projection to orthographical and tried setting scale in sprite renderer and pixels per unit in sprite to achieve desired size, but the final result is always wrong. Any idea or an example of how should I do it?

My code:

 Component map = GameObject.Find("Map").GetComponent<Component>();
         Texture2D texture = Resources.Load<Texture2D>("test");
         Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0f, 0f), 1); // pixels per unit = 1
         GameObject gameObject = new GameObject();
         SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
         spriteRenderer.sprite = sprite;
         gameObject.transform.SetParent(map.transform);
         spriteRenderer.transform.localPosition = new Vector2(0, 0);
         spriteRenderer.size = new Vector2(1, 1);

Main camera inspector screenshot: alt text Canvas inspector screenshot: alt text

s1.png (35.2 kB)
s2.png (40.9 kB)
Comment
Add comment · Show 2
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 Klarzahs · Jan 23, 2019 at 10:51 AM 0
Share

Could you post your code? The approach sounds fine to me, maybe we'll find a bug :)

avatar image sndark50 Klarzahs · Jan 23, 2019 at 12:58 PM 0
Share

I added a piece of code and screenshots,

2 Replies

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

Answer by bpaynom · Jan 23, 2019 at 01:15 PM

From Unity Docs (OrthographicSize) The orthographicSize is half the size of the vertical viewing volume.

With this, if you want to put real pixels size for your sprite renderers:


  1. In the Import Settings, set Pixels per Unit to 1.

  2. Set the Camera Projection to Orthographic.

  3. Set the Camera Orthographic Size to half the height of the screen height.


That's of course for testing purpose. If you want it for multilple resolutions you should do it via script. Remember, the orthographic size half the height of the screen height.


If you dont want none of above, you could let the default Pixels Per Unit (100) and default Orthographic size (5). Then via script, scale your world of your desired sprites according to what I mentioned before.

Comment
Add comment · Show 2 · 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
avatar image sndark50 · Jan 24, 2019 at 04:08 PM 1
Share

Is a good idea to disable canvas scaler?

avatar image tormentoarmagedoom sndark50 · Jan 24, 2019 at 04:59 PM 0
Share

The component? No

avatar image
0

Answer by sndark50 · Jan 25, 2019 at 11:48 AM

Finally, I got it. I put working code for others who will have a similar problem.

         GameObject map = GameObject.Find("Map");
         Texture2D texture = Resources.Load<Texture2D>("test");
         var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0f, 0f), 1);
         GameObject gameObject = new GameObject();
         SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
         spriteRenderer.sprite = sprite;
         gameObject.transform.SetParent(map.transform);
         spriteRenderer.transform.localScale = Vector2.one;

The order is very important. If you change localScale before setting gameObject parent then sprite rendering will be wrong. All settings (camera, canvas) are like on screenshots in the question.

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

147 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

keep sprite same size when changing 1 Answer

How to change spriterenderer to everyone with RPC 0 Answers

Render sprite on sprite ignoring alpha? 1 Answer

[2D] Sprite size issue (same sprite differente sizes in Tilemap and world),[2D] Issue with Sprite size (same sprite, differentes sizes in Tilemap and in World) 1 Answer

Why is my sprite not the original resolution? 2 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