Files

98 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-04-11 10:03:08 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
2025-05-19 15:42:24 +08:00
namespace Stary.Evo.RKTools
2025-04-11 10:03:08 +08:00
{
2026-03-25 16:58:26 +08:00
#if Evo_Rokid
2025-05-19 15:42:24 +08:00
public class VoiceCommandController : MonoBehaviour
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
public Dictionary<string, Action> VoiceCommands = new Dictionary<string, Action>();
public bool isInit = false;
2025-04-11 10:03:08 +08:00
2025-05-19 15:42:24 +08:00
void Awake()
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
2025-04-11 10:03:08 +08:00
}
2025-05-19 15:42:24 +08:00
void Start()
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
//if (!Permission.HasUserAuthorizedPermission("android.permission.RECORD_AUDIO")) return;
2025-04-11 10:03:08 +08:00
}
2025-05-19 15:42:24 +08:00
/// <summary>
/// 注册语音指令
/// </summary>
/// <param name="content"></param>
/// <param name="spell"></param>
/// <param name="action"></param>
public void RegisteredVoiceCommand(string content, string spell, Action action)
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
if (!isInit)
{
if (!Permission.HasUserAuthorizedPermission("android.permission.RECORD_AUDIO"))
{
Permission.RequestUserPermission("android.permission.RECORD_AUDIO");
}
2026-03-25 16:58:26 +08:00
Rokid.UXR.Module.ModuleManager.Instance.RegistModule("com.rokid.voicecommand.VoiceCommandHelper", false);
Rokid.UXR.Module.OfflineVoiceModule.Instance.ChangeVoiceCommandLanguage(Rokid.UXR.Module.LANGUAGE.CHINESE);
2025-05-19 15:42:24 +08:00
isInit = true;
}
if (!VoiceCommands.ContainsKey(content))
{
VoiceCommands.Add(content, action);
}
else
{
Debug.LogError($"语音命令 :“'{content}' ”已经注册了!!!");
return;
}
2026-03-25 16:58:26 +08:00
Rokid.UXR.Module.OfflineVoiceModule.Instance.AddInstruct(Rokid.UXR.Module.LANGUAGE.CHINESE, content, spell, this.gameObject.name,
2025-05-19 15:42:24 +08:00
"OnReceive");
2026-03-25 16:58:26 +08:00
Rokid.UXR.Module.OfflineVoiceModule.Instance.Commit();
2025-04-11 10:03:08 +08:00
}
2025-05-19 15:42:24 +08:00
/// <summary>
/// 删除语音指令
/// </summary>
/// <param name="content"></param>
public void DeleteVoiceCommand(string content)
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
if (VoiceCommands.ContainsKey(content))
{
VoiceCommands.Remove(content);
}
else
{
Debug.LogWarning($"语音命令 :“'{content}' 不存在!!!");
}
2025-04-11 10:03:08 +08:00
}
2025-05-19 15:42:24 +08:00
/// <summary>
/// 清除所有语音指令
/// </summary>
public void ClearAllVoiceCommand()
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
VoiceCommands.Clear();
2026-03-25 16:58:26 +08:00
Rokid.UXR.Module.OfflineVoiceModule.Instance.ClearAllInstruct();
Rokid.UXR.Module.OfflineVoiceModule.Instance.Commit();
2025-04-11 10:03:08 +08:00
}
2025-05-19 15:42:24 +08:00
void OnReceive(string msg)
2025-04-11 10:03:08 +08:00
{
2025-05-19 15:42:24 +08:00
if (VoiceCommands.TryGetValue(msg, out Action action))
{
action?.Invoke();
}
2025-04-11 10:03:08 +08:00
}
}
2026-03-25 16:58:26 +08:00
#endif
2025-05-19 15:42:24 +08:00
}