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
0
Question by GanonvsLink5 · Nov 28, 2014 at 07:14 AM · spritemaptile

Converting single sprite into Multiple

Hi guys, currently I have a single map that I want to split into 16x16 pixels and automatically put it (the entire thing) in the scene. I've been doing it manually up until now by splicing it and using a tile editor to place them together, but I'm really dying for an automatic solution. Is there a way to do this without spending money? I still want the 16x16 tiles to be considered separate game objects. Thanks

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 GanonvsLink5 · Nov 28, 2014 at 07:37 PM 0
Share

Thank you for the response. The part I'm having trouble with is the bulk drag'n'drop of many sprites at once onto the scene, while keeping them as separate game objects. When I try to drag more than one sprite onto the scene, it only brings the first sprite selected. I'm just wondering if there's a software that will directly import the map picture I have into the scene while keeping it into separate 16x16 game objects.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Immanuel-Scholz · Nov 28, 2014 at 07:42 AM

What part makes you the trouble? The splitting of images should be quite fast if you use the auto-split function of unity (clickme and scroll down until "Automatic slicing"). 20 images should take around 5-10 minutes max..

For arranging the images into the scene, you know about the grid snapping, right? (Edit/Snap settings will edit the grid size and dragging while holding down Ctrl/Command key will snap while when you move). That makes it quite easy to manually place objects in a fixed grid. Remember that you can first place all the tiles in a grid anywhere and then move/rotate them together if they are not spot-on as a group.

Remember that many functions like changing properties (like the single/multiple sprites setting) can be executed on multi-selections. You can also bulk-drag'n'drop many sprites in one go onto the scene (and then move them around row-by-row).

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 GanonvsLink5 · Nov 28, 2014 at 03:19 PM 0
Share

So I've figured out that it's probably a good idea for me to use a single image as a map, and split that into an array of separate game objects, but I'm having a little trouble as I'm pretty new to unity.

avatar image
0

Answer by koray1396 · Nov 28, 2014 at 04:01 PM

Check Sprite.Create

Sample would be like;

 Sprite[,] TileSprites;
 public int NumberOfColumns, NumberOfRows;
 public Sprite SourceSprite;
 
 void SliceImage(){
     int PixelsToUnits = SourceSprite.texture.height / NumberOfRows;
     TileSprites = new Sprite[NumberOfColumns, NumberOfRows];
     for(int i = 0; i < NumberOfColumns; i++){
         for(int y = 0; y < NumberOfRows; y++){
             Rect theArea = new Rect(i * PixelsToUnits, y * PixelsToUnits, PixelsToUnits, PixelsToUnits);
             TileSprites[i,y] = Sprite.Create(SourceSprite.texture, theArea, Vector2.zero, PixelsToUnits);
         }
     }
 }

edit: the above would create square tiles, and it requires that source texture resolution is divisible by both column number and row number. You can change it accordingly to your needs.

Comment
Add comment · Show 9 · 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 GanonvsLink5 · Nov 28, 2014 at 04:40 PM 0
Share

Thank you, this is very helpful! I'm trying to find a method that lays out the sprites onto the scene through the API, but I'm not finding anything. Also, I renamed the background I want to use to "CurrentImage" but it still gives me the error 'The name CurrentImage does not exist in this context', do you know what I can do to fix this? I'm sorry, I'm really new to Unity.

avatar image koray1396 · Nov 28, 2014 at 05:37 PM 0
Share

Sorry about that, I just edited, cannot test it now.

So you now only need to assign values to NumberOfColumns and NumberOfRows, and assign a sprite to SourceImage in the editor.

if you want to actually use these sprites on the scene, the method would be like.

 Sprite[,] TileSprites;
 public int NumberOfColumns, NumberOfRows;
 public Sprite SourceSprite;

 void SliceImage(){
     int PixelsToUnits = SourceSprite.texture.height / NumberOfRows;
     TileSprites = new Sprite[NumberOfColumns, NumberOfRows];
     for(int i = 0; i < NumberOfColumns; i++){
         for(int y = 0; y < NumberOfRows; y++){
             Rect theArea = new Rect(i * PixelsToUnits, y * PixelsToUnits, PixelsToUnits, PixelsToUnits);
             TileSprites[i,y] = Sprite.Create(SourceSprite.texture, theArea, Vector2.zero, PixelsToUnits);
             GameObject tile = new GameObject("Tile-"+i.ToString()+"-"+y.ToString());
             SpriteRenderer spr = tile.AddComponent<SpriteRenderer>();
             spr.sprite = TileSprites[i,y];
             tile.transform.position = new Vector3(i,y);
         }
     }
 }


Also forgot to mention that this creates unit tiles, where height and width are 1, if i am not wrong.

avatar image GanonvsLink5 · Nov 28, 2014 at 05:43 PM 0
Share

Or is there even a tiling software that includes an option for tiling an entire image ins$$anonymous$$d of piece by piece tiling?

avatar image koray1396 · Nov 28, 2014 at 05:49 PM 0
Share

the above will create and place gameobjects, in respect to the original texture. what else do you need?

avatar image GanonvsLink5 · Nov 28, 2014 at 05:55 PM 0
Share

Oh, I didn't see your previous post! Thank you!!

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

tilepalette problems 0 Answers

Draw 2d map array with SetTile or Instantiate ? 0 Answers

Infinite map size: respawn you at the other end? 1 Answer

Best way to do tiling in 2D 1 Answer

Get sprite of Tile 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