Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity stops responding as I assign texture2d variable.

Discussion in 'Scripting' started by alussam, Jul 25, 2018.

  1. alussam

    alussam

    Joined:
    May 13, 2018
    Posts:
    15
    Unity stops responding as I run this script: Any Idea, why? Thanks for help.
    public class testEditorScript1 : MonoBehaviour
    {

    public Texture2D image;
    public Texture2D image1;

    public void RotateTex()
    {
    //image1 = image;
    //Texture2D tex = new Texture2D(input.width, input.height);
    int a = 1;
    for (int x = 0; x < image.width; x++)
    {
    for (int y = 0; y < image.height; y++)
    {
    Color color = Color.red;
    image.SetPixel(y, x, color);
    a++;
    Debug.Log(a);
    }
    }
    }
    }
     
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,217
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,533
    What @Peter77 said.

    If you really need the output, log it to a text file and review later. Or as an alternative, dump it into another image so you can visualize it.

    As an aside this looks like ideal stuff to do in a shader instead of in script.
     
  4. alussam

    alussam

    Joined:
    May 13, 2018
    Posts:
    15
    Thanks, but I tried this even without Log. This has the same effect. Ok and by the way there is no log at all, there is no messages in console, just unity stops responding. I think there is a trouble in assigning texture2d variable
     
  5. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    312
    2 things,
    First. check if you are calling RotateTex several times per frame, that will escalate your already heavy loops a lot.
    Second, make your RotateTex into a coroutine, as this kind of looping-though-image/files should be handled separate from the rest of the main serialization.
     
  6. alussam

    alussam

    Joined:
    May 13, 2018
    Posts:
    15
    O
    Sorry, I forgot to mention I use this as editor script.
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    807
    Try it on a very small, say 4x4, texture. Leave in the Debugs, or put on at the end. That way you'll know if it's really freezing, or if it's just running slowly (I suspect it's just really slow.)

    But the docs say to use SetPixels (with an S): If you're constantly regenerating a texture at runtime, it may be faster to generate an array of pixel colors and set all of them at once with SetPixels.
     
    Kiwasi and Kurt-Dekker like this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,412
    And remember, neither .SetPixel or .SetPixels will have any visible effect on the texture until you also call .Apply().
     
    Kiwasi likes this.