teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

空行の入れ方を修正

2018/01/20 14:16

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -180,9 +180,9 @@
180
180
 
181
181
  public AudioSource soundSource;
182
182
  public LineStrip waveLineStrip;
183
+
183
184
  private float soundVolume;
184
185
  private Coroutine stoppingSoundCoroutine;
185
-
186
186
  private float[] waveData;
187
187
  private float waveFormOffsetY;
188
188
 

2

使用例を追記

2018/01/20 14:16

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -165,4 +165,91 @@
165
165
  vh.AddTriangle(offset + 2, offset + 3, offset + 0);
166
166
  }
167
167
  }
168
- ```
168
+ ```
169
+
170
+ ###追記
171
+ 上記LineStripを使う側のスクリプトでは、たとえば下記のようにPositionsを書き換えていくことになるでしょう。試しに[AudioSource](https://docs.unity3d.com/ScriptReference/AudioSource.html)の出している音を[GetOutputData](https://docs.unity3d.com/ScriptReference/AudioSource.GetOutputData.html)で取得してPositionsに反映させてみました。
172
+
173
+ ```C#
174
+ using System.Collections;
175
+ using UnityEngine;
176
+
177
+ public class WaveForm : MonoBehaviour
178
+ {
179
+ private const int DataLength = 256;
180
+
181
+ public AudioSource soundSource;
182
+ public LineStrip waveLineStrip;
183
+ private float soundVolume;
184
+ private Coroutine stoppingSoundCoroutine;
185
+
186
+ private float[] waveData;
187
+ private float waveFormOffsetY;
188
+
189
+ // 音量によって波の振幅が変化しているのをアピールするため、停止時にフェードアウトさせるようにしました
190
+ private IEnumerator StopSound(float time)
191
+ {
192
+ while (this.soundSource.volume > 0.0f)
193
+ {
194
+ this.soundSource.volume -= (this.soundVolume * Time.deltaTime) / time;
195
+ yield return null;
196
+ }
197
+
198
+ this.soundSource.Stop();
199
+ this.stoppingSoundCoroutine = null;
200
+ }
201
+
202
+ private void Start()
203
+ {
204
+ this.soundVolume = this.soundSource.volume;
205
+ this.waveData = new float[DataLength];
206
+ this.waveLineStrip.Positions = new Vector2[DataLength]; // Positionsに新しいVector2配列をセット→DataLength個のコーナーを持つ折れ線になる
207
+
208
+ for (var i = 0; i < DataLength; i++)
209
+ {
210
+ this.waveLineStrip.Positions[i].x = i; // 各コーナーのX座標を書き換える
211
+ }
212
+
213
+ this.waveLineStrip.SetVerticesDirty(); // Positionsを書き換えたので、メッシュの更新が必要なことを通知する
214
+ this.waveFormOffsetY = this.waveLineStrip.rectTransform.rect.height * 0.5f;
215
+ }
216
+
217
+ private void Update()
218
+ {
219
+ // スペースキーで音声の再生・停止を切り替える
220
+ if (Input.GetKeyDown(KeyCode.Space))
221
+ {
222
+ var stopping = this.stoppingSoundCoroutine != null;
223
+
224
+ if (this.soundSource.isPlaying && !stopping)
225
+ {
226
+ this.stoppingSoundCoroutine = StartCoroutine(StopSound(5.0f));
227
+ }
228
+ else
229
+ {
230
+ if (stopping)
231
+ {
232
+ this.StopCoroutine(this.stoppingSoundCoroutine);
233
+ this.stoppingSoundCoroutine = null;
234
+ }
235
+
236
+ this.soundSource.volume = this.soundVolume;
237
+ this.soundSource.Play();
238
+ }
239
+ }
240
+
241
+ this.soundSource.GetOutputData(this.waveData, 0); // soundSourceが現在再生している音声データをwaveDataに取り出す
242
+
243
+ for (var i = 0; i < DataLength; i++)
244
+ {
245
+ this.waveLineStrip.Positions[i].y =
246
+ this.waveFormOffsetY + (this.waveData[i] * 64.0f); // waveDataの値に応じて各コーナーのY座標を書き換える
247
+ }
248
+
249
+ this.waveLineStrip.SetVerticesDirty(); // Positionsを書き換えたので、メッシュの更新が必要なことを通知する
250
+ }
251
+ }
252
+ ```
253
+ ![プレビュー](661689fbbd9dde0711656852c1ca68d1.gif)
254
+
255
+ コード内コメントにあるように、今回の例ではPositions変更後にいちいち[SetVerticesDirty](https://docs.unity3d.com/ScriptReference/UI.Graphic.SetVerticesDirty.html)を実行しないと変更が反映されなかったりします。Positions書き換え用のメソッドを追加して、その中でSetVerticesDirtyを行うようにした方が使いやすいかもしれません。

1

UnityEditorに関するusingディレクティブについて、条件付きコンパイルを設定していなかったため追加

2018/01/20 14:12

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -5,7 +5,9 @@
5
5
 
6
6
  ```C#
7
7
  using System.Linq;
8
+ #if UNITY_EDITOR
8
9
  using UnityEditor;
10
+ #endif
9
11
  using UnityEngine;
10
12
  using UnityEngine.EventSystems;
11
13
  using UnityEngine.UI;