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 Converting Sprite pivot to UI Image pivot

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

  1. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    I have 2 sprites that work correctly in a sprite renderer

    but now Im trying to use them in a UI image

    I've already figured out that I'm supposed to use image.rectTransform.sizeDelta and put in the dimensions of each sprite

    but now I have an issue, these sprites have different pivots
    the pivot of sprite A is (0.5,0.13) with a size of 36x88
    the pivot of sprite B is (0.5,0.14) with a size of 36x87

    so on the sizeDelta im using (36,88) and (36,87)

    But now I dont know how I convert this pivot offset to the anchored position y. ( is that even what im supposed to do)???

    There is also another issue, Im using sprite create to create these sprites, using my pivots, but when I go into the code and lookup with "sprite.pivot" it shows up a different pivot than the one I used to create the sprite, its rounded and the same for both sprites.

    I can solve this issue by storing the exact pivot in a dictionary but is this normal for the correct pivot to become unaccessible after creating the sprite?

    Anyway I would appreciate if someone could give some guidance on how to lineup the sprites correctly in a UI image with a rect transform knowing these 2 different pivots and size

    Thanks
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I wouldn't do any of that. Anytime you manipulate RectTransform stuff you're probably in for a long and thankless slog against complexity beyond your wildest dreams. For instance it takes about 16 lines of code just to set the anchors to the outer perimeters of a RectTransform. (see bottom link)

    Just leave the Image the correct size and drop in the different sprite. Or else, drop both into separate Image Components, line it all up and turn them on and off appropriately at the GameObject level.

    There really is no call to map the pivot into the UI... it also cannot work because then you could save the scene with the pivot set at one place, then change it in the Sprite pivot, it would somehow have to go back and modify every scene and scene prefab that used that pivot. That's likely why it is separate.

    For the fearless only:

    http://forum.unity3d.com/threads/sc...hor-to-gui-object-size-rect-transform.269690/
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    but then how am I supposed to use different sprites on the same image?

    for example if the image is width 100, height 100, my 33x88 sprite is deformed beyond belief to fit the 100x100 size
    the image is going to be displaying an animation so I cant really have an inconsistent pivot on the different frames, the sprites atm glitch around due to the incorrect pivot (which doesnt happen on the sprite renderer since the pivots are set correctly)