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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How To Position Image Over Another Image at Specific Pixel Location w/ Preserve Aspect

Discussion in '2D' started by joshuaFS, Jul 31, 2018.

  1. joshuaFS

    joshuaFS

    Joined:
    Jul 23, 2018
    Posts:
    24
    I'm completely new to Unity (although I do know programming/code), so I am likely asking this question wrong and I'm hoping there is in fact a better way to do what I need.

    I have a simple image (A) with locations on it visually marked. On certain events I need to move another image (B) over and on top of one of the marked locations. Visualize a rectangle with multiple dots scattered around it (image A) that represent locations that the top image (B) needs to move to precisely and cover.

    I was thinking I could use some kind of basic math and write a script to move image B around, but I need to preserve the aspect ratio of my main image A. When I do that, the rectTransform.anchoredPosition, rectTransform.rect.size and rectTransform.sizeDelta don't seem to help as the resolution changes (canvas scales with screen size). I was hoping "GetPixelAdjustedRect" on the image would give something helpful, but nothing looked usable there either. The rectTransform.rect.size does change, but the values do not represent the image shown on the screen due to the "preserve aspect" option set on the Image A script component. I can't see any way to get the size of the resulting rendered image in this case, and so I'm stuck.

    Ultimately, it would be great if there was some way to just markup the main image A in some fashion that could use something like iTween to just "moveTo" on image B, but with all my searching I can't find anything like that, although I'm not sure I'm using the right search terms to even start the search to find it.
     
  2. joshuaFS

    joshuaFS

    Joined:
    Jul 23, 2018
    Posts:
    24
    Just to not leave this thread completely hanging in case someone else has this problem. I ended up writing my own scaling script for the image based on the aspect fitter script, that I can then use to know exact pixel locations in this matrix on the main image and then just use basic math as mentioned earlier to know where everything is. I'm still sure there must be a "right way" of doing something like this in Unity, but for now what I have works and it performs well for my purposes.
     
  3. itsThatGuy

    itsThatGuy

    Joined:
    Apr 5, 2015
    Posts:
    2
    I'm trying to make a pathfinding grid on a 2d image that uses AspectRatioFitter and PreserveAspect and I too have found nothing to help. I'm not a rookie programmer but I don't know enough about all the working parts of Unity UI yet.

    @joshuaFS I have this exact problem and have been googling for AGES to find something simple, but apparently there isn't. Do you mean to say here you extended the AspectRatioFitter.cs class, or you wrote a script that works in tandem with it? Or did you just write your own version of it?
     
  4. vanillachapstick

    vanillachapstick

    Joined:
    Oct 1, 2018
    Posts:
    1
    If anyone has an answer to this, it would be appreciated; I have the same problem.