How to implement PrefabPool?
in Photon Bolt
Hello! Can you help me to implement IPrefabPool for Projectile?
public class RocketProjectile : Bolt.EntityBehaviour, IPrefabPool
{
public GameObject LoadPrefab(PrefabId prefabId)
{
return PrefabDatabase.Find(prefabId);
}
GameObject IPrefabPool.Instantiate(PrefabId prefabId, Vector3 position, Quaternion rotation)
{
GameObject go;
go = GameObject.Instantiate(((IPrefabPool)this).LoadPrefab(prefabId), position, rotation);
go.GetComponent().enabled = true;
return go;
}
public void Destroy(GameObject gameObject)
{
GameObject.Destroy(gameObject);
}
}
and
public class WeaponRocketComponent : MonoBehaviour
{
[SerializeField] private GameObject projectilePrefab;
[SerializeField] private GameObject muzzleFlash;
public void Awake()
{
BoltNetwork.SetPrefabPool(projectilePrefab.GetComponent());
}
public void DisplayEffects(ICharacterState state)
{
// create shell
BoltEntity shellGo = BoltNetwork.Instantiate(projectilePrefab, muzzleFlash.transform.position, Quaternion.identity);
}
}
Is it correct implementation? and how can I check that is working?
Thank you
public class RocketProjectile : Bolt.EntityBehaviour, IPrefabPool
{
public GameObject LoadPrefab(PrefabId prefabId)
{
return PrefabDatabase.Find(prefabId);
}
GameObject IPrefabPool.Instantiate(PrefabId prefabId, Vector3 position, Quaternion rotation)
{
GameObject go;
go = GameObject.Instantiate(((IPrefabPool)this).LoadPrefab(prefabId), position, rotation);
go.GetComponent().enabled = true;
return go;
}
public void Destroy(GameObject gameObject)
{
GameObject.Destroy(gameObject);
}
}
and
public class WeaponRocketComponent : MonoBehaviour
{
[SerializeField] private GameObject projectilePrefab;
[SerializeField] private GameObject muzzleFlash;
public void Awake()
{
BoltNetwork.SetPrefabPool(projectilePrefab.GetComponent());
}
public void DisplayEffects(ICharacterState state)
{
// create shell
BoltEntity shellGo = BoltNetwork.Instantiate(projectilePrefab, muzzleFlash.transform.position, Quaternion.identity);
}
}
Is it correct implementation? and how can I check that is working?
Thank you
0
Comments
-
You are currently destroying the entity instead of pooling it. You can implement the interface to "recycle" the entities instead of instantiating and destroying every time.0