1
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine.Events;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System;
|
||||
namespace BITKit
|
||||
{
|
||||
public class DataPlayer : Provider
|
||||
{
|
||||
public DataPlayer<string> player = new();
|
||||
public Provider output;
|
||||
public Provider outputProgess;
|
||||
public Provider outputTime;
|
||||
public Provider outputTotalTime;
|
||||
public UnityEvent onStart = new();
|
||||
public UnityEvent<float> onChangeTime = new();
|
||||
public UnityEvent onStop = new();
|
||||
CancellationToken cancellationToken;
|
||||
public override void Set<T>(T obj)
|
||||
{
|
||||
switch (obj)
|
||||
{
|
||||
case List<string> list:
|
||||
player.Set(list);
|
||||
break;
|
||||
case string str:
|
||||
player.Set(JsonConvert.DeserializeObject<List<string>>(str));
|
||||
break;
|
||||
case bool _boolean:
|
||||
if (_boolean)
|
||||
player.Start();
|
||||
else
|
||||
player.Stop();
|
||||
break;
|
||||
case float _float:
|
||||
player.SetProgress(_float);
|
||||
onChangeTime.Invoke(_float);
|
||||
break;
|
||||
default:
|
||||
throw new System.Exception();
|
||||
}
|
||||
}
|
||||
void Awake()
|
||||
{
|
||||
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
if (output)
|
||||
player.output += output.Set;
|
||||
if (outputProgess)
|
||||
player.onProgess += outputProgess.Set;
|
||||
if (outputTime)
|
||||
player.onTime += outputTime.Set;
|
||||
if (outputTotalTime)
|
||||
player.onSetTotalTime += outputTotalTime.Set;
|
||||
player.onStart += async () =>
|
||||
{
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
BIT4Log.Log<DataPlayer>("开始播放");
|
||||
onStart.Invoke();
|
||||
};
|
||||
player.onStop += async () =>
|
||||
{
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
BIT4Log.Log<DataPlayer>("停止播放");
|
||||
onStop.Invoke();
|
||||
};
|
||||
}
|
||||
public void Play()
|
||||
{
|
||||
player.Start();
|
||||
}
|
||||
public void Stop()
|
||||
{
|
||||
player.Stop();
|
||||
}
|
||||
public void PlayOrPause()
|
||||
{
|
||||
player.PlayOrPause();
|
||||
}
|
||||
public void Offset(float time)
|
||||
{
|
||||
player.Offset(time);
|
||||
|
||||
}
|
||||
public bool IsPlaying() => player.isPlaying;
|
||||
public PlayerState GetState() => player.state;
|
||||
public bool IsPaused() => GetState() is PlayerState.Paused;
|
||||
void Start()
|
||||
{
|
||||
onStop.Invoke();
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
player.Stop();
|
||||
}
|
||||
}
|
||||
}
|
106
Src/Unity/Scripts/DataPlayer/UnityDataPlayer.cs
Normal file
106
Src/Unity/Scripts/DataPlayer/UnityDataPlayer.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user