public void UnspawnParticle(GameObject gameObject) { //check has ParticleClip ParticleClip particleClip = gameObject.GetComponent <ParticleClip>(); if (particleClip != null) { gameObject.SetActive(false); particleClip.Reset(); //remove the particle from the active list m_ActiveParticle.Remove(particleClip); } else { Debug.LogError("The object to be unspawned doesn't have ParticleClip component"); } }
/** * Create the prefabs for the pool */ GameObject SpawnForPool(GameObject prefab, List <ParticleClip> pool) { GameObject instance = Instantiate(prefab); //disable this at the moment instance.SetActive(false); //add the ParticleClip so it can accessed by this ParticleManager ParticleClip particleClip = instance.GetComponent <ParticleClip>(); pool.Add(particleClip); //set the parent to this (ParticleManager's) game object //so our hierarchy looks clean instance.transform.parent = this.gameObject.transform; return(instance); }
/** * Spawn the object, set the position, and let it die after some seconds */ public GameObject Spawn(string particleName, Vector3 position, float dieAfterSecond) { if (m_IsPause) { return(null); } //get the Particle info from dictionary ParticlePool particle; if (m_ParticleDictionary.TryGetValue(particleName, out particle)) { //get the list from the pool dictionary List <ParticleClip> pool; GameObject spawnObject = null; if (m_ParticlePool.TryGetValue(particleName, out pool)) { //search the inactive game object bool found = false; int i = 0; while (!found && i < pool.Count) { GameObject currentGameObject = pool[i].gameObject; if (!currentGameObject.activeInHierarchy) { spawnObject = currentGameObject; found = true; } i++; } //check the game object available if (spawnObject != null) { //set position if exist spawnObject.transform.position = position; ParticleClip pc = spawnObject.GetComponent <ParticleClip>(); pc.Reset(); //set the die time if (pc.DieTime <= 0) { pc.DieTime = dieAfterSecond; } //set the GameObject active spawnObject.SetActive(true); //add the spawned object to our active list m_ActiveParticle.Add(pc); //return the succeedly found object. YAY! return(spawnObject); } else { //create new object for particle pool if (particle.isExpanding) { GameObject go = SpawnForPool(particle.particlePrefab, pool); return(go); } else { Debug.LogError("No inactive gameobjects for " + particleName); return(null); } } } else { Debug.LogError("No prefabs called " + particleName + " in particle pool dictionary"); return(null); } } else { Debug.LogError("No prefabs called " + particleName + " in dictionary"); return(null); } }