LoginSignup
11
10

More than 5 years have passed since last update.

[Unity] Resources配下のファイル名を定数クラスにするエディタ拡張

Posted at

ソース

GenerateResourceNames.cs

これは何?

Assets/Resources配下がこんな感じのファイル構成だったら、

image

こんな感じの定数クラスを生成するので、

Scripts/ResourceNames.cs
public static class ResourceNames {
    public static class Prefabs {
        public const string Cube = "Prefabs/Cube";
        public const string Sphere = "Prefabs/Sphere";
    }
    public static class Sprites {
        public const string Background = "Sprites/Background";
    }
}

こんな感じで Resources.Load できる。

LoadSample
GameObject cubePrefab = Resources.Load<GameObject> (ResourceNames.Prefabs.Cube);

使い方

Assets/EditorにGenerateResourceNames.csを置くと、
Unityエディタの メニュー -> Edit に、Generate ResourceNames.cs という
メニューが出てくるので、これを実行すれば Scripts/ResourceNames.cs が生成される。

image

なお、Prefabs/Resources みたいな構成には未対応。Assets直下のResourcesだけ見る。

11
10
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
11
10