rabidus-test/Assets/DissolveEffect.cs

57 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DissolveEffect : MonoBehaviour
{
List<Renderer> _renderer = new List<Renderer>();
[SerializeField] private string _param = "_Dissolve";
[SerializeField] private float _fromValue = 0;
[SerializeField] private float _toValue = 1;
[SerializeField] private float _time;
private bool _action = false;
private float _timeElapsed = 0;
private float _currentValue = 0;
public AnimationCurve Curve;
private void Awake()
{
_renderer.AddRange(GetComponentsInChildren<Renderer>());
}
[ContextMenu("Dissolve")]
public void Dissolve()
{
_timeElapsed = 0;
_action = true;
}
private void Update()
{
if (!_action)
return;
if (_timeElapsed < _time)
{
_currentValue = Mathf.Lerp(_fromValue, _toValue, _timeElapsed / _time);
for (int i = 0; i < _renderer.Count; i++)
{
for (int h = 0; h < _renderer[i].materials.Length; h++)
{
_renderer[i].materials[h].SetFloat(_param, _currentValue);
}
}
_timeElapsed += Time.deltaTime;
}
else
{
_action = false;
}
}
}