• 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 Dman00Cman · Sep 24, 2018 at 10:14 PM · androidsprite

Sprite.Create Differences between Editor and Android

I have been struggling today between the differences in a programmatically generated sprite between the editor and a pushed android build. Below is the code to generate the sprite:

 using UnityEngine;
 
 public class RuntimeSprite : MonoBehaviour {
 
     public float updatePeriodSeconds = 0.5f;
     public int width = 256;
     public int height = 256;
 
     private SpriteRenderer ren;
     private float cameraWidthUnits;
 
     // Use this for initialization
     void Start()
     {
         ren = GetComponent<SpriteRenderer>();
 
         Vector3 leftScreenBoundWorld = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
         Vector3 rightScreenBoundWorld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0));
 
         Vector3 screenWidthVector = rightScreenBoundWorld - leftScreenBoundWorld;
 
         cameraWidthUnits = screenWidthVector.x;
 
         width = (int)(cameraWidthUnits * 8.0f);
 
         Texture2D tex = new Texture2D(width, height);
         tex.filterMode = FilterMode.Point;
         GenerateTexture(ref tex);
         ren.sprite = Sprite.Create(tex,
             new Rect(0, 0, width, height),
             new Vector2(0.5f, 0.5f), 8f);
     }
 
     private void GenerateTexture(ref Texture2D tex)
     {
         for (int x = 0; x < width; x++)
         {
             for(int y = 0; y < height; y++)
             {
                 Color color = CalculateColor(x, y);               
                 tex.SetPixel(x, y, color);
             }
         }
 
         tex.Apply();
     }
 
 }
 

This code in the editor generates the following sprite as I would expect in my scene (The generated sprite is the vertical white, black, and red lines. The other sprite is a normal static sprite from a png at 8 pixels per unit): alt text

When I push the build to my Android device, not only are my pixels per unit off, but my sprite colors also change. Here you can see the results.alt text

Any idea what would cause this? I have verified via debugging on the device that the correct colors are being set on the texture.



UPDATE: So I was able to get the texture to render on Android correctly if I make it 2x the size and double the ppu to 16. The issue here is that I'm generating this sprite every ~0.1 seconds to sample some noise, and doing this for a sprite this large is too expensive of an operation.

This makes me think the issue is some sort of auto compression happening on my Android device. Although when I print my Texture2d's format on my device, it displays as RGB32,



UPDATE 2:

I found a work around. Accepted answer posted below.

androidspritesnap.png (19.1 kB)
editorspritesnap.png (3.0 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
0
Best Answer

Answer by Dman00Cman · Sep 26, 2018 at 04:17 PM

If anyone is interested I have found a work around for this.

Instead of programmatically creating the sprite and texture, I created an empty texture in the editor which will be large enough for my needs, and then programmatically modify the texture. In order to make this work, change the sprite's settings to the desired values you want (pixels per unit, filter type, and compression to uncompressed). Under "Advanced" you must check the "Read/Write" box.

Then at runtime, simply modify the sprite's texture via SetPixel and then when you are done, call texture.Apply(). This allowed the sprite to appear at the correct ppu without compression, as I desire. Code is below:

  using UnityEngine;
  
  public class RuntimeSprite : MonoBehaviour {
  
      public float updatePeriodSeconds = 0.5f;
      public int width = 256;
      public int height = 256;
  
      private SpriteRenderer ren;
      private float accumulatedDeltaT;
  
      // Use this for initialization
      void Start()
      {
          ren = GetComponent<SpriteRenderer>();
  
          width = ren.sprite.texture.width;
          height = ren.sprite.texture.height;
 
          accumulatedDeltaT  = 0f;
      }
  
      private void Update()
      {
          accumulatedDeltaT += Time.deltaTime;
          if (accumulatedDeltaT >= updatePeriodSeconds)
          {
               accumulatedDeltaT = 0;
               Texture2D tex = ren.sprite.texture;
            
               GenerateTexture(ref tex);
          }
      }

      private void GenerateTexture(ref Texture2D tex)
      {
          for (int x = 0; x < width; x++)
          {
              for(int y = 0; y < height; y++)
              {
                  Color color = CalculateColor(x, y);               
                  tex.SetPixel(x, y, color);
              }
          }
  
          tex.Apply();
      }
  
  }
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

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

Related Questions

sprite quality decreases in Android 1 Answer

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

how can i place my sprite/gameobject to the upper right corner position of my screen 1 Answer

Does PolygonCollider2D.OverlapPoint() perform differently in Unity player than an Android device? 1 Answer

reduce APK file size 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