106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
using Newtonsoft.Json;
|
|
using UnityEngine.Events;
|
|
using Cysharp.Threading.Tasks;
|
|
using System.Threading;
|
|
using System;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class UnityDataPlayer : MonoBehaviour
|
|
{
|
|
[SerializeField] private int rate;
|
|
[SerializeField] internal DataPlayer<string> player = new();
|
|
[SerializeField] private UnityEvent<string> output;
|
|
[SerializeField] private UnityEvent<float> outputProgess;
|
|
[SerializeField] private UnityEvent<string> outputTime;
|
|
[SerializeField] private UnityEvent<string> outputTotalTime;
|
|
[SerializeField] private UnityEvent onStart = new();
|
|
[SerializeField] private UnityEvent<float> onChangeTime = new();
|
|
[SerializeField] private UnityEvent onStop = new();
|
|
[BIT]
|
|
public void Play()
|
|
{
|
|
player.frameRate = rate;
|
|
player.Start();
|
|
}
|
|
[BIT]
|
|
public void Stop()
|
|
{
|
|
player.Stop();
|
|
}
|
|
[BIT]
|
|
public void PlayOrPause()
|
|
{
|
|
player.PlayOrPause();
|
|
}
|
|
public void Offset(float time)
|
|
{
|
|
player.Offset(time);
|
|
}
|
|
public void SetData(List<string> data)
|
|
{
|
|
player.Set(data);
|
|
}
|
|
public bool IsPlaying() => player.isPlaying;
|
|
public PlayerState GetState() => player.state;
|
|
public bool IsPaused() => GetState() is PlayerState.Paused;
|
|
private void Start()
|
|
{
|
|
onStop.Invoke();
|
|
|
|
player.PlayOrPause();
|
|
player.output += output.Invoke;
|
|
player.onProgess += outputProgess.Invoke;
|
|
player.onTime += outputTime.Invoke;
|
|
player.onStart += async () =>
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
|
BIT4Log.Log<UnityDataPlayer>("开始播放");
|
|
onStart.Invoke();
|
|
};
|
|
player.onStop += async () =>
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
|
BIT4Log.Log<UnityDataPlayer>("停止播放");
|
|
onStop.Invoke();
|
|
};
|
|
player.onSetTotalTime += outputTotalTime.Invoke;
|
|
|
|
destroyCancellationToken.Register(player.Stop);
|
|
}
|
|
public void SetTime(float time)
|
|
{
|
|
player.SetProgress(time);
|
|
onChangeTime.Invoke(time);
|
|
}
|
|
}
|
|
#if UNITY_EDITOR
|
|
[CustomType(typeof(UnityDataPlayer))]
|
|
public sealed class UnityDataPlayerInpsector:BITInspector<UnityDataPlayer>
|
|
{
|
|
private ProgressBar _progressBar;
|
|
public override VisualElement CreateInspectorGUI()
|
|
{
|
|
FillDefaultInspector();
|
|
|
|
_progressBar = root.Create<ProgressBar>();
|
|
|
|
return root;
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
_progressBar.value = agent.player.index;
|
|
_progressBar.highValue = agent.player.list.Count;
|
|
base.OnUpdate();
|
|
}
|
|
}
|
|
#endif
|
|
} |