128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
public class ActorManager : MonoBehaviour
|
|
{
|
|
[Header("Actors")]
|
|
private List<Actor> actors = new List<Actor>();
|
|
|
|
[Header("Slots")]
|
|
public List<Transform> slotList = new List<Transform>(); // 用于在编辑器中设置插槽
|
|
private Dictionary<string, Transform> slots = new Dictionary<string, Transform>();
|
|
|
|
[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.GetComponent<SpriteRenderer>(), 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.GetComponent<SpriteRenderer>(), 1f, 0f, duration));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Actor with name {actorName} not found.");
|
|
}
|
|
}
|
|
|
|
[YarnCommand("play_actor_animation")]
|
|
public void PlayActorAnimation(string actorName, string animationName)
|
|
{
|
|
var actor = actors.Find(a => a.name == actorName);
|
|
if (actor != null)
|
|
{
|
|
actor.SetAnimationState(animationName);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Actor with name {actorName} not found.");
|
|
}
|
|
}
|
|
|
|
[YarnCommand("initialize_actor_at_slot")]
|
|
public void InitializeActorAtSlot(string actorName, string slotName)
|
|
{
|
|
var prefab = Resources.Load<GameObject>($"CharacterPrefab/{actorName}");
|
|
if (prefab != null && slots.TryGetValue(slotName, out var slot))
|
|
{
|
|
var actorInstance = Instantiate(prefab, slot.position, slot.rotation,this.transform);
|
|
actorInstance.name = actorName; // 去掉 "(Clone)" 后缀
|
|
var actor = actorInstance.GetComponent<Actor>();
|
|
if (actor != null)
|
|
{
|
|
actors.Add(actor);
|
|
actor.InitializeAtSlot(slot);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Actor prefab with name {actorName} not found or slot with name {slotName} not found.");
|
|
}
|
|
}
|
|
|
|
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 SetActorAlphaZero(SpriteRenderer actor)
|
|
{
|
|
actor.SetAlpha(0);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
InitializeSlots();
|
|
HideAllActors();
|
|
}
|
|
|
|
private void InitializeSlots()
|
|
{
|
|
foreach (var slot in slotList)
|
|
{
|
|
if (slot != null)
|
|
{
|
|
slots[slot.name] = slot;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HideAllActors()
|
|
{
|
|
foreach (var actor in actors)
|
|
{
|
|
var spriteRenderer = actor.GetComponent<SpriteRenderer>();
|
|
if (spriteRenderer != null)
|
|
{
|
|
spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 0f);
|
|
}
|
|
}
|
|
}
|
|
} |