87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class UITrailEffect : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("잔상 설정")]
|
|||
|
|
public float spawnInterval = 0.02f; // 잔상 촘촘하게 (수치 낮춤)
|
|||
|
|
public float trailLifeTime = 0.4f; // 잔상 사라지는 속도
|
|||
|
|
|
|||
|
|
private float timer;
|
|||
|
|
private Image myImage;
|
|||
|
|
private RectTransform myRect;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
myImage = GetComponent<Image>();
|
|||
|
|
myRect = GetComponent<RectTransform>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
// 시간이 멈춰있어도(TimeScale=0) 강제로 타이머를 돌림
|
|||
|
|
timer += Time.unscaledDeltaTime;
|
|||
|
|
if (timer >= spawnInterval)
|
|||
|
|
{
|
|||
|
|
timer = 0;
|
|||
|
|
SpawnTrail();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SpawnTrail()
|
|||
|
|
{
|
|||
|
|
// 1. 잔상 오브젝트 생성
|
|||
|
|
GameObject trail = new GameObject("Trail_Clone");
|
|||
|
|
trail.transform.SetParent(transform.parent);
|
|||
|
|
trail.transform.position = transform.position;
|
|||
|
|
|
|||
|
|
// 2. 내 이미지 복사
|
|||
|
|
Image trailImage = trail.AddComponent<Image>();
|
|||
|
|
trailImage.sprite = myImage.sprite;
|
|||
|
|
trailImage.color = myImage.color;
|
|||
|
|
trailImage.material = myImage.material;
|
|||
|
|
|
|||
|
|
// 3. 자폭 타이머 스크립트 붙여주기
|
|||
|
|
RectTransform trailRect = trail.GetComponent<RectTransform>();
|
|||
|
|
trailRect.sizeDelta = myRect.sizeDelta * 0.8f; // 본체보다 약간 작게
|
|||
|
|
|
|||
|
|
TrailFader fader = trail.AddComponent<TrailFader>();
|
|||
|
|
fader.Init(trailLifeTime, trailImage, trailRect);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ⭐ 잔상이 스스로 투명해지다가 파괴되는 전용 스크립트
|
|||
|
|
public class TrailFader : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
private float lifeTime;
|
|||
|
|
private Image img;
|
|||
|
|
private RectTransform rect;
|
|||
|
|
private float timer;
|
|||
|
|
private Color startColor;
|
|||
|
|
private Vector2 startSize;
|
|||
|
|
|
|||
|
|
public void Init(float life, Image image, RectTransform rectTransform)
|
|||
|
|
{
|
|||
|
|
lifeTime = life;
|
|||
|
|
img = image;
|
|||
|
|
rect = rectTransform;
|
|||
|
|
startColor = img.color;
|
|||
|
|
startSize = rect.sizeDelta;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
timer += Time.unscaledDeltaTime;
|
|||
|
|
float p = timer / lifeTime;
|
|||
|
|
|
|||
|
|
if (p >= 1f)
|
|||
|
|
{
|
|||
|
|
Destroy(gameObject); // 수명 다하면 즉시 파괴!
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 점점 투명하게, 점점 작게
|
|||
|
|
img.color = new Color(startColor.r, startColor.g, startColor.b, 1f - p);
|
|||
|
|
rect.sizeDelta = startSize * (1f - p);
|
|||
|
|
}
|
|||
|
|
}
|