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

Question Do sprites store correct pivots?

Discussion in 'Scripting' started by karderos, Aug 15, 2023.

  1. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    I am using

    Code (CSharp):
    1.  
    2.  
    3.  
    4. spriteRect = new Rect(0, 0, 36, 88);
    5. pivot =new Vector2(0.5f, 0.14f)
    6.  
    7. Sprite newSpriteA = Sprite.Create(textureA, spriteRect, pivot);
    8.  
    9. spriteRect = new Rect(0, 0, 36, 87);
    10. pivot =new Vector2(0.5f, 0.13f)
    11.  
    12. Sprite newSpriteB = Sprite.Create(textureB, spriteRect, pivot);
    13.  
    14. Debug.Log(newSpriteA.pivot);
    15. Debug.Log(newSpriteB.pivot);
    16.  
    17.  
    when using sprite.pivot, the debug log shows that both sprites have the same pivot
    I understand that sprite.pivot shows int values in pixels, and so its rounding to the same y pixel

    it still works correctly on a sprite renderer, but this is a problem for using it on an Image

    Where is the correct pivot with floats being stored?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    UI Images don't support sprite pivots. In the UI, pivots are determined by the Rect Transform, the sprite's own pivot is ignored.

    The
    Sprite.pivot
    property is a Vector2 with two floats. The pivot coordinates are not rounded, the position is measured in the texture's pixels but can be fractional. If you want to get the original relative pivot, you'll have to divide the returned pivot by the Sprite.textureRect's width/height (though there might be some precision error).
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    As this is clearly a duplicate of this post:

    https://forum.unity.com/threads/converting-sprite-pivot-to-ui-image-pivot.1478910/#post-9223407

    It would behoove you to ask the actual question of what you're actually struggling with.

    I believe your words were:

    The first way I would think is to ensure all sprites are aligned with a super-box size.

    Another way, which I already listed to you, is to make multiple GameObjects with multiple Images. This is also a solution for animation which will work 100% of the time.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    the math isnt checking out for anything

    I create a 36x87 sprite, with a pivot of (0.46f,0.14f)

    when i use sprite.pivot it logs (11.00,6.00)

    I click the created sprite on the inspector(during playmode), it shows (0.47,0.09)

    thats 3 different pivots,
    the one I actually entered (0.46f,0.14f)
    sprite.pivot (11.00,6.00)
    inspector on sprite during playmode (0.47,0.09)

    I dont understand why this is happening

    and (11.00,6.00) is supposed to translate to (0.47,0.09)?? How?

    6/87? equals 0.068 the math doesnt check out