回答編集履歴

3

空行の入れ方を修正

2018/01/20 14:16

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -362,12 +362,12 @@
362
362
 
363
363
  public LineStrip waveLineStrip;
364
364
 
365
+
366
+
365
367
  private float soundVolume;
366
368
 
367
369
  private Coroutine stoppingSoundCoroutine;
368
370
 
369
-
370
-
371
371
  private float[] waveData;
372
372
 
373
373
  private float waveFormOffsetY;

2

使用例を追記

2018/01/20 14:16

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -333,3 +333,177 @@
333
333
  }
334
334
 
335
335
  ```
336
+
337
+
338
+
339
+ ###追記
340
+
341
+ 上記LineStripを使う側のスクリプトでは、たとえば下記のようにPositionsを書き換えていくことになるでしょう。試しに[AudioSource](https://docs.unity3d.com/ScriptReference/AudioSource.html)の出している音を[GetOutputData](https://docs.unity3d.com/ScriptReference/AudioSource.GetOutputData.html)で取得してPositionsに反映させてみました。
342
+
343
+
344
+
345
+ ```C#
346
+
347
+ using System.Collections;
348
+
349
+ using UnityEngine;
350
+
351
+
352
+
353
+ public class WaveForm : MonoBehaviour
354
+
355
+ {
356
+
357
+ private const int DataLength = 256;
358
+
359
+
360
+
361
+ public AudioSource soundSource;
362
+
363
+ public LineStrip waveLineStrip;
364
+
365
+ private float soundVolume;
366
+
367
+ private Coroutine stoppingSoundCoroutine;
368
+
369
+
370
+
371
+ private float[] waveData;
372
+
373
+ private float waveFormOffsetY;
374
+
375
+
376
+
377
+ // 音量によって波の振幅が変化しているのをアピールするため、停止時にフェードアウトさせるようにしました
378
+
379
+ private IEnumerator StopSound(float time)
380
+
381
+ {
382
+
383
+ while (this.soundSource.volume > 0.0f)
384
+
385
+ {
386
+
387
+ this.soundSource.volume -= (this.soundVolume * Time.deltaTime) / time;
388
+
389
+ yield return null;
390
+
391
+ }
392
+
393
+
394
+
395
+ this.soundSource.Stop();
396
+
397
+ this.stoppingSoundCoroutine = null;
398
+
399
+ }
400
+
401
+
402
+
403
+ private void Start()
404
+
405
+ {
406
+
407
+ this.soundVolume = this.soundSource.volume;
408
+
409
+ this.waveData = new float[DataLength];
410
+
411
+ this.waveLineStrip.Positions = new Vector2[DataLength]; // Positionsに新しいVector2配列をセット→DataLength個のコーナーを持つ折れ線になる
412
+
413
+
414
+
415
+ for (var i = 0; i < DataLength; i++)
416
+
417
+ {
418
+
419
+ this.waveLineStrip.Positions[i].x = i; // 各コーナーのX座標を書き換える
420
+
421
+ }
422
+
423
+
424
+
425
+ this.waveLineStrip.SetVerticesDirty(); // Positionsを書き換えたので、メッシュの更新が必要なことを通知する
426
+
427
+ this.waveFormOffsetY = this.waveLineStrip.rectTransform.rect.height * 0.5f;
428
+
429
+ }
430
+
431
+
432
+
433
+ private void Update()
434
+
435
+ {
436
+
437
+ // スペースキーで音声の再生・停止を切り替える
438
+
439
+ if (Input.GetKeyDown(KeyCode.Space))
440
+
441
+ {
442
+
443
+ var stopping = this.stoppingSoundCoroutine != null;
444
+
445
+
446
+
447
+ if (this.soundSource.isPlaying && !stopping)
448
+
449
+ {
450
+
451
+ this.stoppingSoundCoroutine = StartCoroutine(StopSound(5.0f));
452
+
453
+ }
454
+
455
+ else
456
+
457
+ {
458
+
459
+ if (stopping)
460
+
461
+ {
462
+
463
+ this.StopCoroutine(this.stoppingSoundCoroutine);
464
+
465
+ this.stoppingSoundCoroutine = null;
466
+
467
+ }
468
+
469
+
470
+
471
+ this.soundSource.volume = this.soundVolume;
472
+
473
+ this.soundSource.Play();
474
+
475
+ }
476
+
477
+ }
478
+
479
+
480
+
481
+ this.soundSource.GetOutputData(this.waveData, 0); // soundSourceが現在再生している音声データをwaveDataに取り出す
482
+
483
+
484
+
485
+ for (var i = 0; i < DataLength; i++)
486
+
487
+ {
488
+
489
+ this.waveLineStrip.Positions[i].y =
490
+
491
+ this.waveFormOffsetY + (this.waveData[i] * 64.0f); // waveDataの値に応じて各コーナーのY座標を書き換える
492
+
493
+ }
494
+
495
+
496
+
497
+ this.waveLineStrip.SetVerticesDirty(); // Positionsを書き換えたので、メッシュの更新が必要なことを通知する
498
+
499
+ }
500
+
501
+ }
502
+
503
+ ```
504
+
505
+ ![プレビュー](661689fbbd9dde0711656852c1ca68d1.gif)
506
+
507
+
508
+
509
+ コード内コメントにあるように、今回の例ではPositions変更後にいちいち[SetVerticesDirty](https://docs.unity3d.com/ScriptReference/UI.Graphic.SetVerticesDirty.html)を実行しないと変更が反映されなかったりします。Positions書き換え用のメソッドを追加して、その中でSetVerticesDirtyを行うようにした方が使いやすいかもしれません。

1

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

2018/01/20 14:12

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -12,8 +12,12 @@
12
12
 
13
13
  using System.Linq;
14
14
 
15
+ #if UNITY_EDITOR
16
+
15
17
  using UnityEditor;
16
18
 
19
+ #endif
20
+
17
21
  using UnityEngine;
18
22
 
19
23
  using UnityEngine.EventSystems;