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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1273閲覧

Unity Resources.Loadで名前の違うアセットをランダムに選びたい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/06/01 05:42

Unityで自分で作ったアセットをResourcesフォルダからランダムに選択したいのですが可能でしょうか。

私の考えではクラス名が同じであればファイル名が違っても読み込めるだろうと考えています。

試しにThemeSelectorの処理でやってみましたがNULLが返ってきました。

Resourcesフォルダ内は以下のようになっていて今後それぞれのアセットが増える予定です。
Resourcesフォルダ内
Resourcesフォルダ内

using

1using System.Collections.Generic; 2using UnityEngine; 3using UnityEditor; 4using System.IO; 5 6public class DataCreator : EditorWindow 7{ 8 #region 定数 9 private const float WINDOWSIZEWIDTH = 500; 10 private const float WINDOWSIZEHEIGHT = 500; 11 private const float BUTTONWIDTH = 100; 12 private const float BUTTONHIGHT = 50; 13 #endregion 14 #region static変数 15 private static string folderPath = "Assets/Resources/"; 16 public static ThemesOneData settingOne; 17 public static ThemesTwoData settingTwo; 18 #endregion 19 #region 変数 20 private string fileName; 21 private string themeName; 22 private int count; 23 private int lineCount; 24 private int pieceCount; 25 private int themesRadio = 0; 26 private int themesTypeRadio = 0; 27 private ThemesRankData.ThemesRank rank; 28 private ThemesRankData.ThemesType type; 29 #endregion 30 31 [MenuItem("Editor/CreateThemesData")] 32 private static void Open() 33 { 34 var window = GetWindow<DataCreator>("ThemesData"); 35 window.minSize = new Vector2(WINDOWSIZEWIDTH, WINDOWSIZEHEIGHT); 36 } 37 38 private void OnGUI() 39 { 40 41 EditorGUILayout.LabelField("お題データを作成・編集するEditorです。"); 42 EditorGUILayout.HelpBox("空白のみで保存しないでください",MessageType.Warning); 43 GUILayout.BeginVertical(GUI.skin.box); 44 { 45 fileName = EditorGUILayout.TextField("ファイル名", fileName); 46 EditorGUILayout.HelpBox("ファイル名は英語で記入してください。", MessageType.Info); 47 48 themeName = EditorGUILayout.TextField("お題の名前", themeName); 49 50 themesRadio = GUILayout.SelectionGrid(themesRadio, new string[] { "テーマ1", "テーマ2" }, 2); 51 rank = (ThemesRankData.ThemesRank)EditorGUILayout.EnumPopup("難易度", rank); 52 if (themesRadio == 0) 53 { 54 EditorGUILayout.HelpBox("テーマ1を作成します。秒数かパズルの種類のどちらかを入力してください。", MessageType.Info); 55 themesTypeRadio = GUILayout.SelectionGrid(themesTypeRadio, new string[] { "秒数", "パズルの種類" }, 2); 56 if (themesTypeRadio == 0) 57 { 58 count = EditorGUILayout.IntField("秒数", count); 59 } 60 else 61 { 62 type = (ThemesRankData.ThemesType)EditorGUILayout.EnumPopup("パズルの種類", type); 63 } 64 65 } 66 else 67 { 68 EditorGUILayout.HelpBox("テーマ2を作成します。", MessageType.Info); 69 lineCount = EditorGUILayout.IntField("ライン数", lineCount); 70 pieceCount = EditorGUILayout.IntField("必要パズル数", pieceCount); 71 } 72 73 74 } 75 GUILayout.EndVertical(); 76 77 EditorGUILayout.BeginHorizontal(); 78 { 79 GUILayout.FlexibleSpace();//ボタンを右寄せ 80 if (GUILayout.Button("complete!", GUILayout.Width(BUTTONWIDTH), GUILayout.Height(BUTTONHIGHT))) 81 { 82 83 if (fileName == null||themeName==null||fileName==""||themeName=="")//未入力をチェック、できていなければ戻る 84 { 85 86 EditorUtility.DisplayDialog("注意!", "ファイル名またはお題の名前が入力されていないか空白が含まれています。", "OK"); 87 return; 88 } 89 if (!Directory.Exists(folderPath))//Resourcesフォルダの存在チェック,なければ作成 90 { 91 Directory.CreateDirectory(folderPath); 92 } 93 94 95 var filepath = folderPath + fileName +themesRadio+ ".asset";//保存先のPathを作成します 96 if(themesRadio == 0) 97 { 98 settingOne = AssetDatabase.LoadAssetAtPath<ThemesOneData>(filepath); 99 } 100 else 101 { 102 settingTwo = AssetDatabase.LoadAssetAtPath<ThemesTwoData>(filepath); 103 } 104 105 if (themesRadio == 0) 106 { 107 if (settingOne == null) 108 { // ロードしてnullだったら存在しないので生成 109 110 settingOne = CreateInstance<ThemesOneData>(); 111 AssetDatabase.CreateAsset(settingOne, filepath); 112 } 113 else 114 { 115 if (EditorUtility.DisplayDialog("注意!", "この名前のファイルは既に存在します。\n上書きしますか?", "上書き", "キャンセル")) 116 { 117 settingOne = AssetDatabase.LoadAssetAtPath<ThemesOneData>(filepath); 118 } 119 } 120 } 121 else 122 { 123 if (settingTwo == null) 124 { // ロードしてnullだったら存在しないので生成 125 126 settingTwo = CreateInstance<ThemesTwoData>(); 127 AssetDatabase.CreateAsset(settingTwo, filepath); 128 } 129 else 130 { 131 if (EditorUtility.DisplayDialog("注意!", "この名前のファイルは既に存在します。\n上書きしますか?", "上書き", "キャンセル")) 132 { 133 settingTwo = AssetDatabase.LoadAssetAtPath<ThemesTwoData>(filepath); 134 } 135 } 136 } 137 138 //ここから下の処理でデータを保存 139 140 if (themesRadio == 0) 141 { 142 143 settingOne.themeName = themeName; 144 if(themesTypeRadio ==0) 145 { 146 settingOne.count = count; 147 settingOne.themeType = ThemesRankData.ThemesType.None; 148 } 149 else 150 { 151 settingOne.count = 0; 152 settingOne.themeType = type; 153 } 154 settingOne.themeRank = rank; 155 156 } 157 else 158 { 159 settingTwo.themeName = themeName; 160 settingTwo.themeRank = rank; 161 settingTwo.lineCount = lineCount; 162 settingTwo.pieceCount = pieceCount; 163 } 164 165 166 } 167 168 } 169 EditorGUILayout.EndHorizontal(); 170 171 } 172 173 174} 175アセットを作成するEditorの処理

using

1using System.Collections.Generic; 2using UnityEngine; 3 4[CreateAssetMenu(menuName = "ScriptableObjects/Create_ThemesData")] 5public class ThemesOneData : ThemesRankData 6{ 7 8 [SerializeField,ReadOnly] 9 private string m_themeName; 10 11 [SerializeField,ReadOnly] 12 private ThemesRank m_themeRank ; 13 14 [SerializeField, ReadOnly] 15 private int m_count; 16 17 [SerializeField, ReadOnly] 18 private ThemesType m_themeType; 19 20 public string themeName { get { return m_themeName; } set { m_themeName = value; } } 21 public ThemesRank themeRank { get { return m_themeRank; } set { m_themeRank = value; } } 22 public int count { get { return m_count; } set { m_count = value; } } 23 public ThemesType themeType { get { return m_themeType; } set { m_themeType = value; } } 24 25} 26選択したいアセットの元スクリプト1

using

1using System.Collections.Generic; 2using UnityEngine; 3 4[CreateAssetMenu(menuName = "ScriptableObjects/Create_ThemesData")] 5public class ThemesTwoData : ThemesRankData 6{ 7 8 [SerializeField, ReadOnly] 9 private string m_themeName; 10 11 [SerializeField, ReadOnly] 12 private ThemesRank m_themeRank; 13 14 [SerializeField, ReadOnly] 15 private int m_lineCount; 16 17 [SerializeField, ReadOnly] 18 private int m_pieceCount; 19 20 public string themeName { get { return m_themeName; } set { m_themeName = value; } } 21 public ThemesRank themeRank { get { return m_themeRank; } set { m_themeRank = value; } } 22 public int lineCount { get { return m_lineCount;} set { m_lineCount = value; } } 23 public int pieceCount { get { return m_pieceCount; } set { m_pieceCount = value; } } 24} 25選択したいアセットの元スクリプト2

using

1using System.Collections.Generic; 2using UnityEngine; 3 4public class ThemesRankData : ScriptableObject 5{ 6 public enum ThemesRank 7 { 8 A=100, 9 B=80, 10 C=50, 11 D=25, 12 E=10, 13 } 14 15 public enum ThemesType 16 { 17 None = 0, 18 Red , 19 Green, 20 Blue, 21 Yellow, 22 Purple, 23 } 24 25 //まだ手を付けない優先順位低い 26 public enum LineType 27 { 28 Normal, 29 Cross, 30 } 31} 32元スクリプトに含まれるデータ類

using

1using System.Collections.Generic; 2using UnityEngine; 3 4public class ThemeSelector : MonoBehaviour 5{ 6 private const string PATH = "Assets/Resources/"; 7 private static ThemesOneData themesOneData; 8 private static ThemesTwoData themesTwoData; 9 private void Start() 10 { 11 themesOneData = Resources.Load<ThemesOneData>(PATH);//ここのPATHを変更するのでしょうか。 12 themesTwoData = Resources.Load<ThemesTwoData>(PATH);//ここのPATHを変更するのでしょうか。 13 print(themesOneData); 14 print(themesTwoData); 15 } 16} 17アセットを選ぶ処理、本題の部分

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

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

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

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

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

guest

回答2

0

using

1using System.Collections.Generic; 2using UnityEngine; 3 4public class ThemeSelector : MonoBehaviour 5{ 6 private static Object[] themesOneData; 7 private static Object[] themesTwoData; 8 private static Object themeOneData; 9 private static Object themeTwoData; 10 private void Start() 11 { 12 themesOneData = Resources.LoadAll("",typeof(ThemesOneData)); 13 themesTwoData = Resources.LoadAll("", typeof(ThemesTwoData)); 14 15 themeOneData = (ThemesOneData)themesOneData[Random.Range(0, themesOneData.Length - 1)]; 16 themeTwoData = (ThemesTwoData)themesTwoData[Random.Range(0, themesTwoData.Length - 1)]; 17 18 print(themesOneData.Length); 19 print(themeTwoData); 20 } 21} 22編集したスクリプト

作成したアセットを配列ですべて受け取り、ランダムで選択することで解決しました。あとはメソッド化とアンロードを追加するだけです。ありがとうございます。

投稿2020/06/01 07:49

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

ベストアンサー

Unityで自分で作ったアセットをResourcesフォルダからランダムに選択したいのですが可能でしょうか。

私の考えではクラス名が同じであればファイル名が違っても読み込めるだろうと考えています。

Resources.Loadは「Resourcesフォルダから指定した名前のファイルを読み込む」という処理なので、ファイル名が違ったら当然読み込めません。
(というか、そもそもファイル名を含んだパスを指定してあげる必要があるので、この時点で間違っています)

試してはいませんが、Resources.FindObjectsOfTypeAllとかResources.LoadAllとかなら、いけるかもしれません。

投稿2020/06/01 05:53

fiveHundred

総合スコア9796

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

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

退会済みユーザー

退会済みユーザー

2020/06/01 05:57

ファイル名まで指定だったのですね.... LoadAllなどを使う場合リストに格納するとしてリストからランダムに選ぶことは可能でしょうか。
fiveHundred

2020/06/01 06:04

可能といえば可能です。 ただ、必要のないものまでロードしてしまうので、気になるのであれば個別にロードしたほうがいいのかもしれません。
退会済みユーザー

退会済みユーザー

2020/06/01 06:42 編集

何度もすいません。 個別ロードの場合はどのように対処できますか。 それと themesOneData = Resources.LoadAll<ThemesOneData>(PATH); themesTwoData = Resources.LoadAll<ThemesTwoData>(PATH); themeOneData = themesOneData[Random.Range(0, themesOneData.Length-1)]; themeTwoData = themesTwoData[Random.Range(0, themesTwoData.Length-1)]; print(themeOneData); print(themeTwoData); のように編集してみたのですが配列に登録されません。
fiveHundred

2020/06/01 06:47 編集

> 個別ロードの場合はどのように対処できますか。 今回のように、数値だけ違うのであれば、「"TestData" + num」(numは数値)みたいな感じで文字列として連結させればよろしいかと思います。
fiveHundred

2020/06/01 06:48

というか「"Assets/Resources/"」の部分は不要です。
退会済みユーザー

退会済みユーザー

2020/06/01 07:12 編集

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ThemeSelector : MonoBehaviour { private static Object[] themesOneData; private static Object[] themesTwoData; private static Object themeOneData; private static Object themeTwoData; private void Start() { themesOneData = Resources.LoadAll("",typeof(ThemesOneData)); themesTwoData = Resources.LoadAll("", typeof(ThemesTwoData)); themeOneData = (ThemesOneData)themesOneData[Random.Range(0, themesOneData.Length - 1)]; themeTwoData = (ThemesTwoData)themesTwoData[Random.Range(0, themesTwoData.Length - 1)]; print(themesOneData.Length); print(themeTwoData); } } objectで受け取りtypeofで指定するという記事を見たので上記のように編集し, パスを空白にすることで解決できたのですが、パスなしの場合の書き方はこれでいいのでしょうか。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問