so I have a multiple category buttons in every category there is more than 20 items. So make prefab and instantiate when button is pressed. The problem is When I instantiate prefab I have to initialize it with info like name, sprite, btn click event etc. this will take time to instantiate prefabs and game hangs. Here is code
for (int i = 0; i < prefab.Length; i++)
{
GameObject go = Instantiate(basePrefab) as GameObject;
go.SetActive(true);
go.transform.SetParent(prefabParent, false);
go.transform.localScale = Vector3.one;
go.GetComponent<Category>().Init(prefab[i]);
}
This code is called in button click. Alternatively to achieve some performance in start method I already instantiate some basePrefab on a empty gameobject and on button click just change parent and initialize them. this approach will give some benefits but still game is hangs for 2 sec here is that code
for (int i = 0; i < prefab.Length; i++)
{
GameObject go = InstantiatedGameObject[i];
go.SetActive(true);
go.transform.SetParent(prefabParent, false);
go.transform.localScale = Vector3.one;
go.GetComponent<Category>().Init(prefab[i]);
}
Any solution to improve performance ??
So Here is About prefab contenet: Basically prefab contains Background image, Mask image in this image Catgory image, Selection image, and
a Text. In init function the category image change and Text value change Also Background contains button in init set this button's click event