{"id":19778890,"url":"https://github.com/dialogflow/dialogflow-unity-client","last_synced_at":"2025-04-30T21:31:17.168Z","repository":{"id":27520369,"uuid":"31001255","full_name":"dialogflow/dialogflow-unity-client","owner":"dialogflow","description":"Unity library for Dialogflow","archived":false,"fork":false,"pushed_at":"2019-02-20T18:56:31.000Z","size":3785,"stargazers_count":54,"open_issues_count":13,"forks_count":15,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-06T04:41:44.884Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dialogflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-19T04:31:03.000Z","updated_at":"2024-04-14T06:30:01.000Z","dependencies_parsed_at":"2022-09-02T09:12:30.791Z","dependency_job_id":null,"html_url":"https://github.com/dialogflow/dialogflow-unity-client","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialogflow%2Fdialogflow-unity-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialogflow%2Fdialogflow-unity-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialogflow%2Fdialogflow-unity-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialogflow%2Fdialogflow-unity-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dialogflow","download_url":"https://codeload.github.com/dialogflow/dialogflow-unity-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251785406,"owners_count":21643475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T05:32:34.395Z","updated_at":"2025-04-30T21:31:16.723Z","avatar_url":"https://github.com/dialogflow.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DEPRECATED Api.ai: Unity Plugin\n\n| Deprecated |\n|-------|\n| This Dialogflow client library and Dialogflow API V1 [have been deprecated and will be shut down on October 23th, 2019](https://blog.dialogflow.com/post/migrate-to-dialogflow-api-v2/). Please [migrate to Dialogflow API V2](https://cloud.google.com/dialogflow-enterprise/docs/migrating). |\n\nThe Api.ai Unity Plugin makes it easy to integrate the [Api.ai natural language processing API](http://api.ai) into your Unity project. Api.ai allows the use of voice commands and integration with dialog scenarios defined for a particular agent in Api.ai.\n\nThe Library provides a simple programming interface for making text and voice requests to the Api.ai service. \n\n## Getting started\n\nTo use the Unity SDK you must have Unity installed on your computer. See [official website](http://unity3d.com) for details.\n\n### Installation\n\n[Download](https://www.assetstore.unity3d.com/en/#!/content/31498) plugin bundle from the Unity Asset Store, and unpack it to the Assets folder in your project.\n\n### Example\n\nFirst, load and watch the **ServiceSample** scene from the Api.ai bundle. \n\n![Api.ai Bundle](docs/images/bundle.png)\n\nThis sample demonstrates Api.ai SDK features.\n\n![Running Sample](docs/images/runningSample.png)\n\n* Top buttons start and stop listening accordingly.\n* With the text field and **Send Text** button you can make text requests to the server.\n* **Android Recognizer** button demonstrates recognition options available only on Android devices.\n\n**ApiAiModule** file contains C# code for the sample scene. You can take a look at the methods in the module.\n\n#### Initialization\n\nIn the ‘Start’ method, initialize the ‘ApiAiUnity’ object with access keys (use your access keys from [api.ai](https://api.ai) console).\n\n```csharp\nconst string ACCESS_TOKEN = \"3485a96fb27744db83e78b8c4bc9e7b7\";\nvar config = new AIConfiguration(ACCESS_TOKEN, SupportedLanguage.English);\napiAiUnity = new ApiAiUnity();\napiAiUnity.Initialize(config);\n```\n\nand configure handlers for ‘OnResult’ and ‘OnError’ events\n\n```csharp\napiAiUnity.OnError += HandleOnError;\napiAiUnity.OnResult += HandleOnResult;\n```\n\n#### Results processing\n\nIn ‘HandleOnResult’ and ‘HandleOnError’ you need to process the server response. In the example, it will be printed to the text field.\n\n```csharp\nvoid HandleOnResult(object sender, AIResponseEventArgs e)\n{\n    var aiResponse = e.Response;\n    if (aiResponse != null)\n    {\n        var outText = JsonConvert.SerializeObject(aiResponse, jsonSettings);\n        Debug.Log(outText);\n        answerTextField.text = outText;\n    } \n    else\n    {\n        Debug.LogError(\"Response is null\");\n    }\n}\n```\n\n#### Start and stop listening\n\nThe methods ‘StartListening’ and ‘StopListening’ are connected to particular buttons in the scene UI.\n\n![OnClick](docs/images/buttonOnClick.png)\n\nIn the ‘StartListening` method you need to pass the ‘AudioSource’ object to the ‘apiAi’ object. ‘AudioSource’ will be used for audio capturing.\n``` csharp\npublic void StartListening()\n{   \n    var aud = GetComponent\u003cAudioSource\u003e();\n    apiAiUnity.StartListening(aud);\n}\n```\n\nThe ‘StopListening’ method is quite simple. All the work is done in the ‘ApiAi’ module.\n\n``` csharp\npublic void StopListening()\n{\n    try\n    {\n        apiAiUnity.StopListening();\n    } \n    catch (Exception ex)\n    {\n        Debug.LogException(ex);\n    }\n}\n```\n\n#### Text requests\n\nAnother way to make requests is through **text requests**. In this case, results will not fire the ‘OnResult’ event. All work will be done simultaneously.\n\n``` csharp\npublic void SendText()\n{\n    try\n    {\n        var text = inputTextField.text;\n        var response = apiAiUnity.TextRequest(text);\n        if (response != null)\n        {\n            var outText = JsonConvert.SerializeObject(response, jsonSettings);\n            Debug.Log(\"Result: \" + outText);\n            answerTextField.text = outText;\n        } \n        else\n        {\n            Debug.LogError(\"Response is null\");\n        }\n    } \n    catch (Exception ex)\n    {\n        Debug.LogException(ex);\n    }\n}\n```\n\n#### Native Android recognition\n\nIn some cases it will be useful to use the Google recognition engine built into the Android platform. This way has at least one advantage: using voice activity detection. This means you don't need to stop listening manually.\n\nTo start native Android recognition, all you need is a call appropriate method:\n\n``` csharp\npublic void StartNativeRecognition()\n{\n    try\n    {\n        apiAiUnity.StartNativeRecognition();\n    } catch (Exception ex)\n    {\n        Debug.LogException(ex);\n    }\n}\n```\n\nBut the Unity platform needs one more step to make the Unity\u003c=\u003eAndroid interop work. You need to call the Update method once per frame. To do this, enter the code below:\n\n``` csharp\n// Update is called once per frame\nvoid Update()\n{\n    if (apiAiUnity != null)\n    {\n        apiAiUnity.Update();\n    }\n}\n```\n\n### Create helper module\n\n* Add a new script to the Assets folder (ApiAiModule, for example) ![Create script](docs/images/create_script.png)\n\n* Your new module should look like this:\n\n  ``` csharp\n    using UnityEngine;\n    using System.Collections;\n\n    public class ApiAiModule : MonoBehaviour {\n\n        // Use this for initialization\n        void Start () {\n\n        }\n\n        // Update is called once per frame\n        void Update () {\n\n        }\n    }\n  ```\n\n* First add Api.ai using\n\n  ``` csharp\n    using fastJSON;\n    using ApiAiSDK;\n    using ApiAiSDK.Model;\n    using ApiAiSDK.Unity;\n  ```\n\n* Add a private field to your module to keep the reference to the SDK object:\n\n  ``` csharp\n    private ApiAiUnity apiAiUnity;\n  ```\n\n* At the start of your module, the ApiAiUnity object must be initialized. Required data for initialization are the keys from your development console on [api.ai](http://api.ai) service and a supported language:\n\n  ``` csharp\n    // Use this for initialization\n    void Start()\n    {\n        const string ACCESS_TOKEN = \"your_access_token\";\n\n        var config = new AIConfiguration(ACCESS_TOKEN, SupportedLanguage.English);\n\n        apiAiUnity = new ApiAiUnity();\n        apiAiUnity.Initialize(config);\n\n        apiAiUnity.OnResult += HandleOnResult;\n        apiAiUnity.OnError += HandleOnError;\n    }\n  ```\n\n* ‘OnError’ and ‘OnResult’ events are used for processing service results. Handling functions must look like this:\n\n  ``` csharp\n    void HandleOnResult(object sender, AIResponseEventArgs e)\n    {\n        var aiResponse = e.Response;\n        if (aiResponse != null) {\n            // get data from aiResponse\n        } else {\n            Debug.LogError(\"Response is null\");\n        }\n    }\n\n    void HandleOnError(object sender, AIErrorEventArgs e)\n    {\n        Debug.LogException(e.Exception);\n    }\n  ```\n\n### Usage\n\nApiAi Unity SDK lets you perform the following actions:\n\n1. Start the listening process and then send voice data to the Api.ai service for recognition and processing\n2. Send a simple text request to the Api.ai service\n3. Use the integrated Android recognition engine to send recognized text to the Api.ai service for processing\n\n#### Using Api.ai recognition\n\nTo use the Api.ai voice recognition service you need to provide the ApiAiUnity object with a valid ‘AudioSource’ object. This can usually be received by using the `GetComponent\u003cAudioSource\u003e()` function.\n\nA temporary limitation of this situation is that if you are using Api.ai recognition, you need to stop listening manually. You can use these code snippets to start and stop listening:\n\n``` csharp\npublic void StartListening()\n{\n    try {\n        var aud = GetComponent\u003cAudioSource\u003e();\n        apiAiUnity.StartListening(aud);\n    } catch (Exception ex) {\n        Debug.LogException(ex);\n    }\n}\n\npublic void StopListening()\n{\n    try {\n        apiAiUnity.StopListening();\n    } catch (Exception ex) {\n        Debug.LogException(ex);\n    }\n}\n```\n\nAfter you start or stop listening, you will receive the Api.ai result in the ‘OnResult’ handler.\n\n**Note**: In some cases the Unity application must get Sound Recording privileges in order to use the Microphone. To do so, change your helper module Start function like this: \n\n``` csharp\nIEnumerator Start()\n{\n    // check access to the Microphone\n    yield return Application.RequestUserAuthorization (UserAuthorization.Microphone);\n    if (!Application.HasUserAuthorization(UserAuthorization.Microphone)) {\n        throw new NotSupportedException (\"Microphone using not authorized\");\n    }\n\n    ... // apiAiUnity initialization...\n}\n```\n\n#### Simple text requests\n\nThe use of text requests is very simple. All you need is a text query.\n\n``` csharp\npublic void SendText()\n{\n    var text = \"hello\";\n    try {\n        var response = apiAiUnity.TextRequest(text);\n        if (response != null) {\n            // process response\n        } else {\n            Debug.LogError(\"Response is null\");\n        }\n    } catch (Exception ex) {\n        Debug.LogException(ex);\n    }\n}\n```\n\n**Note**: You will receive the Api.ai result immediately, not in the ‘OnResult’ handler.\n\n#### Using native Android recognition\n\nThis situation is only applicable for Android Unity applications. You can check if an application is running on the Android platform using this simple code snippet:\n\n``` csharp\nif (Application.platform == RuntimePlatform.Android) {\n    // you can use Android recognition here\n}\n```\n\nBecause native Android recognition uses a Unity-to-Native bridge, you need to add the following code to the script ‘Update’ method. This code is used to check recognition results from the native layer, since callbacks are not supported in this case.\n\n``` csharp\nif (apiAiUnity != null) {\n    apiAiUnity.Update();\n}\n```\n\nTo start the recognition process, use the simple call of the ‘StartNativeRecognition’ method.\n\n``` csharp\npublic void StartNativeRecognition(){\n    try {\n        apiAiUnity.StartNativeRecognition();\n    } catch (Exception ex) {\n        Debug.LogException (ex);\n    }\n}\n```\n\n## How to make contributions?\nPlease read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## License\nSee [LICENSE](LICENSE).\n\n## Terms\nYour use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the [Google APIs Terms of Service](https://developers.google.com/terms/).\n\nThis is not an official Google product.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdialogflow%2Fdialogflow-unity-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdialogflow%2Fdialogflow-unity-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdialogflow%2Fdialogflow-unity-client/lists"}