100 lines
3.0 KiB
C#
100 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Sirenix.OdinInspector;
|
|
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();
|
|
}
|
|
}
|
|
} |