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 /
This question was closed Nov 25, 2016 at 07:47 PM by dracolique for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by dracolique · Nov 25, 2016 at 07:57 AM · terrainspritetexture2d

How do I make sure my sprites are fitted properly to my textures?

I'm working on an orthographic top-down 2D sprite game, and I finally have my world working the way I want... except for one small thing: for some reason it looks like the sprites and textures aren't aligning properly for my world terrain tiles.

I noticed that the world terrain tiles weren't lining up properly, so I scaled them down to 0.975 * their normal size and added a 50 pixel border to the texture so I could more clearly see what's going on. See the attached image.... you can clearly see that the tiles are lining up properly in the world (the thin white line is the space between them due to being scaled down).

But that border, tho... what gives? Why are the textures not aligned properly? Image, texture and sprite are all created at perfectly square 1024x1024 (I've doubled checked this dozens of times). So why are things not lining up properly?

You may notice in the code that I'm loading my images in as a byte[]. This is because I'm reading them straight from a .zip file to a memory stream (.zip -> memory -> texture2d -> sprite). Could doing it this way cause anomalies, or am I just missing something obvious here?

Obviously I could simply adjust the offset of the texture relative to the sprite... if I knew how to do that, but I can't seem to figure it out.

  e = zip["s" + sectorID.ToString("D3") + "_" + (tileID+1).ToString("D3") + ".jpg"];
 
             if (e == null)
             {
                 Debug.Log("s" + sectorID.ToString("D3") + "_" + (tileID+1).ToString("D3") + ".jpg does not exist!");
                 return;//Don't be tryin' to draw no images that don't exist, fool!
             }
 
             MemoryStream data = new MemoryStream();
             e.Extract(data);
             byte[] imageBuffer = data.ToArray();
 
            
             Texture2D tex = new Texture2D(tileSize, tileSize);
             tex.LoadImage(imageBuffer);
 
             int borderWidth = 50;
             var borderColor = new Color(1, 0, 0, 1);
 
             //Nasty slow code for adding 50px red border.  For testing purposes only!
             for (var x =0; x < tex.width; x++) {
                 for (var y = 0; y < tex.height; y++) {
                     if (x < borderWidth || x > tex.width - 1 - borderWidth) tex.SetPixel(x, y, borderColor);
                     else if (y < borderWidth || y > tex.height - 1 - borderWidth) tex.SetPixel(x, y, borderColor);
                 }
             }
 
             tex.Apply();            
 
             //Create the sprite and apply to renderer----top,left,width,height--top left pivot, scale = 100 pixels per world unit  
             Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tileSize * 0.975f, tileSize * 0.975f), new Vector2(0f, 1f), 100f);           
         
 
             //Store it in the buffer for later use.
             sBuffer.loadedSprites.Add(tileID, sprite);            
 
             renderer.sprite = (Sprite)sBuffer.loadedSprites[tileID];

alt text

hairpull.png (53.6 kB)
Comment
Add comment · Show 1
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 dracolique · Nov 25, 2016 at 06:38 AM 0
Share

Ok so after posting this I did a little more digging on setting sprite offsets, and found the following:

http://answers.unity3d.com/questions/605905/dynamic-offset-tiling-for-spriterenderer.html

A quote from that thread:

Sprites are designed with the assumption that you define a fixed texture area for the Sprite - so the texture offset feature is removed from SpriteRenderer. However UV hacks can still be done in a vertex shader.

If there's nothing wrong with my existing code and I do end up having to adjust UV offsets, then I guess I'll either be writing a shader to do it or converting my sprites to 3D planes so I can set the offsets with $$anonymous$$imal hassle.

1 Reply

  • Sort: 
avatar image
0

Answer by Gmotagi · Nov 25, 2016 at 09:54 AM

Have you tried adjusting the pixels per unit value on the actual sprite to match the scale of your world?

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 dracolique · Nov 25, 2016 at 07:19 PM 0
Share

Yes, everything is set to 100 pixels per unit (default). Adjusting this value on the tiles just scales them relative to everything else.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a gameObject with a dynamically generated sprite? 0 Answers

Load Unity 4.3 Sprites with AssetBundles 5 Answers

Why sprites don't load? 0 Answers

Save generated Texture2D as Texture type Sprite 1 Answer

Polygon Collider 2D Not Precise Enough 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