173 lines
5.1 KiB
C#
173 lines
5.1 KiB
C#
|
|
// using System.Collections;
|
|||
|
|
// using UnityEngine;
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 音频报警组件,支持不同优先级的报警声音
|
|||
|
|
// /// </summary>
|
|||
|
|
// public class AudioAlarmComponent : MonoBehaviour
|
|||
|
|
// {
|
|||
|
|
// [Header("音频设置")]
|
|||
|
|
// public AudioSource audioSource;
|
|||
|
|
// public float frequency = 500f; // 500Hz
|
|||
|
|
// public float volume = 0.5f;
|
|||
|
|
|
|||
|
|
// [Header("报警模式")]
|
|||
|
|
// private AlarmPriority currentPriority = AlarmPriority.None;
|
|||
|
|
// private bool isPlaying = false;
|
|||
|
|
// private Coroutine alarmCoroutine;
|
|||
|
|
|
|||
|
|
// void Awake()
|
|||
|
|
// {
|
|||
|
|
// if (audioSource == null)
|
|||
|
|
// audioSource = GetComponent<AudioSource>();
|
|||
|
|
|
|||
|
|
// if (audioSource == null)
|
|||
|
|
// {
|
|||
|
|
// audioSource = gameObject.AddComponent<AudioSource>();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 设置基本属性
|
|||
|
|
// audioSource.clip = GenerateTone(frequency, 0.1f);
|
|||
|
|
// audioSource.volume = volume;
|
|||
|
|
// audioSource.loop = false;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 设置报警状态
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetAlarmState(AlarmPriority priority, bool isActive)
|
|||
|
|
// {
|
|||
|
|
// if (alarmCoroutine != null)
|
|||
|
|
// {
|
|||
|
|
// StopCoroutine(alarmCoroutine);
|
|||
|
|
// alarmCoroutine = null;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// currentPriority = priority;
|
|||
|
|
// isPlaying = isActive;
|
|||
|
|
|
|||
|
|
// if (!isActive)
|
|||
|
|
// {
|
|||
|
|
// audioSource.Stop();
|
|||
|
|
// return;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// switch (priority)
|
|||
|
|
// {
|
|||
|
|
// case AlarmPriority.High:
|
|||
|
|
// // 高优先级:10个脉冲群@500hz,每3秒重复一次
|
|||
|
|
// alarmCoroutine = StartCoroutine(HighPriorityAlarm());
|
|||
|
|
// break;
|
|||
|
|
|
|||
|
|
// case AlarmPriority.Medium:
|
|||
|
|
// // 中优先级:3个脉冲群@500hz,每3秒重复一次
|
|||
|
|
// alarmCoroutine = StartCoroutine(MediumPriorityAlarm());
|
|||
|
|
// break;
|
|||
|
|
|
|||
|
|
// case AlarmPriority.Low:
|
|||
|
|
// // 低优先级:2个脉冲群@500hz,每18秒重复一次
|
|||
|
|
// alarmCoroutine = StartCoroutine(LowPriorityAlarm());
|
|||
|
|
// break;
|
|||
|
|
|
|||
|
|
// default:
|
|||
|
|
// audioSource.Stop();
|
|||
|
|
// break;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 静音控制
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetMute(bool muted)
|
|||
|
|
// {
|
|||
|
|
// audioSource.mute = muted;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 高优先级报警模式
|
|||
|
|
// /// </summary>
|
|||
|
|
// private IEnumerator HighPriorityAlarm()
|
|||
|
|
// {
|
|||
|
|
// while (isPlaying)
|
|||
|
|
// {
|
|||
|
|
// // 10个脉冲群
|
|||
|
|
// for (int i = 0; i < 10 && isPlaying; i++)
|
|||
|
|
// {
|
|||
|
|
// audioSource.Play();
|
|||
|
|
// yield return new WaitForSeconds(0.1f);
|
|||
|
|
// yield return new WaitForSeconds(0.05f); // 短暂间隔
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 等待3秒后重复
|
|||
|
|
// yield return new WaitForSeconds(3f);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 中优先级报警模式
|
|||
|
|
// /// </summary>
|
|||
|
|
// private IEnumerator MediumPriorityAlarm()
|
|||
|
|
// {
|
|||
|
|
// while (isPlaying)
|
|||
|
|
// {
|
|||
|
|
// // 3个脉冲群
|
|||
|
|
// for (int i = 0; i < 3 && isPlaying; i++)
|
|||
|
|
// {
|
|||
|
|
// audioSource.Play();
|
|||
|
|
// yield return new WaitForSeconds(0.1f);
|
|||
|
|
// yield return new WaitForSeconds(0.1f); // 间隔
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 等待3秒后重复
|
|||
|
|
// yield return new WaitForSeconds(3f);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 低优先级报警模式
|
|||
|
|
// /// </summary>
|
|||
|
|
// private IEnumerator LowPriorityAlarm()
|
|||
|
|
// {
|
|||
|
|
// while (isPlaying)
|
|||
|
|
// {
|
|||
|
|
// // 2个脉冲群
|
|||
|
|
// for (int i = 0; i < 2 && isPlaying; i++)
|
|||
|
|
// {
|
|||
|
|
// audioSource.Play();
|
|||
|
|
// yield return new WaitForSeconds(0.1f);
|
|||
|
|
// yield return new WaitForSeconds(0.2f); // 较长间隔
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 等待18秒后重复
|
|||
|
|
// yield return new WaitForSeconds(18f);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 生成指定频率的音调
|
|||
|
|
// /// </summary>
|
|||
|
|
// private AudioClip GenerateTone(float frequency, float duration)
|
|||
|
|
// {
|
|||
|
|
// int sampleRate = 44100;
|
|||
|
|
// int sampleCount = Mathf.RoundToInt(sampleRate * duration);
|
|||
|
|
// float[] samples = new float[sampleCount];
|
|||
|
|
|
|||
|
|
// for (int i = 0; i < sampleCount; i++)
|
|||
|
|
// {
|
|||
|
|
// samples[i] = Mathf.Sin(2 * Mathf.PI * frequency * i / sampleRate);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// AudioClip clip = AudioClip.Create("AlarmTone", sampleCount, 1, sampleRate, false);
|
|||
|
|
// clip.SetData(samples, 0);
|
|||
|
|
// return clip;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// void OnDestroy()
|
|||
|
|
// {
|
|||
|
|
// if (alarmCoroutine != null)
|
|||
|
|
// {
|
|||
|
|
// StopCoroutine(alarmCoroutine);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// }
|