Files
aibis-dream/Assets/Scripts/FixSystemNew/Open/OpenSystem.cs
T
2025-06-15 18:47:05 +08:00

304 lines
9.8 KiB
C#

using AibisDream.FixSystem;
using AibisDream.Framework;
using Cinemachine;
using UnityEngine;
using UnityEngine.Playables;
using Yarn.Unity;
using System.Collections;
using UnityEngine.Timeline;
using System.Collections.Generic;
using DG.Tweening;
using AibisDream.Utility;
using System.Linq;
namespace AibisDream
{
public class OpenSystem : MonoBehaviour
{
public DeepInClock deepInClock;
public PlayableDirector openPlayable;
public LightRoom lightRoom;
public GameObject screen;
public SpriteRenderer dreamSprite;
public BubbleSlotGroup bubbleSlotGroup;
private void Awake()
{
// 注册
FixSystemCenter.SystemDic.Register(this);
var virtualCam = transform.Find("Op Camera").GetComponent<ICinemachineCamera>();
CameraKit.Instance.RegisterCamera(CameraEnum.Op, virtualCam);
var breakCam = transform.Find("Break Camera").GetComponent<ICinemachineCamera>();
CameraKit.Instance.RegisterCamera(CameraEnum.BreakCamera, breakCam);
var dreamCam = transform.Find("Dream Camera").GetComponent<ICinemachineCamera>();
CameraKit.Instance.RegisterCamera(CameraEnum.DreamCamera, dreamCam);
}
private void Start()
{
dreamSprite.gameObject.SetActive(true);
dreamSprite.SetAlpha(0);
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
EnableClock();
}
}
public void OpenLightRoom()
{
screen.SetActive(false);
lightRoom.gameObject.SetActive(true);
}
public void EnableClock()
{
lightRoom.gameObject.SetActive(false);
deepInClock.Enable();
}
public void PlayOpenTimeline()
{
// 在播放Timeline之前绑定DialogController
BindDialogController();
//OpenAnima.SetActive(true);
openPlayable.Play();
}
public void DreamFadeIn(float time, float targetAlpha)
{
dreamSprite.DOFade(targetAlpha, time).SetEase(Ease.InOutSine);
}
public void DreamDisappear()
{
dreamSprite.gameObject.SetActive(false);
}
// 添加动态绑定方法
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);
}
}
}
private void BindPlayToolPanel()
{
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.name.Contains("Fade"))
{
// 绑定DialogController实例
openPlayable.SetGenericBinding(track, UIManager.Instance.GetPanel<PlayToolPanel>());
}
}
}
// 通用的Timeline播放方法
public IEnumerator PlayTimeline(string timelinePath, Dictionary<string, Object> bindings = null)
{
// 加载Timeline资源
var timelineAsset = Resources.Load<TimelineAsset>(timelinePath);
if (timelineAsset == null)
{
Debug.LogError($"找不到Timeline资源: {timelinePath}");
yield break;
}
openPlayable.playableAsset = timelineAsset;
// BindDialogController();
openPlayable.Play();
while (openPlayable.state == PlayState.Playing)
{
yield return null;
}
}
public void PlayTimeline_noWait(string timelinePath)
{
Debug.Log($"[Timeline Debug] 开始加载 Timeline: {timelinePath}");
// 加载Timeline资源
var timelineAsset = Resources.Load<TimelineAsset>(timelinePath);
if (timelineAsset == null)
{
Debug.LogError($"[Timeline Debug] 找不到Timeline资源: {timelinePath}");
return;
}
Debug.Log($"[Timeline Debug] Timeline资源加载成功: {timelinePath}");
openPlayable.playableAsset = timelineAsset;
// 检查所有轨道的绑定状态
var timeline = openPlayable.playableAsset as TimelineAsset;
if (timeline != null)
{
var tracks = timeline.GetOutputTracks();
Debug.Log($"[Timeline Debug] Timeline包含 {tracks.Count()} 个轨道:");
foreach (var track in tracks)
{
var binding = openPlayable.GetGenericBinding(track);
Debug.Log($"[Timeline Debug] 轨道: {track.name}, 绑定状态: {(binding != null ? "已绑定" : "未绑定")}, 绑定对象: {binding?.name ?? ""}");
}
}
Debug.Log("[Timeline Debug] 开始播放 Timeline");
openPlayable.Play();
// 添加播放状态变化的监听
openPlayable.stopped += (director) => Debug.Log("[Timeline Debug] Timeline 播放结束");
openPlayable.paused += (director) => Debug.Log("[Timeline Debug] Timeline 暂停");
}
public void FinishCut()
{
DialogController.Instance.StartDialogNode("Cut_End");
}
public void PauseTimeline()
{
Debug.Log("TRY PAUSE Timeline"); // 添加这行
if (openPlayable != null && openPlayable.state == PlayState.Playing)
{
Debug.Log("Timeline is being paused"); // 添加这行
openPlayable.Pause();
}
}
public IEnumerator ResumeTimeline()
{
if (openPlayable != null && openPlayable.state == PlayState.Paused)
{
openPlayable.Resume();
// 等待 Timeline 播放完成
while (openPlayable.state == PlayState.Playing)
{
yield return null;
}
}
}
}
public static class OpenYarnCommand
{
private static OpenSystem OpenSystem => FixSystemCenter.SystemDic.Get<OpenSystem>();
[YarnCommand("enable_clock")]
public static void EnableClock()
{
OpenSystem.EnableClock();
}
[YarnCommand("open_light_room")]
public static void OpenLightRoom()
{
OpenSystem.OpenLightRoom();
}
// [YarnCommand("play__timeline")]
// public static IEnumerator RunTimeline()
// {
// OpenSystem.PlayOpenTimeline();
// yield return null;
// }
[YarnCommand("Open_play_timeline")]
public static IEnumerator PlayTimeline(string timelinePath)
{
yield return OpenSystem.PlayTimeline(timelinePath);
}
[YarnCommand("Open_play_timeline_no_wait")]
public static void PlayTimeline_noWait(string timelinePath)
{
OpenSystem.PlayTimeline_noWait(timelinePath);
}
[YarnCommand("play_timeline_with_bindings")]
public static IEnumerator PlayTimelineWithBindings(string timelinePath, string[] bindingNames)
{
var bindings = new Dictionary<string, Object>();
// 解析绑定名称并查找对应物体
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);
}
[YarnCommand("dream_fade_in")]
public static void DreamFadeIn(float time, float targetAlpha)
{
OpenSystem.DreamFadeIn(time, targetAlpha);
}
[YarnCommand("dream_disappear")]
public static void DreamDisappear()
{
OpenSystem.DreamDisappear();
}
[YarnCommand("resume_timeline")]
public static IEnumerator ResumeTimeline()
{
yield return OpenSystem.ResumeTimeline();
}
[YarnCommand("pause_timeline")]
public static void PauseTimeline()
{
OpenSystem.PauseTimeline();
}
}
}