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 muhtalha132 · Aug 13, 2018 at 06:03 AM · androidtransformscalewidthpixels

Set width and height of 2D gameobject in PIXELS

So I'm new to Unity and having programmed games before without a game engine I'm used to just being able to specify the width and height of any sprite I use in PIXELS. The problem in unity is that the scale property in transform doesn't have anything to do with how many pixels it is. I just want a simple way to be able to do gameObject.setWidth(500px) and gameObject.setHeight(400px). I've searched online and I can't find anything just a bunch of complicated answers that don't make sense.

Comment
Add comment · Show 19
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 Piyush_Pandey · Aug 13, 2018 at 06:50 AM 0
Share

As you started Unity, you will have to unlearn the pixel thing. 1 unit in Unity is 100px but i would suggest you to get a hang of new unity ter$$anonymous$$ology.

avatar image muhtalha132 Piyush_Pandey · Aug 13, 2018 at 07:16 AM 0
Share

I wanna set the width and height to be a certain percentage of the screen width or height so that my sprites are scaled appropriately for different resolutions. I feel that this is the best way to achieve that.

avatar image Piyush_Pandey muhtalha132 · Aug 13, 2018 at 07:26 AM 0
Share

If you are using Canvas UI then you have anchors and pivots. If you are using sprites then you can get the Screen.width and Screen.height and can set according to that.

Show more comments
avatar image GMihai · Aug 13, 2018 at 08:10 AM 0
Share

You can set the pixels per unit property on a sprite when you import it : alt text

Example: If you want to move a 1000px sprite on x by 100px with the default value of 100 pixels / unity unit you will need to move 1 unity unit -> transform.position.x = 1;

Also you can get device resolution with Screen.width and Screen.height and then change to the appropriate sprite size on start. Also check this answer for generating sprites with different pixels/unit : link text

Update : Sprite and texture generation based on device resolution sample attach it to an empty gameobject maybe this is what you need:

 using UnityEngine;
 
 public class Test : $$anonymous$$onoBehaviour {
 
     private Sprite sprite;
     private Texture2D texture;
     private SpriteRenderer rend;
 
     float prevwidth;
     float prevheight;
 
     private void Start()
     {
         rend = gameObject.AddComponent<SpriteRenderer>();
     }
 
     void Update () {
         if(prevwidth != Screen.width || prevheight != Screen.height)
         {
             GenTandS();
             prevwidth = Screen.width;
             prevheight = Screen.height;
             Debug.Log("Screen Width : " + prevwidth + " Height : " + prevheight);
             Debug.Log("Sprite size" + sprite.rect);
         }
     }
 
     void GenTandS()
     {

     texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);

    //helping grid not required
   //Here you can use texture.Resize(width, height);
     int cellSize = 100;
         for (int i = 0; i < texture.width; i++)
         {
             for (int j = 0; j < texture.height; j++)
             {
                 if ((i + 1) % cellSize == 0 ||
                     (j + 1) % cellSize == 0 ||
                     i == 0 || j == 0)
                 {
                     texture.SetPixel(i, j, Color.white);
                 }
                 else
                 {
                     texture.SetPixel(i, j, Color.black);
                 }
             }
         }
 
         texture.Apply();
 
         sprite = Sprite.Create(texture,
                      new Rect(0, 0, texture.width, texture.height),
                      new Vector2(0.5f, 0.5f), 100);
 
         rend.sprite = sprite;
 
     }
 }

spriteimport.png (12.2 kB)
avatar image muhtalha132 GMihai · Aug 13, 2018 at 04:18 PM 0
Share

No I'm not asking about moving or positioning I'm asking about SCALING. The pixels per unit are not gonna help with scale. Also, you said "Also you can get device resolution with Screen.width and Screen.height and then change to the appropriate sprite size on start." $$anonymous$$y question is HOW would I actually SET the sprite size on start? I wanna do this programmatically not in the editor.

avatar image GMihai muhtalha132 · Aug 14, 2018 at 07:42 AM 0
Share

Check the script I attached to the answer. You can also use Texture2D.Resize(width, height); function then Texture2D.Apply(); to resize your actual texture. After any pixel modifications you need to apply but please note that this is costly on performance so I would not recommend using it a lot. Is this what you meant by scaling?

avatar image muhtalha132 GMihai · Aug 14, 2018 at 04:29 PM 0
Share

Thanks for all the info! I would like to point out that your approach may work for a single sprite attached to a gameobject but what about a game object with many children? For example I have a character gameobject composed of legs, arms, head, etc child gameobjects. Now in the editor I can select this whole character and using the rect tool scale the whole thing up or down. However, again the problem comes with the fact that scaling uses these arbitrary numbers and not actual pixel values. So 1.5 in the x and y would scale it 1.5x it's original size but I don't even know it's original size since it's a gameobject composed of many children sprites arranged in different ways. I simply want to know the number of pixels that the rect encapsulates on a gameobject.

avatar image GMihai muhtalha132 · Aug 14, 2018 at 05:40 PM 0
Share

So to get this straight before I answer what you want to achieve is a pixel perfect 2D game?

Show more comments
avatar image Sonky108 · Aug 14, 2018 at 08:33 AM 0
Share

I think you can assume pixel values when designing UI elements, not using Sprite for GameObjects. Simply because it depends on your camera settings.

https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html

The manual here says that orthographicSize is a half of vertical view. I assume (not able to check it now) that when, for example, setting the property to 5, you get 10 units on a vertical axis and 17.77 (16:9 ratio) on horizontal. On sprite settings default 100 Pixels Per Unit (marked by @xphorsepower ) says that every 100 pixels of the image will take 1 unit.

$$anonymous$$nowing that and having for example Image with 500x500 size, putting that into object with scale 1, 1, 1 I will guess that it will take half of the screen on vertical axis.

I am not able to check it all above is true, but my thought is that using Sprite you cannot easily set width and height in pixels (but you can write your own implementation) because of orthographic size of the camera.

avatar image GMihai Sonky108 · Aug 14, 2018 at 10:29 AM 0
Share

@Sonky108 you are right about camera settings the`Camera orthographic Size` is required as we are talking about 2D but it sets your overall scale ratio / camera distance from the sprite itself. Example if you want to display a 100px sprite on the whole screen height orthographic Size must be set to 0.5 ergo if it`s a 1000px sprite set it to 5 assu$$anonymous$$g pixels / unit is set to 100. But this is what happens if you want to fill the screen with different sprite sizes : 100x100 resolution / Sprite 100x100px / Camera orthographic Size 0.5 alt text 1000x1000 resolution / Sprite 100x100px / Camera orthographic Size 0.5 alt text 100x100 resolution / Sprite 1000x1000px / Camera orthographic Size 5 alt text 1000x1000 resolution / Sprite 1000x1000px / Camera orthographic Size 5 alt text 16:9 ratio / 711px w / 400px h alt text 16:9 ratio / 1111px w / 625px h alt text The problem is actual size of the sprite/texture used if you don`t use the proper ration your performance will be trash. Ideally you need to have big textures to start with then resize them down in order to display them properly at the smallest size you can

avatar image Sonky108 GMihai · Aug 14, 2018 at 10:46 AM 0
Share

I have problems launching the images. Could you share them?

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Texture2D Width problem - C# Android school project 2 Answers

GUI Scale on Android 1 Answer

How to realize Image.fillAmount with Sprites in game? 0 Answers

How is localScale adjusted when re-parenting transforms? 2 Answers

I'm interpolating an object between positions and rotations. Now I need to make sure it happens in front of the camera. 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