Search Unity

Question Draw on Texture2d during OnPostprocessAllAssets to disk

Discussion in 'Asset Database' started by Faluk, Aug 5, 2022.

  1. Faluk

    Faluk

    Joined:
    Oct 8, 2017
    Posts:
    2
    Hello,

    I came up to the need of drawing some pixels into a Texture2D to solve some artifacts created by some filtering.

    I have go trough the documentation and I can't manage to get the changes done to the disk asset itself.

    Have anyone face a similar problem in the past?

    This is a simplification of the code I have right now:


    Code (CSharp):
    1.     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
    2.     {
    3.         foreach (string asset in importedAssets)
    4.         {
    5.             if (!(asset.Contains(_atlasPath) && asset.EndsWith(".png")))
    6.             {
    7.                 continue;
    8.             }
    9.  
    10.             Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(asset);
    11.             texture.SetPixel(1, 1, Color.black);
    12.            
    13.             texture.Apply();
    14.             AssetDatabase.SaveAssets();
    15.         }
    16.     }
    Thanks!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,001
    hmmm not sure but maybe AssetDatabase.SetDirty before saving might help.

    oh, is the texture read/write enabled? See inspector
     
  3. Faluk

    Faluk

    Joined:
    Oct 8, 2017
    Posts:
    2
    Thanks for the response.

    Yes the Texture has Read/Write enabled, if not it would throw an error on calling SetPixel.

    AssetDatabase.SetDirty does not exists so I suppose you mean EditorUtility.SetDirty. I tried it before the SaveAssets call but it's unfortunately not working.
     
  4. volblob73

    volblob73

    Unity Technologies

    Joined:
    Sep 19, 2017
    Posts:
    36