using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Playables; namespace BF { public class TimeLineManager : ManagerBase { private List gameObjectIds; private List timeLineItems; private List removeTimelineIdxs; static TimeLineManager instance; public static TimeLineManager Create() { BFLog.LogAssert(instance == null, "This method only allows BFMain to call once"); instance = new TimeLineManager(); instance.Init(); return instance; } public override void Init() { gameObjectIds = new List(); timeLineItems = new List(); removeTimelineIdxs = new List(); } public override void Update() { removeTimelineIdxs.Clear(); int count = timeLineItems.Count; for (int i = 0; i < count;) { timeLineItems[i].FrameMove(Time.deltaTime); if (!removeTimelineIdxs.Contains(i)) i++; else count--; } } public override void LateUpdate() { } public override void Destroy() { gameObjectIds.Clear(); timeLineItems.Clear(); removeTimelineIdxs.Clear(); } public TimeLineItem Load(GameObject tlObject) { int gameObjectId = tlObject.GetInstanceID(); int idx = gameObjectIds.IndexOf(gameObjectId); if (idx >= 0) return timeLineItems[idx]; var playableDirector = tlObject.GetComponent(); if (null == playableDirector) return null; TimeLineItem tlItem = new TimeLineItem(playableDirector); gameObjectIds.Add(gameObjectId); timeLineItems.Add(tlItem); return tlItem; } public bool Unload(GameObject tlObject) { int gameObjectId = tlObject.GetInstanceID(); int idx = gameObjectIds.IndexOf(gameObjectId); if (idx >= 0) { timeLineItems[idx].Clear(); gameObjectIds.RemoveAt(idx); timeLineItems.RemoveAt(idx); removeTimelineIdxs.Add(idx); return true; } return false; } public void Binding(GameObject tlObject, string trackName, Object obj) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Binding(trackName, obj); } public void Unbinding(GameObject tlObject, string trackName) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.UnBinding(trackName); } public void ClearBinding(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.ClearAllBinding(); } public void Play(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Play(); } public void Pause(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Pause(); } public void Resume(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Resume(); } public void Stop(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Stop(); } public void Reset(GameObject tlObject, double timePoint) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Reset(timePoint); } public void ResetAndPlay(GameObject tlObject, double timePoint) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.ResetAndPlay(timePoint); } public void Reset(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.Reset(); } public void ResetAndPlay(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.ResetAndPlay(); } public void RegisterCompleteCallback(GameObject tlObject, System.Action action) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.onComplete = action; } public void RegisterCallback(GameObject tlObject, double time, System.Action action) { var tlItem = Load(tlObject); if (null != tlItem) tlItem.RegisterEvent(time, action); } public double GetTimelineTime(GameObject tlObject) { var tlItem = Load(tlObject); if (null != tlItem) return tlItem.Duration; return 0; } } }