Search Unity

Record Position on Image Clicked (multiple resolutions)

Discussion in 'Scripting' started by chex35, Aug 4, 2020.

  1. chex35

    chex35

    Joined:
    Feb 4, 2019
    Posts:
    2
    I've read and watched many videos explaining world and local space but can't wrap my head around how to do the following:

    I have a image: 600x1008
    My canvas scalar is: 3840x2160
    My Game display is an iPhone XS Max: 2688x1242

    Goal: I'd like to record the location of the touch on the actual image component and then translate it to the original location on the image to be displayed on the web. Thoughts?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Unity has a function that will pretty much do all the work for you:

    https://docs.unity3d.com/ScriptRefe...ility.ScreenPointToLocalPointInRectangle.html

    You just need to pass it your camera (or null if you're using screen space - Overlay), the RectTransform of the image, and the point the user touched on the screen. It will give you back the point in the image's local space (as an out parameter). I think the point comes out in normalized coordinates (0 to 1), so you just have to multiply that point by the resolution of the image (on an axis by axis basis) if you want pixel coordinates.