186 lines
5.6 KiB
C#
186 lines
5.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class ActorManager : Singleton<ActorManager>
|
|
{
|
|
#region 索引
|
|
|
|
private Transform _actorRoot;
|
|
private Dictionary<string, ActorSlot> _slotDict;
|
|
private Dictionary<string, AbsActor> _actorDict;
|
|
|
|
private ActorFactory _actorFactory;
|
|
|
|
#endregion
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitRefs();
|
|
DestroyEditorTemp();
|
|
InitDefaultActor();
|
|
}
|
|
|
|
public void DestroyEditorTemp()
|
|
{
|
|
foreach (Transform child in _actorRoot)
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (!Application.isPlaying)
|
|
{
|
|
DestroyImmediate(child.gameObject);
|
|
continue;
|
|
}
|
|
#endif
|
|
Destroy(child.gameObject);
|
|
}
|
|
}
|
|
|
|
private void InitDefaultActor()
|
|
{
|
|
foreach (var slot in _slotDict.Values.Where(slot => slot.hasDefaultActor))
|
|
{
|
|
InitActor(slot.defaultActorName, slot.slotName, slot.defaultActorType);
|
|
}
|
|
}
|
|
|
|
private void InitRefs()
|
|
{
|
|
_actorRoot = transform.Find("ActorRoot");
|
|
_slotDict = new Dictionary<string, ActorSlot>();
|
|
foreach (var child in transform.Find("Slots").GetComponentsInChildren<ActorSlot>())
|
|
{
|
|
_slotDict[child.slotName] = child;
|
|
}
|
|
|
|
_actorDict = new Dictionary<string, AbsActor>();
|
|
_actorFactory = new ActorFactory(_actorRoot);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public void InitAllDefault()
|
|
{
|
|
InitRefs();
|
|
DestroyEditorTemp();
|
|
foreach (var slot in _slotDict.Values)
|
|
{
|
|
var actor = InitActor(slot.defaultActorName, slot.slotName, slot.defaultActorType);
|
|
actor.Show();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
public AbsActor InitActor(string actorName, string slotName, ActorType actorType)
|
|
{
|
|
var slot = _slotDict[slotName];
|
|
// 生成新组件
|
|
var actor = _actorFactory.CreateActor(actorName, slot, actorType);
|
|
// 保存
|
|
_actorDict.Add(actorName, actor);
|
|
|
|
return actor;
|
|
}
|
|
|
|
public bool TryFindActor(string actorOrSlot, out AbsActor actor)
|
|
{
|
|
if (_actorDict.TryGetValue(actorOrSlot, out actor))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
actor = _actorDict.Values.FirstOrDefault(item => item.SlotName == actorOrSlot);
|
|
return actor != null;
|
|
}
|
|
|
|
public void ChangeSlot(string actorOrSlot, string targetSlot)
|
|
{
|
|
if (!_slotDict.TryGetValue(targetSlot, out var targetSlotObj)) return;
|
|
if (!TryFindActor(actorOrSlot, out var actor)) return;
|
|
actor.SetSlot(targetSlotObj);
|
|
}
|
|
}
|
|
|
|
public static class ActorYarnCommand
|
|
{
|
|
private const string ClinicSlotName = "clinic";
|
|
|
|
[YarnCommand("init_actor")]
|
|
public static void InitActor(string actorName, string slotName = ClinicSlotName, string actorType = "Anima")
|
|
{
|
|
if (Enum.TryParse<ActorType>(actorType, out var actorTypeEnum))
|
|
{
|
|
ActorManager.Instance.InitActor(actorName, slotName, actorTypeEnum);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"Actor类型{actorType}有问题");
|
|
}
|
|
}
|
|
|
|
[YarnCommand("change_actor_state")]
|
|
public static void PlayAnima(string animaName, string slotOrActorName = ClinicSlotName)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(slotOrActorName, out var actor))
|
|
{
|
|
actor.ChangeState(animaName);
|
|
}
|
|
}
|
|
|
|
[YarnCommand("change_actor_state_async")]
|
|
public static IEnumerator PlayAnimaAsync(string animaName, string slotOrActorName = ClinicSlotName)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(slotOrActorName, out var actor))
|
|
{
|
|
yield return actor.ChangeStateAsync(animaName);
|
|
}
|
|
}
|
|
|
|
[YarnCommand("fade_in_actor")]
|
|
public static IEnumerator ActorFadeIn(string actorName = ClinicSlotName, float duration = 1)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(actorName, out var actor))
|
|
{
|
|
yield return actor.FadeInAsync(duration);
|
|
}
|
|
}
|
|
|
|
[YarnCommand("fade_out_actor")]
|
|
public static IEnumerator ActorFadeOut(string actorName = ClinicSlotName, float duration = 1)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(actorName, out var actor))
|
|
{
|
|
yield return actor.FadeOutAsync(duration);
|
|
}
|
|
}
|
|
|
|
[YarnCommand("change_actor_slot")]
|
|
public static void ChangeSlot(string actorOrSlot, string targetSlot)
|
|
{
|
|
ActorManager.Instance.ChangeSlot(actorOrSlot, targetSlot);
|
|
}
|
|
|
|
[YarnCommand("show_actor")]
|
|
public static void ShowActor(string actorName = ClinicSlotName)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(actorName, out var actor))
|
|
{
|
|
actor.Show();
|
|
}
|
|
}
|
|
|
|
[YarnCommand("hide_actor")]
|
|
public static void HideActor(string actorName = ClinicSlotName)
|
|
{
|
|
if (ActorManager.Instance.TryFindActor(actorName, out var actor))
|
|
{
|
|
actor.Hide();
|
|
}
|
|
}
|
|
}
|
|
} |