rabidus-test/Assets/DissolveEffect.cs

57 lines
1.3 KiB
C#
Raw Normal View History

2023-10-17 14:17:56 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DissolveEffect : MonoBehaviour
{
List<Renderer> _renderer = new List<Renderer>();
2023-10-18 17:11:47 +03:00
2023-10-20 13:29:44 +03:00
[SerializeField] private string _param = "_Dissolve";
[SerializeField] private float _fromValue = 0;
[SerializeField] private float _toValue = 1;
[SerializeField] private float _time;
2023-10-18 17:11:47 +03:00
2023-10-20 13:29:44 +03:00
private bool _action = false;
private float _timeElapsed = 0;
private float _currentValue = 0;
2023-10-18 17:11:47 +03:00
2023-10-23 16:05:01 +03:00
public AnimationCurve Curve;
2023-10-20 13:29:44 +03:00
private void Awake()
2023-10-17 14:17:56 +03:00
{
_renderer.AddRange(GetComponentsInChildren<Renderer>());
}
[ContextMenu("Dissolve")]
public void Dissolve()
{
2023-10-20 14:30:47 +03:00
_timeElapsed = 0;
2023-10-20 13:29:44 +03:00
_action = true;
}
private void Update()
{
if (!_action)
return;
if (_timeElapsed < _time)
2023-10-17 14:17:56 +03:00
{
2023-10-20 13:29:44 +03:00
_currentValue = Mathf.Lerp(_fromValue, _toValue, _timeElapsed / _time);
2023-10-17 14:17:56 +03:00
for (int i = 0; i < _renderer.Count; i++)
{
for (int h = 0; h < _renderer[i].materials.Length; h++)
{
2023-10-20 13:29:44 +03:00
_renderer[i].materials[h].SetFloat(_param, _currentValue);
2023-10-17 14:17:56 +03:00
}
}
2023-10-20 13:29:44 +03:00
_timeElapsed += Time.deltaTime;
}
2023-10-20 14:30:47 +03:00
else
{
_action = false;
}
2023-10-17 14:17:56 +03:00
}
}