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.

Question Convert Percentage Data Into UI Rect Position

Discussion in 'UGUI & TextMesh Pro' started by siddharth3322, Dec 17, 2022.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,036
    I have different country maps to show within the gameplay and for different stats of country I want to show level button at correct its stats position.

    Through web service level button data coming into the percentage form range from 0 to 100%.

    This percentage data, I have to convert into pixels from available Map image.

    usa map.jpg

    At present, I have placed level buttons at random position.
    Now how to convert pixel data into RectTransform position that is confusing for me.

    I have tried this code:
    Code (CSharp):
    1. float xPos = (thisLevel.mapPositionX * containerWidth) / 100;
    2. float yPos = (thisLevel.mapPositionY * containerHeight) / 100;
    3.  
    4. Vector3 buttonPosition = new Vector3(xPos, yPos, 0f);
    5. buttonPosition = levelButtonsContainer.InverseTransformVector(buttonPosition);
    But this giving me weird result.
    For level buttons container image I have set center for pivot and anchor.
    Please share your suggestion for this.