//coroutine which waits for 'time' seconds before deactivating the instance IEnumerator DespawnInTime(PoolTimeObject timeObject) { //cache instance to deactivate GameObject instance = timeObject.instance; if (instance == null) { yield break; } //wait for defined seconds float timer = Time.time + timeObject.time; while (instance.activeInHierarchy && Time.time < timer) { yield return(null); } //the instance got deactivated in between already if (!instance.activeInHierarchy) { yield break; } //despawn it now Despawn(instance); }
//similiar to Despawn(), but also yields for 'PoolTimeObject.time' seconds //started by Despawn(instance, time) above IEnumerator DespawnInTime(PoolTimeObject timeObject) { //cache instance to deactivate GameObject instance = timeObject.instance; //wait for defined seconds yield return(new WaitForSeconds(timeObject.time)); //loop through all pool options of this component for (int cnt = 0; cnt < this._PoolOptions.Count; cnt++) { PoolOptions poolOptions = this._PoolOptions[cnt]; //seach in active instances for this instance if (poolOptions.active.Contains(instance)) { //in case it was unparented during runtime, reparent it now if (instance.transform.parent != container) { instance.transform.parent = container; } //let Deactivate() of PoolOption disable this instance poolOptions.Deactivate(instance); //remove instance from our active list of this component this._AllActive.Remove(instance); } } }
////根据时间将GameObject设置回不被激活 public void DeSpawn(GameObject instance, float time) { //create new class PoolTimeObject to keep track of the instance PoolTimeObject timeObject = new PoolTimeObject(); //assign time and instance variable of this class timeObject.instance = instance; timeObject.time = time; //start timed deactivation using the created properties StartCoroutine(DespawnInTime(timeObject)); }
//timed deactivation of an instance public void Despawn(GameObject instance, float time) { //ahead: we need to start a coroutine which waits for 'time' seconds //and then deactivates the instance, but StartCoroutine() nor Invoke() //takes 2 parameters, so we use an own class to store these values //create new class PoolTimeObject PoolTimeObject timeObject = new PoolTimeObject(); //assign time and instance variable of this class timeObject.instance = instance; timeObject.time = time; //start timed deactivation and pass in the created PoolTimeObject class StartCoroutine("DespawnInTime", timeObject); }
IEnumerator DespawnInTime(PoolTimeObject timeObject) { GameObject instance = timeObject.instance; float timer = Time.time + timeObject.time; while (instance.activeInHierarchy && Time.time < timer) { yield return(null); } if (!instance.activeInHierarchy) { yield break; } DeSpawn(instance); }
//similiar to Despawn(), but also yields for 'PoolTimeObject.time' seconds //started by Despawn(instance, time) above IEnumerator DespawnInTime(PoolTimeObject timeObject) { //cache instance to deactivate GameObject instance = timeObject.instance; //wait for defined seconds yield return new WaitForSeconds(timeObject.time); //loop through all pool options of this component for (int cnt = 0; cnt < this._PoolOptions.Count; cnt++) { PoolOptions poolOptions = this._PoolOptions[cnt]; //seach in active instances for this instance if (poolOptions.active.Contains(instance)) { //in case it was unparented during runtime, reparent it now if (instance.transform.parent != container) instance.transform.parent = container; //let Deactivate() of PoolOption disable this instance poolOptions.Deactivate(instance); //remove instance from our active list of this component this._AllActive.Remove(instance); } } }