Instructions.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using Meta.WitAi;
  21. using Meta.WitAi.Data.Configuration;
  22. using UnityEngine;
  23. using UnityEngine.UI;
  24. #if UNITY_EDITOR
  25. using UnityEditor;
  26. #endif
  27. namespace Oculus.Voice.Demo.UIShapesDemo
  28. {
  29. [ExecuteAlways]
  30. public class Instructions : MonoBehaviour
  31. {
  32. internal enum Step
  33. {
  34. SetupWit = 0,
  35. MissingServerToken,
  36. MissingClientToken,
  37. AddConfig,
  38. AddVoiceExperiences,
  39. SetConfig,
  40. Ready
  41. }
  42. static readonly string[] steps = new string[]
  43. {
  44. "Create an application at https://wit.ai. You can import the \"shapes_demo - Wit.ai Config.zip\" in the Demo/Data directory to create it for you.\n\nConnect to the Wit.ai app by clicking Oculus>Voice SDK>Settings and copy the Server Access Token from the Wit.ai app's settings page.Next, create a new Wit configuration by clicking Create.",
  45. "Copy the Server Access Token from the Wit.ai app's settings page and paste it in field found in Oculus/Voice SDK/Settings.",
  46. "Wit configuration is missing a Client Access Token. Open the Wit configuration, expand Application Configuration, and click Refresh or paste a Client Access Token from your Wit.ai app settings page.",
  47. "Create a Wit configuration by clicking Assets/Create/Voice SDK/Configuration.",
  48. "The scene is missing the App Voice Experience component. Add it by clicking Assets/Create/Voice SDK/Add App Voice Experience to Scene.",
  49. "The App Voice Experience GameObject is missing its Wit configuration. Set the configuration to begin trying voice commands.",
  50. ""
  51. };
  52. [SerializeField] private Text instructionText;
  53. private Step currentStep = Step.Ready;
  54. internal Step CurrentStep => currentStep;
  55. internal string CurrentStepText => steps[(int) currentStep];
  56. private void OnValidate()
  57. {
  58. UpdateStep();
  59. }
  60. private void OnEnable()
  61. {
  62. UpdateStep();
  63. }
  64. private void Update()
  65. {
  66. UpdateStep();
  67. }
  68. private void UpdateStep()
  69. {
  70. #if UNITY_EDITOR
  71. var appVoiceExperience = FindObjectOfType<AppVoiceExperience>();
  72. string[] guids = AssetDatabase.FindAssets("t:WitConfiguration");
  73. if (guids.Length == 0)
  74. {
  75. currentStep = Step.SetupWit;
  76. }
  77. else if (!appVoiceExperience)
  78. {
  79. currentStep = Step.AddVoiceExperiences;
  80. }
  81. else if (!appVoiceExperience.RuntimeConfiguration.witConfiguration)
  82. {
  83. currentStep = Step.SetConfig;
  84. appVoiceExperience.RuntimeConfiguration.witConfiguration =
  85. AssetDatabase.LoadAssetAtPath<WitConfiguration>(
  86. AssetDatabase.GUIDToAssetPath(guids[0]));
  87. }
  88. else if (!WitAuthUtility.IsServerTokenValid())
  89. {
  90. currentStep = Step.MissingServerToken;
  91. }
  92. else if (string.IsNullOrEmpty(appVoiceExperience.RuntimeConfiguration?.witConfiguration?
  93. .GetClientAccessToken()))
  94. {
  95. currentStep = Step.MissingClientToken;
  96. }
  97. else
  98. {
  99. currentStep = Step.Ready;
  100. }
  101. instructionText.text = steps[(int) currentStep];
  102. #endif
  103. }
  104. }
  105. #if UNITY_EDITOR
  106. [CustomEditor(typeof(Instructions))]
  107. public class InstructionManagerEditor : Editor
  108. {
  109. public override void OnInspectorGUI()
  110. {
  111. var instructions = (Instructions) target;
  112. if (instructions.CurrentStep == Instructions.Step.Ready)
  113. {
  114. GUILayout.Label(
  115. "Everything is ready. Press play to test activation via the Activate button.");
  116. }
  117. else
  118. {
  119. GUILayout.TextArea(instructions.CurrentStepText);
  120. GUILayout.Space(32);
  121. switch (instructions.CurrentStep)
  122. {
  123. case Instructions.Step.SetupWit:
  124. SetupWitResources();
  125. break;
  126. case Instructions.Step.MissingServerToken:
  127. MissingServerTokenResources();
  128. break;
  129. case Instructions.Step.MissingClientToken:
  130. MissingClientTokenResources();
  131. break;
  132. }
  133. }
  134. }
  135. private void MissingClientTokenResources()
  136. {
  137. GUILayout.Label("Resources", EditorStyles.boldLabel);
  138. if (GUILayout.Button("Select Wit Config"))
  139. {
  140. Selection.activeObject = (FindObjectOfType<AppVoiceExperience>()
  141. .RuntimeConfiguration.witConfiguration);
  142. }
  143. if (GUILayout.Button("Open Wit.ai"))
  144. {
  145. Application.OpenURL("https://wit.ai/apps");
  146. }
  147. }
  148. private void MissingServerTokenResources()
  149. {
  150. GUILayout.Label("Resources", EditorStyles.boldLabel);
  151. if (GUILayout.Button("Open Wit.ai"))
  152. {
  153. Application.OpenURL("https://wit.ai/apps");
  154. }
  155. }
  156. private void SetupWitResources()
  157. {
  158. GUILayout.Label("Resources", EditorStyles.boldLabel);
  159. if (GUILayout.Button("Open Wit.ai"))
  160. {
  161. Application.OpenURL("https://wit.ai/apps");
  162. }
  163. GUILayout.Label("Wit.ai Sample Application File");
  164. GUILayout.BeginHorizontal();
  165. if (GUILayout.Button("Open In Explorer"))
  166. {
  167. EditorUtility.RevealInFinder("Assets/Oculus/Voice/Demo/Data/");
  168. }
  169. if (GUILayout.Button("Copy Path"))
  170. {
  171. GUIUtility.systemCopyBuffer = Application.dataPath + "/Oculus/Voice/Demo/Data";
  172. }
  173. GUILayout.EndHorizontal();
  174. }
  175. }
  176. #endif
  177. }