using UnityEngine; using System.Threading.Tasks; using System; using asap.core; using System.Linq; public class OfferStoryChainVideoCtrl : OfferStoryChainAnimationCtrl { [SerializeField] private LeviathanVideoDecoder videoDecoder; [SerializeField] private TextAsset[] idleVideos, actionVideos; private string[] _audioNames; private const double MicrosecondsPerSecond = 1_000_000d; public override void Init(string baseName) { // _idleAnimNames = Enumerable.Range(1, 6).Select(i => baseName + "_idle_" + i).ToArray(); // _aniNames = Enumerable.Range(1, 5).Select(i => baseName + "_action_" + i).ToArray(); // _idleAnimNames = Enumerable.Range(1, 6).Select(i => "idle_" + i).ToArray(); // _aniNames = Enumerable.Range(1, 5).Select(i => "action_" + i).ToArray(); _audioNames = Enumerable.Range(1, actionVideos.Length).Select(i => "audio_" + baseName + "_action_" + i).ToArray(); for (int i = 0; i < transform.childCount; i++) { var child = transform.GetChild(i); // Debug.Log("[OfferStoryChain] child: " + child.name); child.gameObject.SetActive(!child.TryGetComponent(out _)); } } public override void PlayIdleAnimation(int progress) { try { progress = Mathf.Clamp(progress, 0, idleVideos.Length - 1); videoDecoder.SetLooping(true); videoDecoder.ChangeVideo(idleVideos[progress], LeviathanVideoDecoderBase.PlayAlphaType.None, multithreadedDecode:true); } catch (Exception e) { Debug.LogError(e); } } public override async Task PlayUpdateAnimation(int progress) { try { // GContext.Publish(new Game.EventUISound(_audioNames[progress - 1])); // skeletonGraphic.AnimationState.SetAnimation(0, _aniNames[progress - 1], false); // var duration = skeletonGraphic.AnimationState.GetCurrent(0).Animation.Duration; // skeletonGraphic.AnimationState.AddAnimation(0, _idleAnimNames[progress], true, 0f); // await Task.Delay(TimeSpan.FromSeconds(duration)); progress -= 1; GContext.Publish(new Game.EventUISound(_audioNames[progress])); progress = Mathf.Clamp(progress, 0, actionVideos.Length - 1); videoDecoder.ChangeVideo(actionVideos[progress], LeviathanVideoDecoderBase.PlayAlphaType.None, multithreadedDecode:true); videoDecoder.SetLooping(false); await Task.Delay(TimeSpan.FromSeconds(GetCurrentVideoLengthSeconds())); videoDecoder.ChangeVideo(idleVideos[progress + 1], LeviathanVideoDecoderBase.PlayAlphaType.None, multithreadedDecode:true); videoDecoder.SetLooping(true); } catch (Exception e) { Debug.LogError(e); } } private double GetCurrentVideoLengthSeconds() { if (videoDecoder == null) { return 0d; } // 首选容器时长(微秒),对可变帧率更准确。 if (videoDecoder.Duration > 0) { return videoDecoder.Duration / MicrosecondsPerSecond; } // 兜底:总帧数 * 每帧时长(秒)。 if (videoDecoder.FrameCount > 0 && videoDecoder.FrameInterval > 0) { return videoDecoder.FrameCount * videoDecoder.FrameInterval; } return 0d; } // #if UNITY_EDITOR // [Header("Test")] // [SerializeField] private int progress; // private void Update() // { // if (Input.GetKeyDown(KeyCode.I)) // { // PlayIdleAnimation(progress); // } // if (Input.GetKeyDown(KeyCode.U)) // { // PlayUpdateAnimation(progress); // } // } // #endif }