103 lines
2.4 KiB
C#
103 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using MyBT;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
[CustomEditor(typeof(NamedFadeScene))]
|
|
public class NamedFadeSceneInspector : ComponentHandlerInspector
|
|
{
|
|
}
|
|
#endif
|
|
|
|
[System.Serializable]
|
|
public class NamedFadeScene : ComponentHandler
|
|
{
|
|
public override string TypeLabel()
|
|
{
|
|
return "FadeScene";
|
|
}
|
|
|
|
public override string ContentLabel()
|
|
{
|
|
UpdateComponent();
|
|
return "Black";
|
|
}
|
|
|
|
public override void UpdateComponent()
|
|
{
|
|
base.UpdateComponent();
|
|
_renderer = GetComponent<Renderer>();
|
|
if (_renderer == null)
|
|
{
|
|
Debug.LogError("Renderer component not found on this GameObject.");
|
|
return;
|
|
}
|
|
_material = _renderer.sharedMaterial;
|
|
}
|
|
|
|
public float duration = 5f;
|
|
|
|
private Renderer _renderer;
|
|
private Material _material;
|
|
|
|
|
|
public override void FadeOut(NodeState nodeState)
|
|
{
|
|
switch (nodeState)
|
|
{
|
|
case NodeState.FirstRun:
|
|
gameObject.SetActive(true);
|
|
StartCoroutine(FadeAlpha(0f, 1f));
|
|
break;
|
|
case NodeState.Running:
|
|
if (_material.color.a == 1f)
|
|
{
|
|
Task.SetSucceeded();
|
|
}
|
|
break;
|
|
case NodeState.Aborting:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void FadeIn(NodeState nodeState)
|
|
{
|
|
switch (nodeState)
|
|
{
|
|
case NodeState.FirstRun:
|
|
StartCoroutine(FadeAlpha(1f, 0f));
|
|
break;
|
|
case NodeState.Running:
|
|
if (_material.color.a == 0f)
|
|
{
|
|
gameObject.SetActive(false);
|
|
Task.SetSucceeded();
|
|
}
|
|
break;
|
|
case NodeState.Aborting:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private IEnumerator FadeAlpha(float startAlpha, float endAlpha)
|
|
{
|
|
float elapsedTime = 0f;
|
|
Color color = _material.color;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
|
|
color.a = alpha;
|
|
_material.color = color;
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure the final alpha value is set
|
|
color.a = endAlpha;
|
|
_material.color = color;
|
|
}
|
|
}
|