using System.Linq.Expressions; using AibisDream.FixSystem; using AibisDream.Framework; using AibisDream.Kit; using Cinemachine; using UnityEngine; using UnityEngine.Playables; using Yarn.Unity; using System.Collections; using UnityEngine.Timeline; using System.Collections.Generic; namespace AibisDream { public class OpenSystem : MonoBehaviour { public DeepInClock deepInClock; public PlayableDirector OpenPlayable; public GameObject OpenAnima; public SpriteDeepIn deepIn; public LightRoom lightRoom; public GameObject screen; private bool isCuttingMode = false; private void Awake() { // 注册 FixSystemCenter.SystemDic.Register(this); var virtualCam = transform.Find("Op Camera").GetComponent(); CameraKit.Instance.RegisterCamera(CameraEnum.Op, virtualCam); } public void Update() { if (Input.GetKeyDown(KeyCode.K)) { EnableClock(); } } private void OnDestroy() { // deepInClock.OnClockReached -= PlayFadeIn; // fadeInPlayable.stopped -= PlayDeepIn; // deepIn.OnFinished -= Finished; } public void OpenLightRoom() { screen.SetActive(false); lightRoom.gameObject.SetActive(true); } public void EnableClock() { lightRoom.gameObject.SetActive(false); deepInClock.Enable(); } // public void PlayFadeIn() // { // fadeInAnima.SetActive(true); // fadeInPlayable.Play(); // } public void PlayOpenTimeline() { // 在播放Timeline之前绑定DialogController BindDialogController(); OpenAnima.SetActive(true); OpenPlayable.Play(); } // private void PlayDeepIn(PlayableDirector director) // { // fadeInAnima.SetActive(false); // deepIn.gameObject.SetActive(true); // deepIn.Play(); // } private void Finished() { DialogController.Instance.StartDialogNode("DeepInEnd"); } public void SwitchToBodyModule() { StartCoroutine(FixSystemCenter.StateMachine.SwitchState(FixState.BodyModule)); Debug.Log("SwitchToBodyModule"); } // 添加动态绑定方法 public void BindTimelineTarget(string trackName, Object target) { if (OpenPlayable == null) return; var timeline = OpenPlayable.playableAsset as TimelineAsset; if (timeline == null) return; foreach (var track in timeline.GetOutputTracks()) { if (track.name == trackName) { OpenPlayable.SetGenericBinding(track, target); break; } } } // 添加DialogController绑定方法 private void BindDialogController() { if (OpenPlayable == null) return; var timeline = OpenPlayable.playableAsset as TimelineAsset; if (timeline == null) return; // 遍历所有轨道 foreach (var track in timeline.GetOutputTracks()) { // 检查轨道类型是否为Signal Track或其他需要DialogController的轨道 if (track is SignalTrack || track.name.Contains("Dialog")) { // 绑定DialogController实例 OpenPlayable.SetGenericBinding(track, DialogController.Instance); } } } // 通用的Timeline播放方法 public IEnumerator PlayTimeline(string timelinePath, Dictionary bindings = null) { // 加载Timeline资源 var timelineAsset = Resources.Load(timelinePath); if (timelineAsset == null) { Debug.LogError($"找不到Timeline资源: {timelinePath}"); yield break; } // 创建临时PlayableDirector var director = gameObject.AddComponent(); director.playableAsset = timelineAsset; // 处理绑定 if (bindings != null) { foreach (var track in timelineAsset.GetOutputTracks()) { // 尝试从bindings中找到匹配的绑定 if (bindings.TryGetValue(track.name, out var binding)) { director.SetGenericBinding(track, binding); } // 特殊处理DialogController else if (track is SignalTrack || track.name.Contains("Dialog")) { director.SetGenericBinding(track, DialogController.Instance); } // 尝试在场景中查找同名物体 else { var targetObj = GameObject.Find(track.name); if (targetObj != null) { director.SetGenericBinding(track, targetObj); } } } } // 播放Timeline director.Play(); // 等待Timeline播放完成 while (director.state == PlayState.Playing) { yield return null; } // 清理 Destroy(director); } } public static class OpenYarnCommand { private static OpenSystem OpenSystem => FixSystemCenter.SystemDic.Get(); [YarnCommand("enable_clock")] public static void EnableClock() { OpenSystem.EnableClock(); } [YarnCommand("open_light_room")] public static void OpenLightRoom() { OpenSystem.OpenLightRoom(); } [YarnCommand("play_open_timeline")] public static IEnumerator RunTimeline() { OpenSystem.PlayOpenTimeline(); yield return null; // if (true) // { // while (OpenSystem.OpenPlayable.state == PlayState.Playing) // { // yield return null; // } // } } [YarnCommand("play_timeline")] public static IEnumerator PlayTimeline(string timelinePath) { yield return OpenSystem.PlayTimeline(timelinePath); } [YarnCommand("play_timeline_with_bindings")] public static IEnumerator PlayTimelineWithBindings(string timelinePath, string[] bindingNames) { var bindings = new Dictionary(); // 解析绑定名称并查找对应物体 foreach (var bindingName in bindingNames) { var targetObj = GameObject.Find(bindingName); if (targetObj != null) { bindings[bindingName] = targetObj; } else { Debug.LogWarning($"找不到绑定物体: {bindingName}"); } } yield return OpenSystem.PlayTimeline(timelinePath, bindings); } } }