質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%

Q&A

1回答

470閲覧

Unity サウンドマネージャー

M_Kazuma

総合スコア34

0グッド

0クリップ

投稿2018/01/29 14:16

特にゲームが止まるわけでもないのですがQiitaでサウンドマネージャーを使わせてもらっていたのですがなぜかこのようなものが出るのですが分かる方いますか?
モノは問題なく使えますが気になるので消せるのなら消したいです
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
Sound:.ctor() (at Assets/Scripts/Sound/Sound.cs:57)
Sound:GetInstance() (at Assets/Scripts/Sound/Sound.cs:22)
Sound:LoadBgm(String, String) (at Assets/Scripts/Sound/Sound.cs:106)
GameMane:Awake() (at Assets/Scripts/SceneScripts/Main/GameMane.cs:10)

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Sound : MonoBehaviour { 6 7 /// SEチャンネル数 8 const int SE_CHANNEL = 4; 9 10 /// サウンド種別 11 enum eType 12 { 13 Bgm, // BGM 14 Se, // SE 15 } 16 17 // シングルトン 18 static Sound _singleton = null; 19 // インスタンス取得 20 public static Sound GetInstance() 21 { 22 return _singleton ?? (_singleton = new Sound()); 23 } 24 25 // サウンド再生のためのゲームオブジェクト 26 GameObject _object = null; 27 // サウンドリソース 28 AudioSource _sourceBgm = null; // BGM 29 AudioSource _sourceSeDefault = null; // SE (デフォルト) 30 AudioSource[] _sourceSeArray; // SE (チャンネル) 31 // BGMにアクセスするためのテーブル 32 Dictionary<string, _Data> _poolBgm = new Dictionary<string, _Data>(); 33 // SEにアクセスするためのテーブル 34 Dictionary<string, _Data> _poolSe = new Dictionary<string, _Data>(); 35 36 /// 保持するデータ 37 class _Data 38 { 39 /// アクセス用のキー 40 public string Key; 41 /// リソース名 42 public string ResName; 43 /// AudioClip 44 public AudioClip Clip; 45 46 /// コンストラクタ 47 public _Data(string key, string res) 48 { 49 Key = key; 50 ResName = "Sounds/" + res; 51 // AudioClipの取得 52 Clip = Resources.Load(ResName) as AudioClip; 53 } 54 } 55 56 /// コンストラクタ 57 public Sound() 58 { 59 // チャンネル確保 60 _sourceSeArray = new AudioSource[SE_CHANNEL]; 61 } 62 63 /// AudioSourceを取得する 64 AudioSource _GetAudioSource(eType type, int channel = -1) 65 { 66 if (_object == null) 67 { 68 // GameObjectがなければ作る 69 _object = new GameObject("Sound"); 70 // 破棄しないようにする 71 GameObject.DontDestroyOnLoad(_object); 72 // AudioSourceを作成 73 _sourceBgm = _object.AddComponent<AudioSource>(); 74 _sourceSeDefault = _object.AddComponent<AudioSource>(); 75 for (int i = 0; i < SE_CHANNEL; i++) 76 { 77 _sourceSeArray[i] = _object.AddComponent<AudioSource>(); 78 } 79 } 80 81 if (type == eType.Bgm) 82 { 83 // BGM 84 return _sourceBgm; 85 } 86 else 87 { 88 // SE 89 if (0 <= channel && channel < SE_CHANNEL) 90 { 91 // チャンネル指定 92 return _sourceSeArray[channel]; 93 } 94 else 95 { 96 // デフォルト 97 return _sourceSeDefault; 98 } 99 } 100 } 101 102 // サウンドのロード 103 // ※Resources/Soundsフォルダに配置すること 104 public static void LoadBgm(string key, string resName) 105 { 106 GetInstance()._LoadBgm(key, resName); 107 } 108 public static void LoadSe(string key, string resName) 109 { 110 GetInstance()._LoadSe(key, resName); 111 } 112 void _LoadBgm(string key, string resName) 113 { 114 if (_poolBgm.ContainsKey(key)) 115 { 116 // すでに登録済みなのでいったん消す 117 _poolBgm.Remove(key); 118 } 119 _poolBgm.Add(key, new _Data(key, resName)); 120 } 121 void _LoadSe(string key, string resName) 122 { 123 if (_poolSe.ContainsKey(key)) 124 { 125 // すでに登録済みなのでいったん消す 126 _poolSe.Remove(key); 127 } 128 _poolSe.Add(key, new _Data(key, resName)); 129 } 130 131 /// BGMの再生 132 /// ※事前にLoadBgmでロードしておくこと 133 public static bool PlayBgm(string key) 134 { 135 return GetInstance()._PlayBgm(key); 136 } 137 bool _PlayBgm(string key) 138 { 139 if (_poolBgm.ContainsKey(key) == false) 140 { 141 // 対応するキーがない 142 return false; 143 } 144 145 // いったん止める 146 _StopBgm(); 147 148 // リソースの取得 149 var _data = _poolBgm[key]; 150 151 // 再生 152 var source = _GetAudioSource(eType.Bgm); 153 source.loop = true; 154 source.clip = _data.Clip; 155 source.Play(); 156 157 return true; 158 } 159 /// BGMの停止 160 public static bool StopBgm() 161 { 162 return GetInstance()._StopBgm(); 163 } 164 bool _StopBgm() 165 { 166 _GetAudioSource(eType.Bgm).Stop(); 167 168 return true; 169 } 170 171 /// SEの再生 172 /// ※事前にLoadSeでロードしておくこと 173 public static bool PlaySe(string key, int channel = -1) 174 { 175 return GetInstance()._PlaySe(key, channel); 176 } 177 bool _PlaySe(string key, int channel = -1) 178 { 179 if (_poolSe.ContainsKey(key) == false) 180 { 181 // 対応するキーがない 182 return false; 183 } 184 185 // リソースの取得 186 var _data = _poolSe[key]; 187 188 if (0 <= channel && channel < SE_CHANNEL) 189 { 190 // チャンネル指定 191 var source = _GetAudioSource(eType.Se, channel); 192 source.clip = _data.Clip; 193 source.Play(); 194 } 195 else 196 { 197 // デフォルトで再生 198 var source = _GetAudioSource(eType.Se); 199 source.PlayOneShot(_data.Clip); 200 } 201 202 return true; 203 } 204} 205番号リスト 206

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

MonoBehaviourを継承したクラスであるSoundを、GetInstance内のreturn _singleton ?? (_singleton = new Sound());の部分でnewを使って新規生成していることによる警告でしょう。
MonoBehaviourの生成は基本的にUnityの管理下にあるので、勝手にnewで生成されてしまうと都合が悪いため、警告を表示しているものと思われます。

多くのスクリプトはゲームオブジェクトにアタッチして使用するのでMonoBehaviourの継承が必要ですが、このSoundクラスはアタッチせず単独で使うよう設計されている様子です。ですので、MonoBehaviourの継承をしないよう書き換えてみてはいかがでしょうか。

【Unity】サウンド管理クラスの設計 - Qiitaで紹介されているSoundのクラス定義冒頭部分は

C#

1using UnityEngine; 2using System.Collections; 3using System.Collections.Generic; 4 5/// サウンド管理 6public class Sound { 7 8// 省略

となっており、MonoBehaviourを継承していませんでした。

参考: MonoBehaviourを継承しないという選択肢【Unity】 - (:3[kanのメモ帳]

投稿2018/01/29 20:34

編集2018/01/29 21:59
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問