• 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 csirmazbendeguz · Apr 13, 2017 at 03:21 PM · 2d2d gamedrawing

Modify pixels per unit of generated sprite (via script)

I just started learning Unity, so maybe I am approaching this problem from a wrong direction.

I am using 16x16 pixels wide sprites, and I set the "Pixels Per Unit" field in the Import Settings to 16 pixels. If I left it on 100 the sprites would not render correctly (they would collapse into tiny images).

From these 16x16 sprites I generate a (4x4) "map", and above this map I'd like to draw a (3x3) grid.alt text

I try to do this by creating a Texture2D object, modifying its pixels, converting it to a Sprite object and attaching it to a Sprite Renderer.

 private void InitSprite()
     {
         // columns = 3, rows = 3, cellSize = 16
         Texture2D texture = new Texture2D(columns * cellSize,
             rows * cellSize, 
             TextureFormat.ARGB32, 
             false);
             
         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, EDGE_COLOR);
                 } else
                 {
                     texture.SetPixel(i, j, Color.clear);
                 }
             }
         }
 
         texture.Apply();
 
         Sprite sprite = Sprite.Create(texture, 
             new Rect(0, 0, texture.width, texture.height), 
             new Vector2(0.5f, 0.5f));
         
         GetComponent<SpriteRenderer>().sprite = sprite;
     }

However, since I can't modify the sprite's pixel per unit (which is 100 by default), it will not render in the way I want.

alt text

(It doesn't render the whole grid, nor it is in the correct position, but that's not the question, I can figure that out)

I have been looking at ways to solve this:

Using TextureImporter? I have found examples online that derives from the AssetPostprocessor class, and overrides the OnPreprocessTexture method. But how do I hook up this class to modify every texture settings? All I found on the subject was

In a production pipeline AssetPostprocessors should always be placed in pre-built dll's in the project instead of in scripts.

Do I put the script in the Editor folder and Unity will detect that? Do I put my Editor folder in the Assets folder? Or the TextureImporter is for the textures from the Assets folder only?

I'm sorry if these are stupid questions, but I only started Unity a few days ago, and I have been researching online for hours now.

I'm open for suggestions to other approaches as well.

goal.png (8.6 kB)
current.png (7.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Arcaine · Aug 14, 2017 at 04:19 PM

Hi @csirmazbendeguz,

I stumbled on this when I was looking at a similar issue, the problem is that when you're creating your Sprite @ line 27 you need to pass the pixelsPerUnit with it like so,

 Sprite sprite = Sprite.Create(texture, 
              new Rect(0, 0, texture.width, texture.height), 
              new Vector2(0.5f, 0.5f), 100 );

This will create the sprite and set it's pixelsperunit to whatever you like, in your case 100.

Cheers,

Arc

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2d Lighting - Weird light triangles 0 Answers

2D going through walls bug 1 Answer

Changing the orthographicSize of the camera to make sure play field fits 0 Answers

How do I stop my sprite from jumping in the air? 1 Answer

Making the enemy follow the path of the Player,How to make the enemy follow the path of the player? 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