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. Dismiss Notice

Question Offset for icon changes in windowed mode

Discussion in 'Scripting' started by TiggyFairy, Aug 27, 2023.

  1. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    Hello, I have a grid based inventory and it works great except for one small issue: in order to line up the image with the grid I have to add half the width of a tile to it when the width is an odd number. This works fine in full screen mode. However, in windowed mode It looks like I need to add the full tile width as an offset.

    My question is this: is there a way to add this offset that doesn't depend on the screen resolution, windowing, and so on?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Didn't we already suggest using the Grid Layout component for this? Why are you doing this all manually?
     
  3. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    I'm not. I'm using a grid layout group, and it did solve a lot of issues. The problem is that my images are not 1x1. They are all sorts of ratios. And that means that no matter where the image goes it has to be offset in some way.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Does it though? Can you not just anchor the image to the top-left most grid space to takes up?
     
  5. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    That's what the system does. But I still have to offset.

    Code (csharp):
    1.  
    2. //Set Image location
    3. Vector3 offset = Vector3.zero; //Fix for odd sized objects
    4. Vector2Int dimensions = GetDimensions(heldItem);
    5. if (!Interfaces.NumberIsEven(dimensions.y)) { offset.y = -(inv.tileSize / 2); }
    6. if (!Interfaces.NumberIsEven(dimensions.x)) { offset.x = inv.tileSize / 2; }
    7. heldRct.position = currentInv.cells[pos.x, pos.y].transform.position + offset;
    8.  
    9.  
    10. private static Vector2Int GetDimensions(ItemCore iCore)
    11. {
    12.     if (iCore.rotated) { return new Vector2Int(stats.height, stats.width); }
    13.     return new Vector2Int(stats.width, stats.weight);
    14. }
    15.  
    16.  
    17.  
    If I set it to the anchored position it goes waaaaaay off to one side.
     
    Last edited: Aug 27, 2023