Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 vyktor2001 · Jun 13, 2021 at 05:47 PM · for-loop

How to reduce iterations for each pixel?

I am building a game in Unity and I have a RawImage with the dimensions 1024 x 1024 and it doesn't have a texture initially. I build one at runtime. At the moment, I have a button that when pressed generates a new texture and sets it to the RawImage. Basically, I generate a random array of Vector2 that contains 4 random positions within the dimensions of the RawImage. I'm facing long processing times because I need to set each pixel of the RawImage to my desired background color and all of this for just 4 positions. In other words, I do 1024 x 1024 = 1048576 iterations when I would just need 4. Here is a graphical representation as an example: alt text

*In this example I used a resolution of 40x40 in order to make the points more visible.

Here is the code that I use at the moment:

 public RawImage myImage;
 
     public void GenerateButtonPressed()
     {
         Texture2D topTexture = new Texture2D(40, 40);
         Vector2[] randomValues = new Vector2[4];
         Color pointColor = Color.black;
         Color backgroundColor = Color.cyan;
 
         for(int i=0; i < randomValues.Length; i++)
         {
             randomValues[i] = new Vector2(Random.Range(0, 40), Random.Range(0, 40));
         }
 
         int randomCounter = 0;
         for(int i = 0; i < topTexture.width; i++)
         {
             for(int j = 0; j < topTexture.width; j++)
             {
                 if(i == randomValues[randomCounter].x && j == randomValues[randomCounter].y)
                 {
                     topTexture.SetPixel(i, j, pointColor);
                     randomCounter++;
                 }
                 else
                 {
                     topTexture.SetPixel(i, j, backgroundColor);
                 }
                 
             }
         }
         topTexture.Apply();
         myImage.texture = topTexture;
     }


As you can see, I do 2 nested for loops with a pretty big index size just to set 4 points. Is there a way to reset the background without doing all those iterations and to set just the 4 points when the button is presed? Any amount of help would be appreciated because this improvement for sure will make my game more enjoyable. Thanks in advance!

example.png (7.4 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
Best Answer

Answer by Pangamini · Jun 13, 2021 at 05:57 PM

There are multiple things you could reconsider. Setting all pixels at once using SetPixels instead, with a prepared array (which can potentially be prepared on a background thread). Using the *32 version (SetPixel32, SetPixels32) to avoid the floating point conversions. Possibly the fastest option would be to use Texture2D.SetPixelData, which does just a dumb data copy - so make sure that your bytes are in a correct format. Another thing - do you need to re-set all pixels every time? What if you remembered, which pixels were black, and only cleared those and set the new ones in the next image?

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 vyktor2001 · Jun 13, 2021 at 06:21 PM 0
Share

I didn't knew that there are so many options available. I'm going to look into those and implement all of them. Thanks man.

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

120 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

Related Questions

C# Unity how to select only one item in an array and deselect other items 1 Answer

for in loop in Unity iPhone? 2 Answers

For loop on collision (two hit kill) 2 Answers

UnityScript Variable Scope? 2 Answers

Instantiate From array, but cycle trough each gameobject 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