Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Creating new normal map during runtime.

Discussion in 'Scripting' started by prattmyster, Jul 21, 2023.

  1. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    Hi I'm looking for help.
    I am currently working on a mechanic that lets me import textures during runtime and convert them to a texture2D from a byte array.
    i know the ins and outs of creating a new Texture2D(0,0);
    and then setting the byes through LoadImage, however what is the best way to create this texture2D as a normal map. i go to add the normal map format option the graphics format option however it then asks for flags which always leads to an error.

    Code (csharp):
    1.  
    2.  
    3.         public TextureData(Texture2D t)
    4.         {
    5.             if (t == null) return;
    6.             width = t.width;
    7.             height = t.height;
    8.             bytes = t.EncodeToPNG();
    9.             format = t.graphicsFormat;
    10.         }
    11.  
    12.         Texture2D Create()
    13.         {
    14.             if (bytes == null) return null;
    15.             Texture2D t = new Texture2D(width, height, format, TextureCreationFlags.None);
    16.             t.LoadImage(bytes);
    17.             t.Apply();
    18.             return t;
    19.         }
    20.  
    21.  
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    prattmyster likes this.
  3. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    I done a bit more playing and turns out i needed to get the bytes by using, GetRawTextureData then apply with LoadRawTextureData. Not sure why Unity haven't added those parameters onto the docs yet though. However its creating the normal map when needed so not going o argue with it