Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 HelpMeGame · Nov 14, 2019 at 11:32 PM · spriteunity 2dsprites

Setting a sprite's size with pixels

I am relatively new to C#, but I know my way around most of the basics. I know this question has been asked many times, and, after nearly 2 hours of searching I still can't seem to find an answer that works, so please bear with me.

I'm creating a 2D Unity game for iOS, and I need to be able to get the size of the screen, (I know how to do this, it is Screen.width / Screen.height) and with that, set the size of a sprite. I'm trying to get 7 sprites (width-wise) on the screen, but I need it to adjust with the device. Hard coding numbers won't do here. So, to achieve this, I need a way to scale by pixels, which I'm not sure how to do, or if it is even possible with Unity. If there is any other way, please let me know.

(Yes, I have tried using Rect and RectTransform, it either doesn't do what I need, or I'm using it wrong. could be either.)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by revolute · Nov 15, 2019 at 02:55 AM

Use canvas scaler. The whole canvas is scaled to fit reference resolution. Depending on what you need, make it match width or height. After that, any sprite you put in your canvas will be "pixel" size.

Comment
Add comment · Show 1 · 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 HelpMeGame · Nov 16, 2019 at 01:24 AM 0
Share

@revolute I'm not sure I understand what you mean. $$anonymous$$ay you elaborate a little more please?

avatar image
0

Answer by HelpMeGame · Nov 20, 2019 at 11:41 PM

I figured it out, I had to use a combination of math functions that determined the width of the screen, they I had to perfect some of the numbers from there.

Here is a general idea of what works:

 //Declaration Of Variables
 float blockSize = Screen.width / 8.75f;
 float blockDistanceX = Screen.width / 8.75f;
 void Start()
 {
     //Extra variables that couldn't be declared earlier
     float xValue = blockDistanceX * -1f * 3.8f;
     float layerPos = -1500;
 
     //Creates new Block
     GameObject block = Instantiate(objectPrefab, new Vector3(0, 0, 1), Quaternion.identity, parent: canvas);
     block.transform.localScale = new Vector2(blockSize, blockSize);
     block.transform.localPosition = new Vector2(xValue, layerPos);
 }
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

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

Related Questions

Unity 2D Sprites cost many draw calls 1 Answer

Unity Game View won't refresh my sprites 1 Answer

Sprites and level tiles overlapping when they shouldn't 0 Answers

Unity 2D Sprite is invisible. 2 Answers

Sprite.Create makes invisible sprites, even when nothing is changed. 1 Answer


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