Search Unity

Question Responsive Instantiate UI GameObject

Discussion in 'UGUI & TextMesh Pro' started by DiamondKewa, Jun 8, 2023.

  1. DiamondKewa

    DiamondKewa

    Joined:
    Aug 20, 2017
    Posts:
    8
    Hi. I have a Grid with Tilemaps in the scene, which I have divided into regions by coordinates through code (a regular List<object> where there is a start and end position). So through UI I want to make it possible to see these borders. I made the code and it works, but only on one screen resolution. How can I make these borders look the same on all screen resolutions?

    Code where i created ui gameobjects for coordinates:

    GameManager:
    Code (CSharp):
    1. void Start()
    2.     {
    3.        
    4.         List<WorldRegionPositions> worldRegionPositions = new();
    5.  
    6.         foreach (var item in _levelEditorData.Regions)
    7.         {
    8.             worldRegionPositions.Add(new WorldRegionPositions(
    9.                 Camera.main.WorldToScreenPoint(_levelEditorData.LevelGrid.CellToWorld(item.StartPosition)),
    10.                 Camera.main.WorldToScreenPoint(_levelEditorData.LevelGrid.CellToWorld(item.EndPosition))));
    11.         }
    12.  
    13.         _levelUI.CreateOutlineRegion(worldRegionPositions);
    14.     }
    LevelUIManager
    Code (CSharp):
    1. public void CreateOutlineRegion(List<WorldRegionPositions> worldPositions)
    2.     {
    3.         foreach (WorldRegionPositions positions in worldPositions)
    4.         {
    5.             GameObject tempGameObject = Instantiate(_outlineRegionPrefab, _canvas.transform);
    6.             tempGameObject.GetComponent<RectTransform>().position = new Vector3 (positions.StartPosition.x - (-122.5f), positions.StartPosition.y - (-104));
    7.         }
    8.     }
    How looks in my screen resolution and how it looks on other resolutions:
    upload_2023-6-8_14-33-22.png upload_2023-6-8_14-33-50.png
     

    Attached Files: