• 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 janestap · Jan 04, 2015 at 11:11 PM · arraytexture2dconvertcolor32

Convert Texture2D to array of uint and back

For my game I need to convert a texture into an array of uint, do some manipulation, and then back to a texture. I came up with the following code, but it is rather slow.

 private uint[] Texture2DToUIntArray(Texture2D texture)
     {
         Color32 c;
         int width = texture.width;
         int height = texture.height;
 
         uint[] pixels = new uint[width * height];
 
         for (int y = 0; y < height; y++)
         {
             for (int x = 0; x < width; x++)
             {
                 c = texture.GetPixel(x, y);
                 pixels[y * width + x] = Color32ToUInt(c);
             }
         }
 
         return pixels;
     }
 
     private Texture2D UIntArrayToTexture2D(uint[] pixels, int width, int height, TextureFormat format)
     {
         Color32 c;
         Texture2D texture = new Texture2D(width, height, format, false);
 
         for (int y = 0; y < height; y++)
         {
             for (int x = 0; x < width; x++)
             {
                 c = UIntToColor32(pixels[y * width + x]);
                 texture.SetPixel(x, y, c);
             }
         }
         texture.Apply();
 
         return texture;
     }
 
     private uint Color32ToUInt(Color32 color)
     {
         return (uint)((color.a << 24) | (color.r << 16) | (color.g << 8) | (color.b << 0));
     }
 
     private Color32 UIntToColor32(uint color)
     {
         return new Color32((byte)(color >> 16), (byte)(color >> 8), (byte)(color >> 0), (byte)(color >> 24));
     }

Is there any way to make these conversions faster?

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
0

Answer by quizcanners · Jul 27, 2015 at 04:59 PM

I am looking for this too, but you shouldn't use "SetPixel". Here is some pieces from my code, it mostly the same:

 public uint[] pix;
     public int w;
 
     public void toColor (UnityEngine.Color32[] mpix){
         for (int i=0; i<mpix.Length; i++){
             mpix[i].r=(byte)(pix[i]);
             mpix[i].g=(byte)(pix[i]>>8); //& 0xFF00);
             mpix[i].b=(byte)(pix[i]>>16);  //& 0xFF0000);
             mpix[i].a=(byte)(pix[i]>>24);  //& 0xFF000000);
         }
     }
 
     public void fColor (UnityEngine.Color32[] mpix){
         pix=new uint[mpix.Length];
         for (int i=0; i<mpix.Length; i++) {
             pix [i] = (uint)(mpix [i].r | (mpix [i].g << 8) | (mpix [i].b << 16) | (mpix [i].a << 24));
         }
     }

And to apply:

 imgdta.MyTexture = new Texture2D (imgdta.wight,imgdta.wight, TextureFormat.RGBA32, false);
             rend.material.mainTexture=imgdta.MyTexture;
         imgdta.MyTexture.SetPixels3 (imgdta.MyPixels);
         imgdta.MyTexture.Apply (false);
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 starlord89 · Oct 03, 2017 at 12:30 PM

I was wondering if you found a solution ? I tried your method and it crashes my unity after awhile

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

28 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

Related Questions

Rotate non square Color array by 90 degrees 1 Answer

Help converting this to C# - a few issues 3 Answers

loading a folder of textures to an array 0 Answers

Convert Texture2D to Image emgu CV - fast way 2 Answers

Texture to Texture2D 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