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 /
avatar image
0
Question by moneal2001 · Feb 23, 2020 at 07:38 AM · getpixel

GetPixel returns alpha of 0 for all pixels of a texture2d.

I am using this code to create a basic level for my game:

public void Initialize(Texture2D map) { Debug.Log("Initializing Map");

     if (map.width > Max_Columns || map.height > Max_Rows) {

         throw (new Map_Too_Large_Exception("Map is too large.  Cannot be loaded."));
     }

     if (current_Level == null)
     {
         current_Level = new List<Block>();
     }
     else {

         current_Level.Clear();
     }

     for (int x = 0; x < map.width ; x++)
     {
         for (int y = 0; y < map.height; y++)
         {
             GenerateTile(x, -y, map);
         }
     }

     initialized = true;
 }

void GenerateTile(int x, int y, Texture2D map) {

     Color32 pixelColor = map.GetPixel(x, y);

     **Debug.Log(pixelColor);**
     if (pixelColor.a == 0)
     {
         // The pixel is transparent ignore it!
         return;
     }

     foreach (Color_to_Prefab color in color_Map)
     {
         if (color.Color.Equals(pixelColor))
         {
             Vector2 position = new Vector2(x , y);
             GameObject go = Instantiate(color.Prefab, position, Quaternion.identity, transform);
             current_Level.Add(go.GetComponent<Block>());
         }
     }

     manager.Set_Level_Cleared(false);
 }

The problem is my level is always empty. Because every pixel has an alpha of 0. I use the bolded debug.log to check the color. I have used paint.net and gimp to create textures and both produce the same problem. The alpha of colored pixels in both are set to 255 yet no matter what I try when imported to Unity checking the alpha always returns 0.,

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 joemane22 · Feb 23, 2020 at 07:44 AM 1
Share

I am not positive this is the issue but Texture2D.GetPixel(int, int) returns a Color not a Color32. Try that and see if it works. Let me know if not and I will try to dig a little deeper.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 23, 2020 at 09:44 AM

Are you sure that you have marked your texture as "readable" in the texture importer settings? If it's not readable you can not read any texture data because it's not stored in PC RAM but only on the GPU.


Apart from that I highly recommend to not use GetPIxel but instead use GetPixels / GetPixels32 to read in the whole texture as a flattened array. This will be much faster with less overhead.


Likewise you should create a Dictionary<Color32, Block> to implement your "color_Map". This has several advantages. A dictionary lookup has a time complexity of O(1) instead of O(n). Also you should store your prefab references in a Block variable instead of a GameObject variable. This will make Instantiate return the instantiated Block instance and you don't need to call GetComponent afterwards.


Also note that in such tight loops with many iterations you should avoid method calls. In my example over here by removing unnecessary method calls the algorithm runs about 8 times faster in my case.

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.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

GetPixel not working on iPhone 1 Answer

How to calculate scale size of sprite object in unity 1 Answer

GetPixel error on downloaded texture even with crossdomain.xml 1 Answer

WebCamTexture GetPixel(s) not working (RGB values are 0) 0 Answers

Compare getPixel() to a Color 1 Answer


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