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
1
Question by Stone-Legion · Dec 06, 2013 at 12:41 PM · c#instantiatetransformspriterenderer

When setting a transform size with attached SpriteRenderer, why is this not the actual pixel size?

For example, I have a Transform with SpriteRenderer attached. The transform size is set to Vector3(3, 3, 0).

Now I have a loop that runs through as the code below shows, but these instantiated objects always have gaps and spaces between them. What am I missing?

 for(int x = 0; x < 100; x++)
 {
     for(int y = 0; y < 100; y++)
     {
         Vector3 pos = new Vector3(this.transform.position.x+(x * 3),this.transform.position.y+(y * 3), 0);
         GameObject land = (GameObject)Instantiate(landObject,pos,this.transform.rotation);
     }
 }


Thanks to anyone who replies with an answer!

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
1

Answer by Stone-Legion · Dec 06, 2013 at 01:08 PM

Discovered the solution on my own.

For anyone wondering, its quite easy. I had to use renderer.bounds.size.x and y to do the trick.

         for(int x = 0; x < 25; x++)
         {
             for(int y = 0; y < 25; y++)
             {
                 Vector3 pos = new Vector3(this.transform.position.x+ (x *landObject.transform.renderer.bounds.size.x),
                                           this.transform.position.y-( y *landObject.transform.renderer.bounds.size.y), 
                                           0);
                 GameObject land = (GameObject)Instantiate(landObject,pos,this.transform.rotation);
             }
         }
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
avatar image
0

Answer by Tomer-Barkan · Dec 06, 2013 at 01:17 PM

As in all other unity objects, the transform's scale is relative to the model size. In case of sprites, the model size is the size of the texture. By default, the 2D mode uses 1 meter = 100 pixels, so if you have a 100x100 pixel texture, and the scale is (1 , 1) it will occupy 1 meter of world space.

If on the other hand you have a 200x200 pixel texture in your sprite, and the scale is (1 , 1), the size of the object in the world will be 2 meters.

So to calculate the world size of the sprite (assuming you left the default 100 pixels per unit when importing the sprite):

 Texture2D spriteTexture = landObject.GetComponent<SpriteRenderer>().sprite.texture;
 Vector3 spriteSize = new Vector3(spriteTexture.width / 100f, spriteTexture.heigh / 100f, 0);

Then you can use this instead of 3:

 for(int x = 0; x < 100; x++)
 {
     for(int y = 0; y < 100; y++)
     {
         Vector3 pos = new Vector3(this.transform.position.x+(x * spriteSize.x),this.transform.position.y+(y * spriteSize.y), 0);
         GameObject land = (GameObject)Instantiate(landObject,pos,this.transform.rotation);
     }
 }
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 Stone-Legion · Dec 10, 2013 at 08:09 AM 0
Share

This seems to cause the sprites to spread apart even further. I'll try modifying it a bit as I think your approach is more correct if it is written properly - $$anonymous$$e causes the sprites, while side by side, to show half a pixel off at some points. Thanks.

avatar image Tomer-Barkan · Dec 10, 2013 at 01:06 PM 0
Share

Don't forget to check that the setting is indeed that 100 pixels = 1 meter in the world. This is defined in the sprite importer.

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

17 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

Related Questions

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 0 Answers

Random Object spawning with transforms 2D 1 Answer

Object instantiate, Projectile problems 2 Answers

Rotating a certain axis offsets the other ones? 1 Answer

Instantiated projectile always floats up 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