private IPoolAble CreateNew(InstantiationMethod instantiationMethod) { IPoolAble poolAble = instantiationMethod(this); poolAble.Inactivated += PoolAble_Inactivated; return(poolAble); }
private void PoolAble_Inactivated(object sender, PoolAbleEventArgs e) { var hashCode = e.HashCode; int max = pools[hashCode].Max; if (pools[hashCode].PoolingObjects.Count >= max) { IPoolAble poolAble = sender as IPoolAble; poolAble.Inactivated -= PoolAble_Inactivated; MonoBehaviour behaviour = poolAble as MonoBehaviour; pools[hashCode].PoolingObjects.Remove(poolAble); Destroy(behaviour); } }
public void ChangeBullet(GameObject newBullet) { BulletPrefab = newBullet; GenericObjectPool genericObjectPool = GenericObjectPool.Current; genericObjectPool.Init(BulletPrefab.GetHashCode(), behaviour => { GameObject bullet = (GameObject)Instantiate( BulletPrefab); IPoolAble poolAble = bullet.GetComponent <IPoolAble>(); poolAble.Init(BulletPrefab.GetHashCode()); return(poolAble); }, 20, 100); }
public override void Start() { InitWeaponHolder(); GenericObjectPool genericObjectPool = GenericObjectPool.Current; genericObjectPool.Init(BulletPrefab.GetHashCode(), behaviour => { GameObject bullet = (GameObject)Instantiate( BulletPrefab); IPoolAble poolAble = bullet.GetComponent <IPoolAble>(); poolAble.Init(BulletPrefab.GetHashCode()); return(poolAble); }, 20, 100); }
public virtual void Attack() { if (Ammunition == 0) { return; } if (Time.time - _lastShootTime < ReloadTime) { return; } if (transform.CompareTag("Player") && holdingType == PlayerAnimation.WeaponType.Melee) { GetComponent <PlayerAnimation>().AnimateSlash(); } Quaternion rotation = BulletSpawnPosition.root.rotation; IPoolAble poolAble = GenericObjectPool.Current.Get(BulletPrefab.GetHashCode()); Bullet bullet1 = poolAble as Bullet; GameObject bulletGameObject = bullet1.gameObject; bulletGameObject.transform.parent = null; bulletGameObject.transform.position = BulletSpawnPosition.position; bullet1.spawnPosition = BulletSpawnPosition.position; bulletGameObject.transform.rotation = rotation; AudioManager.PlayShootSound(InventaryItemName, _bulletSpawnPosition, transform.tag); //var bullet = (GameObject)Instantiate( //BulletPrefab, //BulletSpawnPosition.position, //rotation); //int code = bullet.GetHashCode(); if (Ammunition != -1) { Ammunition--; } _lastShootTime = Time.time; bullet1.shooter = gameObject; bullet1.SetActive(); }
public void Init(int hashCode, InstantiationMethod instantiationMethod, int min = 10, int max = 20) { if (!pools.ContainsKey(hashCode)) { PoolingElementContext poolingElementContext = new PoolingElementContext(); poolingElementContext.InstationMethod = instantiationMethod; poolingElementContext.Min = min; poolingElementContext.Max = max; poolingElementContext.PoolingObjects = new List <IPoolAble>(); pools.Add(hashCode, poolingElementContext); List <IPoolAble> poolAbles = pools[hashCode].PoolingObjects; for (int i = 0; i < min; i++) { IPoolAble poolAble = CreateNew(instantiationMethod); poolAbles.Add(poolAble); } } }
public IPoolAble Get(int hashCode) { var context = pools[hashCode]; IPoolAble poolAbleToReturn = null; foreach (var poolAble in context.PoolingObjects) { if (poolAble.IsInActive) { poolAbleToReturn = poolAble; break; } } if (poolAbleToReturn == null) { IPoolAble poolAble = CreateNew(context.InstationMethod); context.PoolingObjects.Add(poolAble); poolAbleToReturn = poolAble; } return(poolAbleToReturn); }