Created
August 16, 2016 21:47
-
-
Save CloudyWater/d556dd1610c787b2a19727aef163a5f8 to your computer and use it in GitHub Desktop.
Dash Trail in Unity, now with object pool!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class DashTrail : MonoBehaviour | |
{ | |
public SpriteRenderer mLeadingSprite; | |
public int mTrailSegments; | |
public float mTrailTime; | |
public GameObject mTrailObject; | |
private float mSpawnInterval; | |
private float mSpawnTimer; | |
private bool mbEnabled; | |
private List<GameObject> mTrailObjectsInUse; | |
private Queue<GameObject> mTrailObjectsNotInUse; | |
// Use this for initialization | |
void Start () | |
{ | |
mSpawnInterval = mTrailTime / mTrailSegments; | |
mTrailObjectsInUse = new List<GameObject> (); | |
mTrailObjectsNotInUse = new Queue<GameObject> (); | |
for (int i = 0; i < mTrailSegments; i++) | |
{ | |
GameObject trail = GameObject.Instantiate (mTrailObject); | |
trail.transform.SetParent (transform); | |
mTrailObjectsNotInUse.Enqueue (trail); | |
} | |
mbEnabled = false; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
if (mbEnabled) | |
{ | |
mSpawnTimer += Time.deltaTime; | |
if (mSpawnTimer >= mSpawnInterval) | |
{ | |
GameObject trail = mTrailObjectsNotInUse.Dequeue (); | |
if (trail != null) | |
{ | |
DashTrailObject trailObject = trail.GetComponent<DashTrailObject> (); | |
trailObject.Initiate (mTrailTime, mLeadingSprite.sprite, transform.position, this); | |
mTrailObjectsInUse.Add (trail); | |
mSpawnTimer = 0; | |
} | |
} | |
} | |
} | |
public void RemoveTrailObject (GameObject obj) | |
{ | |
mTrailObjectsInUse.Remove (obj); | |
mTrailObjectsNotInUse.Enqueue (obj); | |
} | |
public void SetEnabled (bool enabled) | |
{ | |
mbEnabled = enabled; | |
if (enabled) | |
{ | |
mSpawnTimer = mSpawnInterval; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment