This commit is contained in:
CortexCore
2025-04-28 15:11:01 +08:00
parent 2d8978e694
commit f8bcf8a4d4
19 changed files with 526 additions and 15 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c4516b33dd9aebc408726bd560ff2c46
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using System.IO;
using UnityEngine;
namespace Net.BITKit
{
public static class AudioClipUtils
{
public static byte[] AudioClipToWavBytes(AudioClip clip)
{
var samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
var wavData = ConvertToWav(samples, clip.channels, clip.frequency);
return wavData;
}
private static byte[] ConvertToWav(float[] samples, int channels, int sampleRate)
{
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
var byteRate = sampleRate * channels * 2;
// 写入 WAV 文件头
writer.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));
writer.Write(0); // placeholder for total file size
writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));
// fmt 块
writer.Write(System.Text.Encoding.ASCII.GetBytes("fmt "));
writer.Write(16); // PCM
writer.Write((short)1); // AudioFormat = PCM
writer.Write((short)channels);
writer.Write(sampleRate);
writer.Write(byteRate);
writer.Write((short)(channels * 2)); // Block align
writer.Write((short)16); // Bits per sample
// data 块
writer.Write(System.Text.Encoding.ASCII.GetBytes("data"));
writer.Write(0); // placeholder for data chunk size
// 写入样本
var sampleCount = samples.Length;
for (var i = 0; i < sampleCount; i++)
{
var intData = (short)(samples[i] * short.MaxValue);
writer.Write(intData);
}
// 回写文件长度
writer.Seek(4, SeekOrigin.Begin);
writer.Write((int)(stream.Length - 8)); // RIFF size
writer.Seek(40, SeekOrigin.Begin);
writer.Write((int)(stream.Length - 44)); // data chunk size
writer.Flush();
return stream.ToArray();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 44ed748c8c7007445b81b479b5607388
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -134,6 +134,8 @@ namespace BITKit.UX
_confirmButton.clicked += Close;
_cancelButton.clicked += Close;
BITInputSystem.AllowInput.AddDisableElements(this);
if (message.OnConfirm is not null)
{
@@ -156,6 +158,7 @@ namespace BITKit.UX
}
private void Close()
{
BITInputSystem.AllowInput.RemoveDisableElements(this);
Dispose();
BITAppForUnity.AllowCursor.RemoveElement(this);
}

View File

@@ -41,6 +41,12 @@ namespace Net.BITKit.UX
x.text = _localizationService.GetLocalizedString('#'+x.viewDataKey);
}
foreach (var x in visualElement.Query<ProgressBar>(className:USS).ToList())
{
if(string.IsNullOrEmpty(x.viewDataKey))continue;
x.title = _localizationService.GetLocalizedString('#'+x.viewDataKey);
}
foreach (var x in visualElement.Query(className:USS).ToList())
{
if(string.IsNullOrEmpty(x.viewDataKey))continue;

View File

@@ -239,7 +239,7 @@ namespace BITKit.UX
await UniTask.NextFrame();
await OnExitAsync.UniTaskFunc();
try
{
var cts = new CancellationTokenSource();
@@ -249,6 +249,10 @@ namespace BITKit.UX
catch (OperationCanceledException)
{
}
finally
{
await UniTask.NextFrame();
}
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8133ef90cc55fcd47b52169ff7a8acab
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.Tween;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
namespace Net.BITKit.UX.SnackBar
{
public class UXSnackBar<TPanel> : UIToolkitSubPanel<TPanel>,ISnackBar where TPanel :IUXPanel
{
[UXBindPath("snack_bar-container")] private VisualElement _snackBarContainer;
private VisualTreeAsset _template;
private readonly ValidHandle _waitHandle = new ValidHandle();
public UXSnackBar(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
protected override void OnInitiated()
{
base.OnInitiated();
_template = _snackBarContainer.Q<TemplateContainer>().templateSource;
_snackBarContainer.Clear();
BITAppForUnity.AllowCursor.AddListener(OnAllowCursor);
}
private void OnAllowCursor(bool obj)
{
if (obj)
{
_waitHandle.AddElement(this);
}
else
{
_waitHandle.RemoveElement(this);
}
}
public async void Add(string message, Severity severity = Severity.Normal)
{
var ve = _snackBarContainer.Create(_template);
ve.AddToClassList($"severity-{severity.ToString().ToLower()}");
ve.Get<Label>().text = message;
if (ve.Q("VisualElement--1") is { } bar)
{
await BITween.Lerp(x => bar.style.width = new StyleLength(Length.Percent(x)), 0f, 100, 5, Mathf.Lerp);
}
else
{
await UniTask.Delay(5000);
}
await _waitHandle;
ve.RemoveFromHierarchy();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f50e7ab406d7dc418e3837c536dff2b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: