246 lines
6.6 KiB
C#
246 lines
6.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using AibisDream.Kit;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class TimelineCenter : Singleton<TimelineCenter>, ITimelineCenter
|
|
{
|
|
private readonly Dictionary<string, DirectorHandler> _timelineDict = new();
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
RegisterAllDirector();
|
|
}
|
|
|
|
private void RegisterAllDirector()
|
|
{
|
|
var allDirectorHandlers = GetComponentsInChildren<DirectorHandler>(true);
|
|
foreach (var directorHandler in allDirectorHandlers)
|
|
{
|
|
_timelineDict.Add(directorHandler.timelineName, directorHandler);
|
|
}
|
|
}
|
|
|
|
// -------------------------- 播放控制 --------------------------
|
|
|
|
/// <summary>
|
|
/// 播放 Timeline(带完成回调的重载)
|
|
/// </summary>
|
|
public void PlayTimeline(string timelineName, Action onComplete = null)
|
|
{
|
|
if (!_timelineDict.TryGetValue(timelineName, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {timelineName} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.Play(onComplete);
|
|
}
|
|
|
|
public IEnumerator PlayTimelineAsync(string name)
|
|
{
|
|
if (!_timelineDict.TryGetValue(name, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {name} not found");
|
|
yield break;
|
|
}
|
|
|
|
yield return directorHandler.PlayAsync();
|
|
}
|
|
|
|
public void StopTimeline(string timelineName)
|
|
{
|
|
if (!_timelineDict.TryGetValue(timelineName, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {timelineName} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.Stop();
|
|
}
|
|
|
|
public void PauseTimeline(string timelineName)
|
|
{
|
|
if (!_timelineDict.TryGetValue(timelineName, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {timelineName} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.Pause();
|
|
}
|
|
|
|
public void ResumeTimeline(string timelineName)
|
|
{
|
|
if (!_timelineDict.TryGetValue(timelineName, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {timelineName} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.Resume();
|
|
}
|
|
|
|
public void ResetTimeline(string timelineName)
|
|
{
|
|
if (!_timelineDict.TryGetValue(timelineName, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {timelineName} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.Reset();
|
|
}
|
|
|
|
#region 状态查询
|
|
|
|
public double GetPlaybackTime(string name)
|
|
{
|
|
if (!_timelineDict.TryGetValue(name, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {name} not found");
|
|
return 0;
|
|
}
|
|
|
|
return directorHandler.GetPlaybackTime();
|
|
}
|
|
|
|
public double GetDuration(string name)
|
|
{
|
|
if (!_timelineDict.TryGetValue(name, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {name} not found");
|
|
return 0;
|
|
}
|
|
|
|
return directorHandler.GetDuration();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 扩展操作
|
|
|
|
public void SetPlaybackSpeed(string name, float speed)
|
|
{
|
|
if (!_timelineDict.TryGetValue(name, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {name} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.SetPlaybackSpeed(speed);
|
|
}
|
|
|
|
public void SeekToTime(string name, double time)
|
|
{
|
|
if (!_timelineDict.TryGetValue(name, out var directorHandler))
|
|
{
|
|
Debug.LogError($"Timeline {name} not found");
|
|
return;
|
|
}
|
|
|
|
directorHandler.SeekToTime(time);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 批量操作
|
|
|
|
public void PauseAll()
|
|
{
|
|
foreach (var directorHandler in _timelineDict.Values)
|
|
{
|
|
directorHandler.Pause();
|
|
}
|
|
}
|
|
|
|
public void StopAll()
|
|
{
|
|
foreach (var directorHandler in _timelineDict.Values)
|
|
{
|
|
directorHandler.Stop();
|
|
}
|
|
}
|
|
|
|
public void ResetAll()
|
|
{
|
|
foreach (var directorHandler in _timelineDict.Values)
|
|
{
|
|
directorHandler.Reset();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
public interface ITimelineCenter
|
|
{
|
|
void PlayTimeline(string timelineName, Action onComplete = null);
|
|
IEnumerator PlayTimelineAsync(string name);
|
|
void StopTimeline(string timelineName);
|
|
void PauseTimeline(string timelineName);
|
|
void ResumeTimeline(string timelineName);
|
|
void ResetTimeline(string timelineName);
|
|
|
|
#region 状态查询
|
|
|
|
/// <summary>
|
|
/// 获取当前播放时间
|
|
/// </summary>
|
|
/// <param name="name">Timeline 名称</param>
|
|
/// <returns>当前播放时间</returns>
|
|
double GetPlaybackTime(string name);
|
|
|
|
/// <summary>
|
|
/// 获取总时长
|
|
/// </summary>
|
|
/// <param name="name">Timeline 名称</param>
|
|
/// <returns>总时长</returns>
|
|
double GetDuration(string name);
|
|
|
|
#endregion
|
|
|
|
#region 扩展操作
|
|
|
|
/// <summary>
|
|
/// 设置播放速度
|
|
/// </summary>
|
|
/// <param name="name">Timeline 名称</param>
|
|
/// <param name="speed">播放速度</param>
|
|
void SetPlaybackSpeed(string name, float speed);
|
|
|
|
/// <summary>
|
|
/// 跳转到指定时间点
|
|
/// </summary>
|
|
/// <param name="name">Timeline 名称</param>
|
|
/// <param name="time">时间点(秒)</param>
|
|
void SeekToTime(string name, double time);
|
|
|
|
#endregion
|
|
|
|
#region 批量操作
|
|
|
|
/// <summary>
|
|
/// 暂停所有 Timeline
|
|
/// </summary>
|
|
void PauseAll();
|
|
|
|
/// <summary>
|
|
/// 停止所有 Timeline
|
|
/// </summary>
|
|
void StopAll();
|
|
|
|
/// <summary>
|
|
/// 重置所有 Timeline
|
|
/// </summary>
|
|
void ResetAll();
|
|
|
|
#endregion
|
|
}
|
|
}
|