Search Unity

Question How to position an image atop a specific point on another image?

Discussion in 'UGUI & TextMesh Pro' started by MasonWheeler, Jan 26, 2022.

  1. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    I'm trying to update an older 2D game to run on Unity. One thing it does is place an overworld map with a bunch of icons that you can click on.

    I've set up a Canvas and placed the overworld map as an Image on the Canvas. The Image object is sized to take up the full screen, but the world map image does not, (it's 4:3 aspect ratio and my screen is 16:9,) so I've centered it with Preserve Aspect checked. But now I'm having a heck of a time placing the icons.

    I have the coordinates, in pixels, where they should be on the world map, assuming (0,0) is the top-left corner of the image, but I can't seem to figure out how to translate them to Unity's coordinate system. Everything is upside-down because Unity's Y coordinate is inverted, and everything is scaled all wrong and badly out of place even if I negate the Y value.

    You'd kind of think there would be a built-in Unity function for "give me a Vector3 representing the place to put a child object's transform position to be over this specific (x, y) coordinate on this Sprite" but if it exists I can't find it anywhere. Does anyone know how to do that?
     
  2. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    83
  3. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,443
    I'm a little confused over what you want if I'm honest. You spent a lot of time in the lead-up to the question then not much time on what the issue is.

    Also, this seems like a UI forum question and not a 2D forum question.
     
  5. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    The lead-up was describing clearly what's going on so that it can be understood. The question is precisely what I said: how do I calculate the proper transform position that will place an image atop a specified pixel position on another image, taking into account that:
    1. the overlay is supposed to be a child object of the main image
    2. the coordinates I have start with y=0 at the top of the image
    3. the image has been automatically scaled up by the Image component with the Preserve Aspect option
     
  6. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    MelvMay is right, this really should be in the UI forum, you're likely to get better help there, but I'll try to offer a solution here anyway. I have some questions. You're asking for a transform position, but are you really talking about RectTransforms? The distinction is important. Where are you getting your pixel coordinate data from? I'm assuming you're getting that from the native image itself, if that's the case you'll need to multiply your coordinates by the scale factor of the image. I don't know of a way to get the adjusted rect when using the preserve aspect toggle (this doesn't actually adjust the RectTransform). Using the aspect ratio fitter component with an aspect of 1 will adjust the RectTransform accordingly. Then you should be able to normalize your coordinate data and set the anchor of the icons to the normalized data.
     
  7. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    Yes.

    The data assets and source code of the original game.

    That sounds helpful. I'll try it.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,443
    Yes, the lead-up was correct but I didn't know what you meant by XY position of a Sprite (what does a sprite have to do with it precisely or are you just saying any world position so not really a Sprite thing) and are you talking about 2D stuff here or UI stuff. This was my point, you're on the 2D forum asking what seems to be UI and bringing Sprites into it but it's not clear.

    Anyway, seems like you got help but I'll move your post to the UI forums.