回答編集履歴
3
音声名を自動取得するコードを追加 音声をサウンドに修正
answer
CHANGED
@@ -3,18 +3,19 @@
|
|
3
3
|
|
4
4
|
###使い方説明 下準備
|
5
5
|
1. Resourcesフォルダを適当な場所に作成
|
6
|
-
2. 読み出したい
|
6
|
+
2. 読み出したいサウンドファイルを制作したフォルダ内に配置
|
7
7
|
仮にbird1.mp3、bird2.mp3、bird3.mp3、bird4.mp3としておきます。
|
8
8
|
準備完了
|
9
9
|
今後も使いそうでしたらわかりやすいようにResources/bird_voicesなどのフォルダを作ってその中に入れてもいいと思います。とりあえず今回は入れている程で進めます。
|
10
10
|
|
11
11
|
###実装
|
12
|
-
**・
|
12
|
+
**・サウンドの配置場所を以下と仮定して進めます。**
|
13
13
|
Resources/bird_voices/bird1.mp3
|
14
14
|
Resources/bird_voices/bird2.mp3
|
15
15
|
Resources/bird_voices/bird3.mp3
|
16
16
|
Resources/bird_voices/bird4.mp3
|
17
17
|
|
18
|
+
###サウンド名を指定するパターン
|
18
19
|
```C#
|
19
20
|
using UnityEngine;
|
20
21
|
using System.Collections;
|
@@ -52,5 +53,37 @@
|
|
52
53
|
}
|
53
54
|
```
|
54
55
|
|
56
|
+
###サウンド名を自動で取得するパターン
|
57
|
+
```C#
|
58
|
+
using UnityEngine;
|
59
|
+
using System.Collections;
|
60
|
+
|
61
|
+
public class birdManager : MonoBehaviour {
|
62
|
+
|
63
|
+
[SerializeField]
|
64
|
+
//鳥のプレハブをセット
|
65
|
+
//※ AudioSourceをつけ忘れないように注意
|
66
|
+
GameObject birdPrefab;
|
67
|
+
|
68
|
+
//サウンドが保存されている場所のパス
|
69
|
+
string birdFolder = "bird_voices/";
|
70
|
+
|
71
|
+
void Awake()
|
72
|
+
{
|
73
|
+
//とりあえず10回ほどループ
|
74
|
+
for (int i = 0; i < 10; i++)
|
75
|
+
{
|
76
|
+
GameObject obj = Instantiate(birdPrefab,new Vector3(Random.Range(1,100),Random.Range(7,10),Random.Range(1,100)), Quaternion.identity) as GameObject;
|
77
|
+
AudioSource source = obj.GetComponent<AudioSource>();
|
78
|
+
//Resources.LoadAllですべてのサウンドを取得 ()内はファルダ名
|
79
|
+
AudioClip[] clips = Resources.LoadAll<AudioClip>(birdFolder);
|
80
|
+
source.clip = clips[Random.Range(0,clips.Length)];
|
81
|
+
source.Play();
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
|
55
88
|
###Resoucesフォルダの注意点などはこちら
|
56
89
|
https://teratail.com/questions/48887
|
2
表現を修正
answer
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
{
|
45
45
|
GameObject obj = Instantiate(birdPrefab,new Vector3(Random.Range(1,100),Random.Range(7,10),Random.Range(1,100)), Quaternion.identity) as GameObject;
|
46
46
|
AudioSource source = obj.GetComponent<AudioSource>();
|
47
|
-
//Resources.Loadでサウンドをロード ()内は画像名パス
|
47
|
+
//Resources.Loadでサウンドをロード ()内は画像名をパスから指定
|
48
48
|
source.clip = Resources.Load<AudioClip>(birdFolder + birdVoiceName[Random.Range(0,birdVoiceName.Length)]);
|
49
49
|
source.Play();
|
50
50
|
}
|
1
誤字の修正
answer
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
https://teratail.com/questions/48887
|
3
3
|
|
4
4
|
###使い方説明 下準備
|
5
|
-
1. Resourcesフォルダを適当な場所に
|
5
|
+
1. Resourcesフォルダを適当な場所に作成
|
6
6
|
2. 読み出したい音声ファイルを制作したフォルダ内に配置
|
7
7
|
仮にbird1.mp3、bird2.mp3、bird3.mp3、bird4.mp3としておきます。
|
8
8
|
準備完了
|