- Home /
Unity loading prefabs difficulties. optimization
Hopefully anyone can help me out. I have a some different prefabs and a script which spawn those objects. I has created an array of prefabs and manually placed them there. But after some time the amount of prefabs increased. What if I had a more objects (100-200) ? How I have to find a right prefab and spawn it ? Public array of gameobjects in script will not be correct in this case (100 prefabs or more) ? What I have to do with this all objects? AssetBundles or Resource folder, which one will be correct ?
Hello buddy,
If you are a beginner just create your prefab Array, play your game and be happy...
But if not Resources is a good practice, but no need to worry if the game is running smooth, you can use the profiler to truly know your game performance issues
Answer by kskjadav007 · Apr 06, 2018 at 10:05 AM
Objectpooling is best i think
public List m_pooledBullet;
public int m_PoolAmount;
void Start()
{
for(int i = 0 ; i<m_PoolAmount;i++)
{
GameObject obj = (GameObject)Instantiate(m_Bullet);
obj.SetActive(false);
m_pooledBullet.Add(obj);
}
}
public GameObject GetPooledBullet()
{
for(int i = 0 ; i<m_pooledBullet.Count;i++)
{
if(!m_pooledBullet[i].activeInHierarchy)
{
return m_pooledBullet[i];
}
}
return null;
}
void Shoot()
{
GameObject obb = GetPooledBullet();
if(obb != null)
{
obb.transform.position = transform.position;
obb.transform.rotation = Quaternion.identity;
obb.gameObject.SetActive(true);
}
else
{
//Debug.Log("null");
}
}