You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.8 KiB
80 lines
1.8 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.Events; |
|
using UnityEngine.Serialization; |
|
|
|
public class ParticleFade : MonoBehaviour |
|
{ |
|
public ParticleSystemRenderer particle; |
|
public Material mat; |
|
float init = 0; |
|
void Init() |
|
{ |
|
if (mat == null) |
|
{ |
|
if (particle == null) |
|
particle = GetComponent<ParticleSystemRenderer>(); |
|
mat = new Material(particle.material); |
|
particle.material = mat; |
|
init = mat.GetFloat($"{alpha}"); |
|
} |
|
} |
|
//private void OnEnable() |
|
//{ |
|
// Init(); |
|
// mat.SetFloat($"{alpha}", init); |
|
//} |
|
private void OnDisable() |
|
{ |
|
Init(); |
|
mat.SetFloat($"{alpha}", init); |
|
|
|
durationSum = 0; |
|
isPlay = false; |
|
} |
|
public void Play() |
|
{ |
|
Init(); |
|
mat.SetFloat($"{alpha}", init); |
|
|
|
durationSum = 0; |
|
isPlay = true; |
|
} |
|
|
|
[Space(10)] |
|
public bool isPlay = false; |
|
|
|
public string alpha; |
|
public float value; |
|
public float duration = 3f; |
|
float durationSum = 0; |
|
public AnimationCurve curve; |
|
float timeCurve; |
|
|
|
[System.Serializable] |
|
public class EndEvent : UnityEvent<object> { } |
|
[FormerlySerializedAs("onEnd")] |
|
[SerializeField] |
|
private EndEvent OnEnd = new EndEvent(); |
|
private void Update() |
|
{ |
|
if (isPlay && mat != null) |
|
{ |
|
durationSum += Time.deltaTime; |
|
timeCurve = durationSum / duration; |
|
|
|
mat.SetFloat($"{alpha}", value * curve.Evaluate(timeCurve)); |
|
|
|
if (duration < durationSum) |
|
{ |
|
durationSum = 0; |
|
|
|
isPlay = false; |
|
//enabled = isPlay; |
|
|
|
OnEnd.Invoke(null); |
|
} |
|
} |
|
} |
|
}
|
|
|