private GhostingSprite BuildNewGhostingSprite() { //create a gameobject and set the current transform as a parent GameObject go = new GameObject(); go.transform.position = transform.position; go.transform.parent = GhostSpritesParent.transform; GhostingSprite gs = go.AddComponent <GhostingSprite>(); return(gs); }
void Update() { if (_hasStarted) { //check for spawn rate //check if we're ok to spawn a new ghost if (Time.time >= _nextSpawnTime) { //is the queue count number equal than the trail length? if (GhostingSpritesQueue.Count == _trailLength) { GhostingSprite peekedGhostingSprite = GhostingSpritesQueue.Peek(); //is it ok to use? bool canBeReused = peekedGhostingSprite.CanBeReused(); if (canBeReused) { //pop the queue GhostingSpritesQueue.Dequeue(); GhostingSpritesQueue.Enqueue(peekedGhostingSprite); //initialize the ghosting sprite if (!useTint) { peekedGhostingSprite.Init(_effectDuration, _desiredAlpha, _refSpriteRenderer.sprite, _sortingLayer, _refSpriteRenderer.sortingOrder - 1, transform, Vector3.zero); } else { peekedGhostingSprite.Init(_effectDuration, _desiredAlpha, _refSpriteRenderer.sprite, _sortingLayer, _refSpriteRenderer.sortingOrder - 1, transform, Vector3.zero, _desiredColor); } _nextSpawnTime += _spawnRate; } else //not ok, wait until next frame to try again { //peekedGhostingSprite.KillAnimationAndSpeedUpDissapearing(); return; } } //check if the count is less than the trail length, we need to create a new ghosting sprite if (GhostingSpritesQueue.Count < _trailLength) { GhostingSprite newGhostingSprite = Get(); GhostingSpritesQueue.Enqueue(newGhostingSprite); //queue it up! //newGhostingSprite.Init(_effectDuration, _desiredAlpha, _refSpriteRenderer.sprite, _sortingLayer,_refSpriteRenderer.sortingOrder-1, transform, Vector3.zero ); if (!useTint) { newGhostingSprite.Init(_effectDuration, _desiredAlpha, _refSpriteRenderer.sprite, _sortingLayer, _refSpriteRenderer.sortingOrder - 1, transform, Vector3.zero); } else { newGhostingSprite.Init(_effectDuration, _desiredAlpha, _refSpriteRenderer.sprite, _sortingLayer, _refSpriteRenderer.sortingOrder - 1, transform, Vector3.zero, _desiredColor); } _nextSpawnTime += _spawnRate; } //check if the queue count is greater than the trail length. Dequeue these items off the queue, as they are no longer needed if (GhostingSpritesQueue.Count > _trailLength) { int difference = GhostingSpritesQueue.Count - _trailLength; for (int i = 1; i < difference; i++) { GhostingSprite gs = GhostingSpritesQueue.Dequeue(); InactiveGhostSpritePool.Add(gs); } return; } } } }