138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Events;
|
||
|
using UnityEngine.EventSystems;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class RecordVoiceUI : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] GameObject effects;
|
||
|
/// <summary>
|
||
|
/// 语音输入的按钮
|
||
|
/// </summary>
|
||
|
[SerializeField] private Button m_VoiceInputBotton;
|
||
|
/// <summary>
|
||
|
/// 录音按钮的文本
|
||
|
/// </summary>
|
||
|
[SerializeField] private Text m_VoiceBottonText;
|
||
|
/// <summary>
|
||
|
/// 录音的提示信息
|
||
|
/// </summary>
|
||
|
[SerializeField] private Text m_RecordTips;
|
||
|
|
||
|
public Button stopBtn;
|
||
|
|
||
|
public AudioRecorder voiceInputs;
|
||
|
|
||
|
public CarAssistant carAssistant;
|
||
|
|
||
|
public event UnityAction onStartListen;
|
||
|
public event UnityAction onStopListen;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
effects.gameObject.SetActive(false);
|
||
|
RegistButtonEvent();
|
||
|
carAssistant.onReceiveText += SetRecordTips;
|
||
|
|
||
|
stopBtn.onClick.AddListener(StopPlay);
|
||
|
stopBtn.gameObject.SetActive(false);
|
||
|
carAssistant.statusManager.onCurrentStatusChanged += (s) =>
|
||
|
{
|
||
|
if (s == carAssistant.statusManager.talking)
|
||
|
{
|
||
|
stopBtn.gameObject.SetActive(true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stopBtn.gameObject.SetActive(false);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
if(carAssistant.statusManager.currentState == carAssistant.statusManager.listening)
|
||
|
{
|
||
|
if (!effects.active)
|
||
|
{
|
||
|
effects.SetActive(true);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (effects.active)
|
||
|
{
|
||
|
effects.SetActive(false);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetRecordTips(string _msg)
|
||
|
{
|
||
|
m_RecordTips.text = _msg;
|
||
|
StartCoroutine(SetTextVisible(m_RecordTips));
|
||
|
}
|
||
|
private IEnumerator SetTextVisible(Text _textbox)
|
||
|
{
|
||
|
yield return new WaitForSeconds(3f);
|
||
|
_textbox.text = "";
|
||
|
}
|
||
|
|
||
|
public void StopPlay()
|
||
|
{
|
||
|
if(carAssistant.statusManager.currentState == carAssistant.statusManager.talking)
|
||
|
carAssistant.statusManager.MakeTransition(carAssistant.statusManager.idle);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 注册按钮事件
|
||
|
/// </summary>
|
||
|
private void RegistButtonEvent()
|
||
|
{
|
||
|
if (m_VoiceInputBotton == null || m_VoiceInputBotton.GetComponent<EventTrigger>())
|
||
|
return;
|
||
|
|
||
|
EventTrigger _trigger = m_VoiceInputBotton.gameObject.AddComponent<EventTrigger>();
|
||
|
|
||
|
//添加按钮按下的事件
|
||
|
EventTrigger.Entry _pointDown_entry = new EventTrigger.Entry();
|
||
|
_pointDown_entry.eventID = EventTriggerType.PointerDown;
|
||
|
_pointDown_entry.callback = new EventTrigger.TriggerEvent();
|
||
|
|
||
|
//添加按钮松开事件
|
||
|
EventTrigger.Entry _pointUp_entry = new EventTrigger.Entry();
|
||
|
_pointUp_entry.eventID = EventTriggerType.PointerUp;
|
||
|
_pointUp_entry.callback = new EventTrigger.TriggerEvent();
|
||
|
|
||
|
//添加委托事件
|
||
|
_pointDown_entry.callback.AddListener(delegate { StartRecord(); });
|
||
|
_pointUp_entry.callback.AddListener(delegate { StopRecord(); });
|
||
|
|
||
|
_trigger.triggers.Add(_pointDown_entry);
|
||
|
_trigger.triggers.Add(_pointUp_entry);
|
||
|
}
|
||
|
|
||
|
void StartRecord()
|
||
|
{
|
||
|
m_VoiceBottonText.text = "listening...";
|
||
|
effects.gameObject.SetActive(true);
|
||
|
if (onStartListen != null) onStartListen();
|
||
|
}
|
||
|
|
||
|
void StopRecord()
|
||
|
{
|
||
|
m_VoiceBottonText.text = "hold to talk";
|
||
|
m_RecordTips.text = "identifying...";
|
||
|
effects.gameObject.SetActive(false);
|
||
|
if (onStopListen != null) onStopListen();
|
||
|
}
|
||
|
}
|