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