#if HAVE_NODECANVAS
using System;
using UnityEngine;
using ParadoxNotion.Design;
using NodeCanvas.Framework;
using Framework.Support;
namespace Framework.Cinematics {
/// Fades a standard shader material using its alpha channel
[Category("Material")]
[Description("Fades a CanvasGroup (UGUI) in or out")]
public class FadeMaterialAlphaTask : ActionTask {
/// Game object whose materials will be faded
[RequiredField]
public BBParameter ObjectToFade;
/// Duration of the action in seconds
public float Length;
/// How long the fade-in should take in seconds
public float FadeInDuration;
/// How long the fade-out should take in seconds
public float FadeOutDuration;
/// Summary of what this action does
protected override string info {
get { return "Fade Material"; }
}
/// Called once when the action is executed
protected override void OnExecute() {
this.materials = MaterialHelper.GetMeshMaterials(this.ObjectToFade.value);
int materialCount = this.materials.Length;
this.previousAlpha = new float[materialCount];
for(int index = 0; index < materialCount; ++index) {
previousAlpha[index] = this.materials[index].color.a;
}
}
///
/// Called every frame, if and while the action is running and until it ends
///
protected override void OnUpdate() {
int materialCount = this.materials.Length;
float opacity = InterpolationHelper.InterpolateFade(
elapsedTime, this.Length, this.FadeInDuration, this.FadeOutDuration
);
opacity *= opacity;
for(int index = 0; index < materialCount; ++index) {
Color color = this.materials[index].color;
color.a = opacity;
//color.a = Easing.Ease(this.interpolation, 0.0f, this.previousAlpha[index], weight);
//color.a = Easing.Ease(this.interpolation, 0.0f, 1.0f, weight);
this.materials[index].color = color;
}
if(base.elapsedTime >= this.Length) {
EndAction();
}
}
/// All materials in whom the opacity level will be changed
[NonSerialized]
private Material[] materials;
/// Opacity levels the materials had before the action
[NonSerialized]
private float[] previousAlpha;
}
} // namespace Framework.Cinematics
#endif // HAVE_NODECANVAS