Keeping the scale of inventory icons across resolutions
Hi! So, I'm working on an adventure game and I decided to reuse my old inventory system/item database with some slight modifications. It's like Resident Evil: you pick an item up, and it gets stored in a grid. Unfortunately, everything is hardcoded, meaning that aside from the Canvas, I didn't use any of the editor's UI elements to design it. Below is the function that gets called inside OnGUI() when the conditions are met:
void DrawInventory()
{
// Render the background
Rect backRect = new Rect(0,0,Screen.width,Screen.height);
GUI.DrawTexture (backRect,background,ScaleMode.StretchToFill,true,Screen.width/Screen.height);
// Assign the items in slots
int i = 0;
for (int x = 0; x < slotsX; x++)
{
for (int y = 0; y < slotsY; y++)
{
Rect slotRect = new Rect(y * 135 + Screen.height/y_mod, x * 40 + Screen.width/x_mod, Screen.width/w_mod, Screen.height/h_mod); // Reversed to respect horizontal arrangement
slots[i] = inventory[i]; // and soft-coded to match all resolutions
if (slots[i].itemName != null)
{
GUI.DrawTexture (slotRect,slots[i].itemIcon);
if (isMouseOver (slotRect))
{
GUI.DrawTexture (slotRect,selectionGrid);
DrawDescription(slots[i].itemDescription);
if (mousePressed)
{
DrawInvMenu(slotRect);
}
}
}
i++;
}
}
}
Now, the code itself (picking items, opening the inventory and some other functions I didn't include for simplicity's sake) work perfectly. But since I'm just drawing a texture against which I place my invisible grid, I ran into an unsurprising problem: changing the size of my Game windows naturally messes the positions and scales of the picked items up. Here's an example: https://i.snag.gy/xna0yt.jpg
I've broken my back thinking of different ways to work around this before I ultimately have to rewrite the whole system. To be honest, I'm a bit clueless when it comes to the GUI, so I don't have many thinking patterns to follow. Any help will be much appreciated!
EDIT: The x_mod, y_mod, w_mod and h_mod variables you see above are publicly defined test variables that I used to tweak the positions at runtime.
Your answer
Follow this Question
Related Questions
Display list as UI or GUI 0 Answers
Inventory stacking 1 Answer
How to make an Inventory Hotbar (C#) 0 Answers
How to create an if statement for each element within a list C# 2 Answers