killapp/Assets/Scripts/UI/Pages/DeviceInfoPage/VideoAndSoundSetting.cs
“虞渠成” a01ff23268 feat: 新增音效资源并优化音效选择功能
1. 新增Arc、Ion、Sonic、Plasma、Quantum、Hardlight共6种音效资源及导入配置
2. 为多选弹窗组件添加点击回调事件,支持选项点击预览音效
3. 为音画设置页面添加音效预览功能,绑定新增的音效资源
4. 修复iOS构建脚本的编译宏条件问题
5. 更新.gitignore忽略灭蚊_mapping.txt文件
2026-07-07 10:32:26 +08:00

336 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using Kill.Bluetooth;
using Kill.Bluetooth.Protocol;
using Kill.Core;
using Kill.Managers;
using Kill.UI.Components;
using UnityEngine.UI;
using UnityEngine;
using System;
using Unity.VisualScripting;
namespace Kill.UI.Pages
{
public class VideoAndSoundSetting : MonoBehaviour
{
public Switch videoSwitch;
public Switch soundSwitch;
public MutiSelectPage videoLengthSelect;
public MutiSelectPage soundEffectSelect;
public Slider volumeSlider;
public Text videoLengthValueText;
public Text soundEffectValueText;
public Text volumeValueText;
// 视频时长选项3秒、5秒、10秒
private List<string> videoLengthKeyList = new List<string> { "100180", "100181", "100182" };
private List<string> videoLengthStrList = new List<string>();
private List<byte> videoLengthValueList = new List<byte> { 0x03, 0x05, 0x0A };
private List<SoundEffectType> soundEffectValueList = new List<SoundEffectType>
{
SoundEffectType.Arc,
SoundEffectType.Hardlight,
SoundEffectType.Ion,
SoundEffectType.Plasma,
SoundEffectType.Quantum,
SoundEffectType.Sonic
};
public AudioSource audioSource;
public AudioClip[] audioClips;
private MultimediaControl multimediaSetting;
private Action callback;
public void Init(Action callback)
{
audioSource=GetComponent<AudioSource>();
this.callback = callback;
InitLanguageText();
ReadMultimediaSettings();
UIManager.Instance.RegisterBackAction(Back);
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
}
/// <summary>
/// 初始化语言文本
/// </summary>
private void InitLanguageText()
{
videoLengthStrList.Clear();
foreach (var key in videoLengthKeyList)
{
videoLengthStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
}
/// <summary>
/// 读取多媒体设置
/// </summary>
private void ReadMultimediaSettings()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
MultimediaConfigFromServer();
return;
}
LoadingUI.Show();
BLECommunicationManager.Instance.ReadMultimediaControl((setting) =>
{
Loom.QueueOnMainThread(() =>
{
multimediaSetting = setting;
UpdateUI();
});
});
}
/// <summary>
/// 写入多媒体设置
/// </summary>
private async void WriteMultimediaSetting()
{
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
LoadingUI.Show();
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_MULTIMEDIA_CONTROL, multimediaSetting.ToBytes());
if (success)
{
SyncMultimediaConfig();
UpdateUI();
}
else
{
ReadMultimediaSettings();
}
return;
}
LoadingUI.Show();
BLECommunicationManager.Instance.WriteMultimediaControl(multimediaSetting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
SyncMultimediaConfig();
DataManager.Instance.SyncDeviceConfigToServer();
UpdateUI();
}
else
{
ReadMultimediaSettings();
}
});
});
}
private void MultimediaConfigFromServer()
{
var config = DataManager.Instance.deviceConfig;
multimediaSetting = new MultimediaControl
{
VideoRecordEnable = config?.video_record_enable ?? false,
RecordDuration = config?.record_duration ?? 0x05,
SoundEnable = config?.sound_enable ?? true,
SoundType = DeviceConfig.ParseServerEnum(config?.sound_type, SoundEffectType.Arc),
Volume = config?.volume ?? 10
};
UpdateUI();
}
private void SyncMultimediaConfig()
{
var config = DataManager.Instance.deviceConfig;
config.video_record_enable = multimediaSetting.VideoRecordEnable;
config.record_duration = multimediaSetting.RecordDuration;
config.sound_enable = multimediaSetting.SoundEnable;
config.sound_type = DeviceConfig.ToServerString(multimediaSetting.SoundType);
config.volume = multimediaSetting.Volume;
}
/// <summary>
/// 更新UI显示
/// </summary>
private void UpdateUI()
{
// 更新视频录制开关
videoSwitch.Init(multimediaSetting.VideoRecordEnable, OnVideoSwitchChanged);
// 更新音效开关
soundSwitch.Init(multimediaSetting.SoundEnable, OnSoundSwitchChanged);
// 更新视频时长显示
int videoLengthIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
if (videoLengthValueText != null && videoLengthIndex >= 0 && videoLengthIndex < videoLengthStrList.Count)
{
videoLengthValueText.text = videoLengthStrList[videoLengthIndex];
}
// 更新音效类型显示
soundEffectValueText.text = multimediaSetting.SoundType.ToString();
// 更新音量滑块
volumeSlider.value = multimediaSetting.Volume;
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
if (volumeValueText != null)
{
volumeValueText.text = $"{multimediaSetting.Volume}";
}
LoadingUI.Hide();
UIManager.Instance.RegisterBackAction(Back);
}
/// <summary>
/// 视频录制开关变化
/// </summary>
private void OnVideoSwitchChanged(bool isOn)
{
multimediaSetting.VideoRecordEnable = isOn;
WriteMultimediaSetting();
}
/// <summary>
/// 音效开关变化
/// </summary>
private void OnSoundSwitchChanged(bool isOn)
{
multimediaSetting.SoundEnable = isOn;
WriteMultimediaSetting();
}
Coroutine volumeChangeCoroutine;
/// <summary>
/// 音量滑块变化
/// </summary>
private void OnVolumeChanged(float value)
{
byte volume = (byte)Mathf.Clamp(value, 0, 15);
multimediaSetting.Volume = volume;
if (volumeValueText != null)
{
volumeValueText.text = $"{volume}";
}
if (volumeChangeCoroutine != null)
{
StopCoroutine(volumeChangeCoroutine);
}
volumeChangeCoroutine = StartCoroutine(CountDownAndWriteMultimediaSetting(0.5f));
}
private IEnumerator CountDownAndWriteMultimediaSetting(float delay)
{
yield return new WaitForSeconds(delay);
WriteMultimediaSetting();
}
/// <summary>
/// 音量滑块拖动结束
/// </summary>
public void OnVolumeSliderEndDrag()
{
WriteMultimediaSetting();
}
/// <summary>
/// 点击视频时长设置
/// </summary>
public void ClickVideoLength()
{
MutiSelectPage m = Instantiate(videoLengthSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
m.Init(currentIndex, OnVideoLengthSelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 视频时长选择回调
/// </summary>
private void OnVideoLengthSelected(int index)
{
if (index >= 0 && index < videoLengthValueList.Count)
{
multimediaSetting.RecordDuration = videoLengthValueList[index];
WriteMultimediaSetting();
}
}
/// <summary>
/// 点击音效类型设置
/// </summary>
public void ClickSoundEffect()
{
MutiSelectPage m = Instantiate(soundEffectSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetSoundEffectIndexFromValue(multimediaSetting.SoundType);
m.Init(currentIndex, OnSoundEffectSelected,OnSoundEffectClick);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 音效类型选择回调
/// </summary>
private void OnSoundEffectSelected(int index)
{
if (index >= 0 && index < soundEffectValueList.Count)
{
multimediaSetting.SoundType = soundEffectValueList[index];
WriteMultimediaSetting();
}
}
private void OnSoundEffectClick(int index)
{
if(audioSource==null)
audioSource=GetComponent<AudioSource>();
if (index >= 0 && index < audioClips.Length)
{
audioSource.clip = audioClips[index];
audioSource.Play();
}
}
/// <summary>
/// 根据视频时长值获取选项索引
/// </summary>
private int GetVideoLengthIndexFromValue(byte value)
{
for (int i = 0; i < videoLengthValueList.Count; i++)
{
if (videoLengthValueList[i] == value)
{
return i;
}
}
// 默认值返回5秒(索引1)
return 1;
}
/// <summary>
/// 根据音效类型值获取选项索引
/// </summary>
private int GetSoundEffectIndexFromValue(SoundEffectType value)
{
for (int i = 0; i < soundEffectValueList.Count; i++)
{
if (soundEffectValueList[i] == value)
{
return i;
}
}
// 默认值返回Hello(索引0)
return 0;
}
public void Back()
{
callback?.Invoke();
Destroy(gameObject);
}
}
}