67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
|
using UnityEngine;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
public class ParticleTrailAlpha : ParticleCleaner
|
|||
|
{
|
|||
|
private static int? colorId;
|
|||
|
private float? _fadeStartTime;
|
|||
|
private readonly List<TrailMaterialRecord> _trailAlphaList = new List<TrailMaterialRecord>();
|
|||
|
|
|||
|
protected override void Awake()
|
|||
|
{
|
|||
|
base.Awake();
|
|||
|
if (colorId == null)
|
|||
|
colorId = Shader.PropertyToID("_Alpha");
|
|||
|
for (var i = 0; i < trailRendererList.Count; i++)
|
|||
|
{
|
|||
|
var materials = trailRendererList[i].materials;
|
|||
|
for (var j = 0; j < materials.Length; j++)
|
|||
|
{
|
|||
|
var material = materials[j];
|
|||
|
_trailAlphaList.Add(new TrailMaterialRecord(material));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected override void EnableComponents(bool isEnable)
|
|||
|
{
|
|||
|
base.EnableComponents(isEnable);
|
|||
|
_fadeStartTime = null;
|
|||
|
if (isEnable)
|
|||
|
for (var i = 0; i < _trailAlphaList.Count; i++)
|
|||
|
_trailAlphaList[i].SetRatio(1f);
|
|||
|
}
|
|||
|
|
|||
|
public override void StartDelayRecovery()
|
|||
|
{
|
|||
|
base.StartDelayRecovery();
|
|||
|
_fadeStartTime = Time.unscaledTime;
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
if (_fadeStartTime != null && DelayRecoveryTime > 0f)
|
|||
|
{
|
|||
|
var ratio = Mathf.Clamp01((Time.unscaledTime - _fadeStartTime.Value) / DelayRecoveryTime);
|
|||
|
for (var i = 0; i < _trailAlphaList.Count; i++)
|
|||
|
_trailAlphaList[i].SetRatio(1f - ratio);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private class TrailMaterialRecord
|
|||
|
{
|
|||
|
private readonly Material _material;
|
|||
|
private readonly float _baseAlpha;
|
|||
|
|
|||
|
public TrailMaterialRecord(Material material)
|
|||
|
{
|
|||
|
_material = material;
|
|||
|
_baseAlpha = material.GetFloat(colorId.Value);
|
|||
|
}
|
|||
|
|
|||
|
public void SetRatio(float ratio)
|
|||
|
{
|
|||
|
_material.SetFloat(colorId.Value, ratio * _baseAlpha);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|