Jaimin
Jaimin

Reputation: 531

Prefab Instantiate performance improvment

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 Here is image

Upvotes: 0

Views: 1418

Answers (2)

Pasi &#214;sterman
Pasi &#214;sterman

Reputation: 2197

If you know maximum quantity of categories and category items you don't have to instantiate them. You can use slots that you activate and deactivate based on the amount of items. During activation you can also bind and unbind them to objects which you can use to assign correct sprites and texts to your button elements.

If you don't know the quantity of category items in advance you can still do the same but using pagination which can be preferable from user perspective as well if there's a lot of items.


public class SomeUI : MonoBehavior
{
    public GameObject categorySlotsParent;
    public GameObject categoryItemSlotsParent;
    public Category[] categories;
    
    CategorySlot[] categorySlots;
    CategoryItemSlot[] categoryItemSlots;

    public void Awake(){
        
        categorySlots = GetComponentsInChildren<CategorySlot>();
        categoryItemSlots = GetComponentsInChildren<CategoryItemSlot>();
        AssignCategories(categories);
        
        if(categories.Length > 0 && categories[0] != null)
            SelectCategory(categories[0]);
    }
    
    void AssignCategories(Category[] categories){
        
        for(int i = 0; i < categorySlots.Length;  i++){
            if(i < categories.Length)
            {
                categorySlots[i].SetActive(true);
                categorySlots[i].AssignCategory(categories[i]);
            }
            else
            {
                categorySlots[i].SetActive(false);
                categorySlots[i].AssignCategory(null);
            }
        }
    }
    
    void AssignCategoryItems(CategoryItem[] categoryItems){
        
        for(int i = 0; i < categorySlots.Length;  i++)
        {
            if(i < categoryItems.Length)
            {
                categoryItemSlots[i].SetActive(true);
                categoryItemSlots[i].AssignCategoryItem(categoryItems[i]);
            }
            else
            {
                categoryItemSlots[i].SetActive(false);
                categoryItemSlots[i].AssignCategoryItem(null);
            }
        }
    }
    
    void SelectCategory(Category category){
        AssignCategoryItems(category.categoryItems);
    }
}

Upvotes: 0

joreldraw
joreldraw

Reputation: 1736

If your instantiated object is a common object , you can improved it pooling the prefab avoiding to instantiate & destroy this object each time. You have a good example of workflow here

Upvotes: 0

Related Questions