Tanoda
SoundAction.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Runtime.Serialization;
4using GILES.Interface;
5using UnityEngine;
6using UnityEngine.UI;
7
8public class SoundAction : ActionObject, ISerializable
9{
10 public Dropdown selector; //TODO: replace to AutoCompleate ComboBox
12
13 internal string selectedSound;
14 internal bool triggerNextOnSoundEnd;
15 private bool deactivated;
16 private bool once = true;
17
18 public override void Start()
19 {
20 base.Start();
21 player.SoundStopped += Player_SoundStopped;
22 FillDropdown();
23 selector.onValueChanged.AddListener(OnDropdownChanged);
24 if (triggerNextOnSoundEnd) GetComponentInChildren<Toggle>().isOn = true;
25 }
26
27 private void Player_SoundStopped(object sender, EventArgs e)
28 {
29 if (!once)
30 return;
31
32 once = false;
33
34#if !UNITY_WEBGL
35 statusImg.color = Color.red;
36#endif
37 if (triggerNextOnSoundEnd) TriggerNow();
38 }
39
40 public void PlayStopPreview()
41 {
42 if (player.isPlaying)
44 else
46 }
47
48 private void FillDropdown()
49 {
50 selector.ClearOptions();
51 var prefabBrowser = FindObjectOfType<pb_PrefabBrowser>();
52 var soundList = prefabBrowser.Sounds.transform.GetComponentsInChildren<Text>();
53 foreach (var o in soundList) selector.options.Add(new Dropdown.OptionData(o.text));
55
56 if (!string.IsNullOrEmpty(selectedSound))
57 for (var i = 0; i < selector.options.Count; i++)
58 if (selector.options[i].text == selectedSound)
59 {
60 selector.value = i;
62 return;
63 }
64 else
66 }
67
68 public void OnDropdownChanged(int value)
69 {
71 if (selector.options.Count < value + 1)
72 {
73 return;
74 }
75 selectedSound = selector.options[value].text;
76#if UNITY_WEBGL
77 var result = new [] {"/fileuploader/" + selectedSound};
78 var files = FindObjectsOfType<NAudioPlayer>(true);
79 foreach (var file in files)
80 {
81 if (string.Equals(file.name, selectedSound, StringComparison.OrdinalIgnoreCase))
82 {
83 result[0] = file.filePath;
84 break;
85 }
86 }
87#else
88 var result = Directory.GetFiles(Application.streamingAssetsPath, selector.options[value].text,
89 SearchOption.AllDirectories);
90#endif
91 if (result.Length == 1)
92 player.SetFilePath(result[0]);
93 else
94 Debug.LogError("More than one sound file found with the same name! (or zero)");
95 }
96
97 public override void Deactivate()
98 {
99 base.Deactivate();
100 deactivated = true;
101 }
102
103 public override void Triggered(string id)
104 {
105 if (deactivated)
106 return;
107
108 once = true;
109
110 if (string.IsNullOrEmpty(player.filePath))
111 {
112 var result = Directory.GetFiles(Application.streamingAssetsPath, selectedSound,
113 SearchOption.AllDirectories);
114 if (result.Length >= 1)
115 player.SetFilePath(result[0]);
116 }
117
118 base.Triggered(id);
119
121
122 if (!triggerNextOnSoundEnd) TriggerNow();
123 }
124
125 public void OnToggleChanged(bool value)
126 {
127 triggerNextOnSoundEnd = value;
128 }
129
130 public void TriggerNow()
131 {
132 TriggerOutput(outPuts[0].name);
133#if !UNITY_WEBGL
134 statusImg.color = Color.green;
135#endif
136 }
137
138 public new void GetObjectData(SerializationInfo info, StreamingContext context)
139 {
140 base.GetObjectData(info, context);
141 info.AddValue("selectedSound", selectedSound, typeof(string));
142 info.AddValue("triggerNextOnSoundEnd", triggerNextOnSoundEnd, typeof(bool));
143 }
144
145 public SoundAction(SerializationInfo info, StreamingContext context) : base(info, context)
146 {
147 selectedSound = (string) info.GetValue("selectedSound", typeof(string));
148 try
149 {
150 triggerNextOnSoundEnd = info.GetBoolean("triggerNextOnSoundEnd");
151 }
152 catch (Exception)
153 {
154 // ignored, old save
155 }
156 }
157}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
Image statusImg
Definition: ActionObject.cs:21
void TriggerOutput(string id)
List< GameObject > outPuts
Definition: ActionObject.cs:19
void SetFilePath(string path)
Definition: NAudioPlayer.cs:64
void PlayAudio()
Definition: NAudioPlayer.cs:70
void StopAudio()
EventHandler SoundStopped
Definition: NAudioPlayer.cs:24
NAudioPlayer player
Definition: SoundAction.cs:11
void PlayStopPreview()
Definition: SoundAction.cs:40
SoundAction(SerializationInfo info, StreamingContext context)
Definition: SoundAction.cs:145
override void Deactivate()
Definition: SoundAction.cs:97
override void Triggered(string id)
Definition: SoundAction.cs:103
new void GetObjectData(SerializationInfo info, StreamingContext context)
Definition: SoundAction.cs:138
void OnToggleChanged(bool value)
Definition: SoundAction.cs:125
void OnDropdownChanged(int value)
Definition: SoundAction.cs:68
Dropdown selector
Definition: SoundAction.cs:10
override void Start()
Definition: SoundAction.cs:18
void TriggerNow()
Definition: SoundAction.cs:130