77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
public class ActorManager : MonoBehaviour
|
|
{
|
|
[Header("Actors")]
|
|
public List<SpriteRenderer> actors;
|
|
|
|
[YarnCommand("fade_in_actor")]
|
|
public IEnumerator FadeInActorByName(string actorName, float duration)
|
|
{
|
|
var actor = actors.Find(a => a.name == actorName);
|
|
if (actor != null)
|
|
{
|
|
yield return StartCoroutine(FadeActor(actor, 0f, 1f, duration));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Actor with name {actorName} not found.");
|
|
}
|
|
}
|
|
|
|
[YarnCommand("fade_out_actor")]
|
|
public IEnumerator FadeOutActorByName(string actorName, float duration)
|
|
{
|
|
var actor = actors.Find(a => a.name == actorName);
|
|
if (actor != null)
|
|
{
|
|
yield return StartCoroutine(FadeActor(actor, 1f, 0f, duration));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Actor with name {actorName} not found.");
|
|
}
|
|
}
|
|
|
|
public void FadeInActor(SpriteRenderer actor, float duration)
|
|
{
|
|
StartCoroutine(FadeActor(actor, 0f, 1f, duration));
|
|
}
|
|
|
|
public void FadeOutActor(SpriteRenderer actor, float duration)
|
|
{
|
|
StartCoroutine(FadeActor(actor, 1f, 0f, duration));
|
|
}
|
|
|
|
private IEnumerator FadeActor(SpriteRenderer actor, float startAlpha, float endAlpha, float duration)
|
|
{
|
|
float elapsedTime = 0f;
|
|
Color color = actor.color;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
|
|
actor.color = new Color(color.r, color.g, color.b, alpha);
|
|
yield return null;
|
|
}
|
|
|
|
actor.color = new Color(color.r, color.g, color.b, endAlpha);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
HideAllActors();
|
|
}
|
|
|
|
private void HideAllActors()
|
|
{
|
|
foreach (var actor in actors)
|
|
{
|
|
actor.color = new Color(actor.color.r, actor.color.g, actor.color.b, 0f);
|
|
}
|
|
}
|
|
} |