LoginSignup
3
1

More than 5 years have passed since last update.

Unity ~Assetからフォルダごとデータを読み込み~

Last updated at Posted at 2017-03-11

使いみち

コレクションリスト作って画像読み込ませるときとか?

実装

Assetの階層作成
Asset/Resources/hogehoge
/hogehogeの中に画像データが入っているイメージを想定
/hogehoge/image1
みたいな

今回はUI>Panelから画像を表示させます。

コード

C#

Sprite[] image;

image = Resources.LoadAll<Sprite>("hogehoge");

panel = GameObject.Find("Panel").GetComponent<Image>();

//こんな感じで呼び出し
panel.sprite = image[0];

image1とかで番号管理している場合はこんなので擬似的にイメージ名から参照も可能


image[int.Parse(imageName.Substring(5)) - 1];

処理的にはimageName="image1"だったら6文字目以降を参照し、数値化する
結構無理やり出しバグあったらごめんね★

-1してるのは配列が0スタートで番号を1スタートシてたらそれを補正するためです。

ファイル総数


image.Length

でファイル総数がわかります。

コレクションリストとかを動的に作ってるなら参照エラーが怒らないように総数と合わせとくと良いんじゃないかなと

※追記

名前参照出来ました()

poseImage.sprite = Resources.Load("PoseImage/" + pose, typeof(Sprite)) as Sprite;

ガチャ的に

箱用意して
Sprite[] normalPose;

変数用意して
int normalPoseNum;

フォルダの中身箱に入れて
normalPose = Resources.LoadAll("PoseModel/Normal");

箱の中身の総数読み出して
normalPoseNum = normalPose.Length;

総数の中からランダムで一つ取り出して名前を参照
setPoseName(normalPose[Random.Range(0, normalPoseNum)].name);

例えば箱を2つ用意しとけばどちらの箱を選ぶかの2択
その後中身抽選って感じの簡易的ガチャが作れるね★

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1