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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug TextureGenerator.GenerateTexture sprite pivots are all Vector2.one * 0.5f

Discussion in '2D' started by codeimpossible, Dec 8, 2020.

  1. codeimpossible

    codeimpossible

    Joined:
    Mar 27, 2013
    Posts:
    8
    I'm trying to script an importer for sprite sheets that have both masks and normal maps. I've been able to get the secondary textures working by calling UnityEditor.Experimental.AssetImporters.TextureGenerator.GenerateTexture(), but it seems that no matter what I do, all the sprites that are generated have their pivot value set to their center.

    The code I have currently looks a bit like this:

    Code (CSharp):
    1. var settings = new TextureGenerationSettings(TextureImporterType.Sprite);
    2. // ... a bunch of other settings
    3.  
    4. settings.textureImporterSettings.spritePivot = inspectorSettings.spritePivot;
    Also, there doesn't appear to be anyway to alter the sprite pivot on the Sprite object itself, and attempting to create an alternate version with Sprite.Create() breaks the relation with the normals and mask textures.

    Is there some other way to set this or is this really broken?

    [Edit: I'm using 2020.1.x of Unity]
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    On TextureGenerationSettings, there's a field called spriteImportData. I suspect this is similar to SpriteMetaData[] spritesheet field found on the TextureImporter. If so, then for spritesheets you would assign a SpriteImportData[] to the settings. That struct has a field called pivot, which if like SpriteMetaData wants a relativized pivot, e.g. (.5f, .5f) would be centered in the middle of the sprite's rect.

    https://docs.unity3d.com/ScriptReference/Experimental.AssetImporters.SpriteImportData.html
     
  3. codeimpossible

    codeimpossible

    Joined:
    Mar 27, 2013
    Posts:
    8
    Yep, i'm assigning that as well, it has all the sprite metadata, with the correct pivots, but the TextureGenerator doesn't seem to respect it.
     
  4. codeimpossible

    codeimpossible

    Joined:
    Mar 27, 2013
    Posts:
    8
    I figured out what I was doing that was causing the issue. In the SpriteImportData[] I was setting the spritePivot, but I wasn't setting the alignment so it was defaulting to center. :oops:

    Anyway, once I had that changed to use Custom alignment everything worked as expected.