Search Unity

Bug Texture references reset after reimporting asset

Discussion in 'Scripting' started by carcasanchez, Jul 8, 2021.

  1. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Hello,
    I have run into a strange issue when managing code-generated textures.
    I have a code where I:
    -Convert a texture to grayscale
    -Save it to disk as png,
    -Loaded as Texture 2D, I assign the texture to the original material, substituting the non-grayscaled texture.
    I do this for several (usually six) materials in a for loop.
    -Then I change the import settings of the texture to make it readable, and reimport it.
    The strange part happens after reimporting the texture asset, when the material references seem to reset to the non-grayscaled textures. This happens very randomly: if I debug line by line, or add watches in Visual Studio, the reference doesn't get reset.
    I suspect textures are not mean to get their import settings changed after loaded, but seems very strange for me this behavior even depends if I'm debugging the code or not. Has anyone some insight they could provide about how referencing textures and reimporting works under the hood?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think you want to reorder your last two steps and reimport the grayscale, THEN assign it...

    Are you also dirtying the scene / prefab that contains this reference? I like to use
    Undo.RecordObject()
    to mark stuff dirty and track undo.
     
  3. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Tried setting dirty the materials after changing the texture and saving assets, still not working.
    I have moved the import code after I finish handling the textures, so it works anyways. But would like to know what happened with the materials previously