更改文件架构
This commit is contained in:
100
Packages/Common~/Scripts/Player/DataPlayer.cs
Normal file
100
Packages/Common~/Scripts/Player/DataPlayer.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Player/DataPlayer.cs.meta
Normal file
11
Packages/Common~/Scripts/Player/DataPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85a892f33eb93d94da1bc7fd91870a75
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
85
Packages/Common~/Scripts/Player/DataRecorder.cs
Normal file
85
Packages/Common~/Scripts/Player/DataRecorder.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit.IData;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Sirenix.OdinInspector;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Events;
|
||||
using BITKit.IO;
|
||||
namespace BITKit
|
||||
{
|
||||
public class DataRecorder : Provider<string>, IProvider<bool>
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public NameProvider nameProvider;
|
||||
public DataRecorder<string> recorder = new();
|
||||
public Provider output;
|
||||
public UnityEvent onStart = new();
|
||||
public UnityEvent onStop = new();
|
||||
DateTime createTime;
|
||||
void Awake()
|
||||
{
|
||||
recorder.output += Output;
|
||||
recorder.onStart += onStart.Invoke;
|
||||
recorder.onStop += onStop.Invoke;
|
||||
onStop.Invoke();
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
Cancel();
|
||||
}
|
||||
public override string Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(string t)
|
||||
{
|
||||
recorder.Set(t);
|
||||
}
|
||||
public void Record()
|
||||
{
|
||||
createTime = DateTime.Now;
|
||||
recorder.Start();
|
||||
}
|
||||
public void Stop()
|
||||
{
|
||||
recorder.Stop();
|
||||
}
|
||||
public void Cancel() => recorder.Cancel();
|
||||
void Output(List<string> jObject)
|
||||
{
|
||||
BITAssets assets = new();
|
||||
PlayableInfo playableInfo = new()
|
||||
{
|
||||
Name = nameProvider.GetName(jObject),
|
||||
CreateTime = createTime,
|
||||
CompleteTime = DateTime.Now,
|
||||
FileName = "base64",
|
||||
};
|
||||
var json = JsonConvert.SerializeObject(playableInfo, Formatting.Indented);
|
||||
var base64Data = JsonConvert.SerializeObject(jObject, Formatting.Indented);
|
||||
//base64Data = GZipHelper.GZipCompressString(base64Data);
|
||||
assets.Add(new BITAsset(nameof(PlayableInfo), json));
|
||||
assets.Add(new BITAsset(playableInfo.FileName, base64Data));
|
||||
|
||||
output?.Set(assets);
|
||||
}
|
||||
bool IProvider<bool>.Get()
|
||||
{
|
||||
return recorder.isPlaying;
|
||||
}
|
||||
public void Set(bool t)
|
||||
{
|
||||
if (t)
|
||||
{
|
||||
Record();
|
||||
}
|
||||
else
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Player/DataRecorder.cs.meta
Normal file
11
Packages/Common~/Scripts/Player/DataRecorder.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac4a685bc2281134ab8c85da0f684465
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user