Runnable.Run / StartCoroutine calles to Watson services from



在我的 ExampleStreaming.cs脚本中,一旦用户话语被识别为 final,我将其发送到 Watson Assistant服务和 Tone Analyzer。因为我将每个服务的脚本都保持原样,所以我必须在每个脚本中拨打电话才能访问其他服务。您可以看到我对下面Tone Analyzer的呼叫(.SendToneAnalysis方法):

private void OnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData)
{
    blah blah blah . . .
    /// Only send the recognized speech utterance to the
    /// Assistant once we know the user has stopped talking.
    if (res.final)
    {
        string _conversationString = alt.transcript;
        Runnable.Run( StopRecording(1f) ); // Stop the microphone from listening.
    /// Message.
    Dictionary<string, object> input = new Dictionary<string, object>
    {
        ["text"] = _conversationString
    };
    MessageRequest messageRequest = new MessageRequest()
    {
        Input = input,
        Context = _Context
    };
    _exampleAssistantV1_script.SendMessageAssistant(messageRequest);
    _exampleToneAnalyzer.SendToneAnalysis(_conversationString);
    . . .

在我的ExampleToneAnalyzer.cs脚本中,我对旨在联系服务并处理成功的事件处理方法进行了简单的呼叫。失败:

public void SendToneAnalysis(string conversationString)
{
    _service.GetToneAnalyze(OnGetToneAnalyze, OnFail, conversationString);
}

这些调用通常是使用StartCoroutines进行的,尤其是在Watson Unity SDK中,有一个专业的Runnable.Run,它本质上是运行共同运行的助手类,而无需从MonoBehavior继承。

我的问题是我对服务的简单方法是否在某些情况下可能是有问题的,或者可能只是错误或不良编程,或者是否完全可以使用该方法而不是类似的方法:

public void SendToneAnalysis(string conversationString)
{
    Runnable.Run( SendAssistantToneAnalysis(conversationString) );
}
private IEnumerator SendAssistantToneAnalysis(string conversationString)
{
    if ( !_service.GetToneAnalyze(OnGetToneAnalyze, OnFail, conversationString) )
    {
        Log.Debug("ExampleToneAnalyzer.SendAssistantToneAnalysis()", "Failed to analyze!");
    }
    while (!_UserUtteranceToneTested)
        yield return null;
}

您不需要从Coroutine内部进行任何服务调用。仅使用iamApikey的身份验证才能使用Coroutine

进行
IEnumerator TokenExample()
{
    //  Create IAM token options and supply the apikey. IamUrl is the URL used to get the 
    //  authorization token using the IamApiKey. It defaults to https://iam.bluemix.net/identity/token
    TokenOptions iamTokenOptions = new TokenOptions()
    {
        IamApiKey = "<iam-api-key>",
        IamUrl = "<iam-url>"
    };
    //  Create credentials using the IAM token options
    _credentials = new Credentials(iamTokenOptions, "<service-url>");
    while (!_credentials.HasIamTokenData())
        yield return null;
    _assistant = new Assistant(_credentials);
    _assistant.VersionDate = "2018-02-16";
    _assistant.ListWorkspaces(OnListWorkspaces, OnFail);
}

这些示例仅是为了显示如何调用服务调用。从coroutine调用代码的唯一原因是,我们可以在运行另一个服务调用之前等待一个服务调用的响应(即,因此在创建工作空间之前,我们不会尝试更新或删除工作区)。

这没问题。runnable.run()最终将startCoroutine()称为下面。

    public Routine(IEnumerator a_enumerator)
    {
        _enumerator = a_enumerator;
        Runnable.Instance.StartCoroutine(this);
        Stop = false;
        ID = Runnable.Instance._nextRoutineId++;
        Runnable.Instance._routines[ID] = this;
    #if ENABLE_RUNNABLE_DEBUGGING
        Log.Debug("Runnable.Routine()", "Coroutine {0} started.", ID ); 
    #endif
    }

,如果它处于活动状态,则可以从任何gameObject调用coroutine。

最新更新