My inventory is causing a fps drop whenever it is populated.
I know its something to do with lines 30-42 considering they are being looped. But I cant think of anyway to fix it as that entire block is needed to populate the inventory.
public GameObject button;
public GameObject content;
public GameObject inventoryui;
public int invCols = 4;
void Update()
{
if (Input.GetButtonUp ("Inventory"))
{
Activation(Item.category.Weapon);
}
}
public void Activation(Item.category invItemCategory)
{
int usedColumns = 0;
int rowNumber = 0;
int columnNumber = 0; //Number of Columns
for (int i = 0; i < PlayerInventory.Inventory.Count; i++)
{
inventoryui.SetActive(true);
if (rowNumber >= invCols)
{
columnNumber = 0;
usedColumns++;
rowNumber = 0;
}
Debug.Log("Col Number: " + columnNumber);
if(PlayerInventory.Inventory[i].ItemCategory == invItemCategory)
{
//Create Button instantiation
GameObject instantiation = Instantiate(button, new Vector3((button.GetComponent<RectTransform>().sizeDelta.x * columnNumber), 0 - (button.GetComponent<RectTransform>().sizeDelta.y * usedColumns), 0), Quaternion.identity) as GameObject;
//Resize Content Housing
Vector2 itemSize = new Vector2(content.GetComponent<RectTransform>().sizeDelta.x, instantiation.GetComponent<RectTransform>().sizeDelta.y * columnNumber);
content.GetComponent<RectTransform>().sizeDelta = itemSize;
//Edit Button instantiation
instantiation.GetComponentInChildren<Text>().text = PlayerInventory.Inventory[i].ItemName;
instantiation.GetComponent<Button>().onClick.AddListener(delegate {Click();});
//Deploy initialisation
instantiation.transform.SetParent(content.transform, false);
rowNumber++;
columnNumber++;
}
}
}
I didn’t look too closely at your code, but I did notice that you’re instantiating every object.
To me, that’s perfectly okay to do when you first load or add an object (if your slots are finite/fixed … or can grow).
However, I’d suggest that you keep it cached (disable the game object when the inventory is closed), for instance, and just open it back up with everything in it.
For the most part, I’d say that would make 1 small stutter when you load the game – the other ones, assuming you don’t have huge additions to your inventory during play, would be pretty small.
There are actually quite a few things you could try to improve:
Move the following line out of the loop, it only needs to be done once:
inventoryui.SetActive(true);
comment out the Debug.Log, logging is expensive and shouldn’t be used for performance testing, especially not in loops
You keep instantiating the objects (apparently enough to get an FPS drop), need to resize and re-position them all the time.
You nearly use GetComponent everywhere, you could cache them once in the beginning if you need the same component of the same GO several times
In order to improve #3 & #4 a little further, you could additionally write a custom type that caches all relevant components (Button, Text, RectTransform etc) and exposes them with a getter, which only requires to use GetComponent once during the initial creation. You’d cache up to N instances in total whereas N in the number of maximum slots in the inventory.
Optionally (in addition or before you try to improve on #3, #4, #5) you can build up the visual representation of the inventory over several frames using a coroutine. Using 2 frames you’d technically reduce the overhead per frame by 50% and so on… This is a nice technique for non-gameplay-critical systems.