Search Unity

Set wrap mode to clamp

Discussion in 'Scripting' started by Phantom_X, Feb 21, 2020.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Hi,
    I'm creating an editor script to generate a gradient texture from a gradient field. I want to set the wrap mode of the newly created texture to clamp.

    I tried setting the texture.wrapMode before calling Apply(), tried after, tried loading the asset and changing it. Nothing seems to work, the wrap mode always stays to repeat.

    What am I missing?

    Thanks!

    Code (CSharp):
    1.        
    2.  
    3. Texture2D texture = new Texture2D(width, height, TextureFormat.RGBAFloat, false);
    4.  
    5. for (int x = 0; x < width; x++)
    6. {
    7.        var color = grad.Evaluate((float)x / (float)width);
    8.        Color[] colors = Enumerable.Repeat(color, height).ToArray();
    9.        texture.SetPixels(x, 0, 1, height, colors);
    10. }
    11.  
    12. texture.wrapMode = TextureWrapMode.Clamp; // Set the wrap mode
    13. texture.Apply();
    14.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Maybe try reverse the order of line 12 and 13, in case the .Apply() actually caches some previous values and overwrites the wrap mode... just a thought.
     
  3. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Yeah I tried that, doesn't help unfortunately.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    how is that texture mapped? how do you test whether it works?
     
  5. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Omg, I forgot to mention the most important!

    This is a texture that I physically create on the hard drive using this:
    Code (CSharp):
    1.        
    2.         byte[] bytes = texture.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat);
    3.         File.WriteAllBytes(savePath, bytes);
    4.         AssetDatabase.Refresh();
    5.  
    so the import settings remains to repeat and this is what I want to change not using an asset preProcessor if possible.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    great. yes that's it, you want to change the import settings, not the object in memory.
     
  7. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    yes, is this possible to do from an editor script without an asset preprocessor?
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    try TextureImporter
     
  9. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Ho yeah, I didn't know how to use that outside the preprocessor, but I found out.
    Thanks!
     
    orionsyndrome likes this.