Tanoda
VoiceAction.cs
Go to the documentation of this file.
1using System.Runtime.Serialization;
2using System.Collections;
3using UnityEngine;
4using UnityEngine.UI;
5using System.Diagnostics;
6using System.IO;
7
8public class VoiceAction : ActionObject, ISerializable
9{
10 public Dropdown selector; //TODO: replace to AutoCompleate ComboBox
11 public InputField inputField;
12
13#if DANA
14 internal string selectedLanguage;
15 internal string textToSpeak;
16 private bool deactivated;
17 private bool once = true;
18
19 public override void Start()
20 {
21 base.Start();
22 if (!string.IsNullOrEmpty(textToSpeak) && inputField) inputField.text = textToSpeak;
23 FillDropdown();
24 selector.onValueChanged.AddListener(OnDropdownChanged);
25 }
26
27 private void FillDropdown()
28 {
29 selector.ClearOptions();
30 var languages = VoiceTTS.InstalledLanguages();
31 if (languages.Length == 0) return;
32 foreach (var o in languages) selector.options.Add(new Dropdown.OptionData(o));
33
34 if (!string.IsNullOrEmpty(selectedLanguage))
35 {
36 for (var i = 0; i < selector.options.Count; i++)
37 if (selector.options[i].text == selectedLanguage)
38 {
39 selector.value = i;
40 OnDropdownChanged(i);
41 return;
42 }
43 }
44 else
45 {
46 OnDropdownChanged(0);
47 selectedLanguage = languages[0];
48 }
49 }
50
51 public void OnTextEditFinished(string value)
52 {
53 textToSpeak = value;
54 VoiceTTS.Speak(textToSpeak, selectedLanguage);
55 }
56
57 public void OnDropdownChanged(int value)
58 {
59 selectedLanguage = selector.options[value].text;
60 }
61
62 public override void Deactivate()
63 {
64 base.Deactivate();
65 deactivated = true;
66 }
67
68
69
70 public override void Triggered(string id)
71 {
73 {
74 if (deactivated)
75 return;
76
77 once = true;
78 var proc = VoiceTTS.Speak(textToSpeak, selectedLanguage);
79 BlurTheScene.Instance.showBlur(proc, textToSpeak);
80 base.Triggered(id);
81 StartCoroutine(waiter(proc));
82 }
83 else
84 {
85 TriggerOutput(outPuts[0].name);
86 statusImg.color = Color.green;
87 }
88 }
89 public IEnumerator waiter(Process p)
90 {
91 //Wait for 4 seconds
92 //yield return new WaitForSeconds(4);
93 while (!p.HasExited)
94 {
95 yield return null;
96 }
97 //p.WaitForExit();
98 TriggerOutput(outPuts[0].name);
99 statusImg.color = Color.green;
101 yield return null;
102
103 }
104
105 public void TriggerNow()
106 {
107 TriggerOutput(outPuts[0].name);
108 statusImg.color = Color.green;
109 }
110
111 public new void GetObjectData(SerializationInfo info, StreamingContext context)
112 {
113 base.GetObjectData(info, context);
114 info.AddValue("lang", selectedLanguage, typeof(string));
115 info.AddValue("textToSpeak", textToSpeak, typeof(string));
116 }
117
118 public VoiceAction(SerializationInfo info, StreamingContext context) : base(info, context)
119 {
120 selectedLanguage = (string) info.GetValue("lang", typeof(string));
121 textToSpeak = info.GetString("textToSpeak");
122 }
123#endif
124}
UnityEngine.Color Color
Definition: TestScript.cs:32
Image statusImg
Definition: ActionObject.cs:21
virtual void Deactivate()
void TriggerOutput(string id)
virtual void Start()
virtual void Triggered(string id)
List< GameObject > outPuts
Definition: ActionObject.cs:19
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
void showBlur(Process p, string text)
Definition: BlurTheScene.cs:57
static BlurTheScene Instance
Definition: BlurTheScene.cs:17
void hideBlur()
Definition: BlurTheScene.cs:66
Dropdown selector
Definition: VoiceAction.cs:10
InputField inputField
Definition: VoiceAction.cs:11