Skip to content

Instantly share code, notes, and snippets.

@haramakoto
Created February 8, 2023 06:34
Show Gist options
  • Save haramakoto/50e97ffe7a94f94eb177d25493a692d5 to your computer and use it in GitHub Desktop.
Save haramakoto/50e97ffe7a94f94eb177d25493a692d5 to your computer and use it in GitHub Desktop.
単純にマテリアルをDoTweenでフェードさせる
namespace hoge.hoge
{
using System;
using DG.Tweening;
using UnityEngine;
public class MaterialFader : MonoBehaviour
{
[SerializeField]
private Material[] alphaMaterials;
[SerializeField]
private string paramName;
private float alphaVal;
private Action onCompleteFadeOut;
private Action onCompleteFadeIn;
public void FadeIn(Action onCompAction = null, float delay = 0, float speed = 1.0f)
{
if (onCompAction != null)
{
this.onCompleteFadeIn += onCompAction;
}
DOTween.To(
() => this.alphaVal,
num => this.alphaVal = num,
1,
speed).OnUpdate(this.OnUpdateAlpha).SetDelay(delay).OnComplete(() =>
{
this.onCompleteFadeIn?.Invoke();
this.onCompleteFadeIn = null;
});
}
public void FadeOut(Action onCompAction = null, float delay = 0, float speed = 1.0f)
{
if (onCompAction != null)
{
this.onCompleteFadeOut += onCompAction;
}
DOTween.To(
() => this.alphaVal,
num => this.alphaVal = num,
0,
speed).OnUpdate(this.OnUpdateAlpha).SetDelay(delay).OnComplete(() =>
{
this.onCompleteFadeOut?.Invoke();
this.onCompleteFadeOut = null;
});
}
private void OnUpdateAlpha()
{
foreach (Material mat in this.alphaMaterials)
{
mat.SetColor(this.paramName, new Color(mat.color.r, mat.color.g, mat.color.b, this.alphaVal));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment