private void CheckForUpdate() { if (imagesToDisplayCopy != imagesToDisplay) { foreach (Transform t in images) { Destroy(t.gameObject); // t.gameObject.SetActive(true); } images = new List <Transform>(imagesToDisplay); for (int i = 0; i < imagesToDisplay; i++) { afterImage = new GameObject("_afterimage"); Afterimage image = afterImage.AddComponent <Afterimage>(); SpriteRenderer imageSprite = afterImage.AddComponent <SpriteRenderer>(); images.Add(afterImage.transform); imageSprite.color = imageColor; if (state == ImageState.OFF) { afterImage.gameObject.SetActive(false); } image.imageInChain = i + 1; image.maxOffset = image.imageInChain * delayBetweenImages; imageSprite.sortingOrder = sprite.sortingOrder - i - 1; afterImage.transform.SetParent(parent.transform, true); } imagesToDisplayCopy = imagesToDisplay; } if (delayBetweenImagesCopy != delayBetweenImages) { for (int i = 0; i < images.Count; i++) { Afterimage image = images[i].gameObject.GetComponent <Afterimage>(); image.maxOffset = image.imageInChain * delayBetweenImages; } delayBetweenImagesCopy = delayBetweenImages; } }
// Use this for initialization void Start() { Application.targetFrameRate = 60; imagePositions = new Vector3[positionsToSave]; savedSprites = new Sprite[positionsToSave]; images = new List <Transform>(imagesToDisplay); sprite = GetComponent <SpriteRenderer>(); parent = new GameObject("Afterimages"); parent.transform.position = new Vector3(0f, 0f, 0f); for (int i = 0; i < imagesToDisplay; i++) { afterImage = new GameObject("_afterimage"); Afterimage image = afterImage.AddComponent <Afterimage>(); SpriteRenderer imageSprite = afterImage.AddComponent <SpriteRenderer>(); images.Add(afterImage.transform); if (!useMulticolors) { imageSprite.color = imageColor; } else { imageSprite.color = multicolors[i]; } afterImage.gameObject.SetActive(false); image.imageInChain = i + 1; image.maxOffset = image.imageInChain * delayBetweenImages; imageSprite.sortingOrder = sprite.sortingOrder - i - 1; afterImage.transform.SetParent(parent.transform, true); } imagesToDisplayCopy = imagesToDisplay; delayBetweenImagesCopy = delayBetweenImages; }
// Update is called once per frame void Update() { CheckForUpdate(); switch (state) { case ImageState.ON: { imagePositions[index] = this.gameObject.transform.position; savedSprites[index] = sprite.sprite; foreach (Transform t in images) { if (t.gameObject.activeSelf == false) { t.gameObject.SetActive(true); } Afterimage image = t.GetComponent <Afterimage>(); t.position = this.gameObject.transform.position; // If this value is null, it must have started generating images and not looped around yet. if (imagePositions[positionsToSave - 1] == Vector3.zero) { int num = index - image.offset; if (num < 0) { num = 0; } t.position = imagePositions[num]; } else { t.position = imagePositions[SubtractAndWrap(index, image.offset, positionsToSave)]; } t.localScale = transform.localScale; SpriteRenderer imageSprite = t.GetComponent <SpriteRenderer>(); if (!saveSprites) { imageSprite.sprite = sprite.sprite; } else { imageSprite.sprite = savedSprites[SubtractAndWrap(index, image.offset, positionsToSave)]; } if (image.offset < image.maxOffset) { image.offset++; } } index++; if (index >= positionsToSave) { index = 0; } } break; case ImageState.OFF: break; case ImageState.FADING: { imagePositions[index] = this.gameObject.transform.position; foreach (Transform t in images) { Afterimage image = t.GetComponent <Afterimage>(); // If this value is null, it must have started generating images. if (imagePositions[positionsToSave - 1] == Vector3.zero) { int num = index - image.offset; if (num < 0) { num = 0; } t.position = imagePositions[num]; } else { t.position = imagePositions[SubtractAndWrap(index, image.offset, positionsToSave)]; } t.localScale = transform.localScale; if (!saveSprites) { t.GetComponent <SpriteRenderer>().sprite = sprite.sprite; } else { t.GetComponent <SpriteRenderer>().sprite = savedSprites[SubtractAndWrap(index, image.offset, positionsToSave)]; } if (image.offset > 0) { image.offset--; } if (image.imageInChain == imagesToDisplay && image.offset == 0) { disableImages(); state = ImageState.OFF; break; } } index++; if (index >= positionsToSave) { index = 0; } } break; } }