Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Set secondary textures to sprites

Discussion in '2D' started by johanolofsson, Apr 9, 2020.

  1. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    I have a custom importer which creates textures, sprites, animations etc. However, I can't figure out how to assign secondary textures such as _NormalMap, _Emissive etc to my sprites. I obviously set the primary texture using Sprite.texture but there seem to be no way setting the secondary as done in the sprite editor.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Aside from the standard way...
    Untitled - Copy.png
    You should be able to assign it to the material the sprite uses. Capture - Copy.PNG
    Then assign that material to the SpriteRenderer or TilemapRenderer's slot for it.
     
  3. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    id0 likes this.
  4. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    Yes I want to do this from code. I am currently not using the TextureImporter at all. I have my own ScriptedImporter for .pyxel files. It's basically a zip file with the layers as .png and a .json metadatafile. I simply create textures from the .pngs and sprites for them. Can I use the texture importer internally in my own ScriptedImporter?
     
  5. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    I believe, that every texture used in Unity game should have its TextureImporter. It is responsible for changing .png/.tga/... into texture like ETC/DXT/...
    I do not know your game, but I doubt you are using directly .png - probably your .png is transformed into some texture format and it is because of its texture importer.
     
  6. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    I import .pyxel files using a ScriptedImporter. Not a fan of the MenuItem approach as it requires me to manually perform the import and keep track of prev created assets. Anyway a .pyxel is a zip with metadata and .pngs. I simply read the .pngs from the zip using Ionic.zip and get their bytes, I then create textures and use LoadData() to create the textures. I then add the textures to the importer context. Works like a charm but I am unable to secondary textures as that is not exposed through this API. I believe the TextureImporter requires the file to actually be available as an asset with a path. I dont have a texture asset with a path, I have them in memory. I guess I could temporarily save them but it's seems like a suboptimal solution. Thx for your input, I'll see what I can find out.
     
  7. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    Turns out this can be achieved by using the TextureGenerator class and simply store the output from it as assets in the importer context. Special thx to https://github.com/martinhodler for pointing me in the right direction!
     
  8. codeimpossible

    codeimpossible

    Joined:
    Mar 27, 2013
    Posts:
    9
    Hey, I'm trying to do the same thing, I have the TextureGenerator generating the texture, but it doesn't seem to be applying the secondary texture, even though I'm setting it in the settings. Do I need to save the mask texture locally before passing it in? I'm not sure what I'm missing :(
     
  9. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    Damn, dude, thank you for this script! I search for something like this, even trying to write it myself, but then found this tread. This feature should be by default, unity team not using their brain, as usual. I have 300 frames of animation, and I need add normal to all of them manually. Ridiculous.