LoginSignup
8
5

More than 5 years have passed since last update.

Resources.Loadを列挙型(Enum)で行えるエディタ拡張

Last updated at Posted at 2017-03-21

機能

パス取得機能

Resourcesフォルダ内にあるアセットのパスをEnumで指定して取得できます。
全てのアセットからの取得と「Texture」といったタイプごとの取得があります。

リソース読み込み機能

Resourcesフォルダ内にあるアセットをEnumで指定して読み込めます。
全てのアセットからの読み込みと「Texture」といったタイプ毎の読み込みがあります。

使い方

スクリプトを使用したいUnityプロジェクトへ

本体:https://github.com/Tanakancolle/DogProject
依存:https://github.com/Tanakancolle/Core
の二つをUnityプロジェクトのAssets内に置いてください。
Gitを使用しているならサブモジュールとして持ってくると楽です。
キャプチャ.PNG

ウィンドウを開く

メニューバーにある「Tools/Resources Loader」をクリックするとウィンドウが開きます。
キャプチャ.PNG

生成するパスを指定

ウィンドウにある「生成パス」にスクリプトを生成したいパスを入れてください。

生成開始

ウィンドウにある「OK」ボタンをクリックするとスクリプトが生成されます。

↓Resources内のフォルダ構成
キャプチャ2.PNG

↓生成されたスクリプト

ResourcesLoader.cs
using UnityEngine;

public static class ResourcesLoader
{
#region Resources
    public enum eResources
    {
        TestTexture
    }

    private static readonly string[] ResourcesPaths = new string[]
    {
        "test/TestTexture"
    };

    public static string GetResourcesName(eResources type)
    {
        return ResourcesPaths[(int)type];
    }

    public static T LoadResources<T>(eResources type) where T : Object
    {
        return Resources.Load<T> (GetResourcesName (type));
    }

#endregion

#region Texture
    public enum eTexture
    {
        TestTexture
    }

    private static readonly string[] TexturePaths = new string[]
    {
        "test/TestTexture"
    };

    public static string GetTextureName(eTexture type)
    {
        return TexturePaths[(int)type];
    }

    public static Texture LoadTexture(eTexture type)
    {
        return Resources.Load<Texture> (GetTextureName (type));
    }

#endregion

#region Sound
    public enum eSound
    {
        dummy
    }

    private static readonly string[] SoundPaths = new string[]
    {
        "dummy"
    };

    public static string GetSoundName(eSound type)
    {
        return SoundPaths[(int)type];
    }

    public static AudioClip LoadSound(eSound type)
    {
        return Resources.Load<AudioClip> (GetSoundName (type));
    }

#endregion

}

※対象がない場合は「dummy」が使用されます
※Enum名はアセットの名前から拡張子を省いたものが使用されます

生成されたスクリプトからアセットの読み込み

全アセット共通の読み込み方法

アセットは↓のように読み込みができます。「TestTexture.png」を読み込んでいます。
var texture = ResourcesLoader.LoadResources<Texture> (ResourcesLoader.eResources.TestTexture);

Textureのみの読み込み方法

タイプごとの読み込みもあります。こちらも「TestTexture.png」を読み込んでいます。
var texture = ResourcesLoader.LoadTexture (ResourcesLoader.eTexture.TestTexture);

タイプごとの読み込みに関してはTextureの他にはAudioClipがあります。

拡張

「Texture」や「AudioClip」のようなタイプごとの読み込み対象を増やす方法です。

大まかな動作説明

まずは大まかにどのようにこの機能が動作しているか説明します。

クラス図

dogclass.png
この機能の大まかなクラス図です。
ここで注目してほしいのは「ILoaderParameter」と「ILoaderEditor」です。

ILoaderParameterについて

このインターフェースは情報を提供するインターフェースです。
このインターフェースを継承したクラスがResourcesフォルダ内の対象とするアセットの指定等を行います。
このインターフェスが提供するのは、
・対象全体の名前
・対象の拡張子
・対象のタイプ名
の3つになります。

ILoaderEditorについて

このインターフェースは生成するスクリプトの内容を記述するインターフェースです。
このインターフェースを継承したクラスが「ILoaderParameter」から受け取った情報を元にスクリプトの内容を記述します。

タイプごとの読み込み対象を増やす

ILoaderParameterを継承したクラスを作成

ILoaderParameterを継承したクラスを作成します。
例として「TextAsset」を対象とした読み込みを増やします。
「TestAssetLoaderParameter」クラスを作成します。

TestAssetLoaderParameter.cs
namespace EditorCreate
{
    public class TextAssetLoaderParameter : ILoaderParameter
    {
        public string GetName()
        {
            return "TextAsset";
        }

        public string[] GetTargetExtensions()
        {
            return new string[] {
                "txt","html","htm","xml","bytes","json","csv","yaml","fnt"
            };
        }

        public string GetTypeName()
        {
            return GetName ();
        }

        public string[] GetUsings()
        {
            return new string[] { "UnityEngine" };
        }
    }
}

クラス図で表すと↓になります。
dogclass2.png 

ResourcesLoaderCreaterクラスに作成したクラスを追加

「ResourcesLoaderCreater」クラスの「parameters」に作成したクラスを追加します。
private ILoaderParameter[] parameters = new ILoaderParameter[] {
    new AllLoaderParameter(),
    new TextureLoaderParameter(),
    new AudioClipLoaderParameter(),
    new TextAssetLoaderParameter(), // 追加
};

以上で自動生成されたスクリプトにTextAssetを対象としたメソッドが作成されます。

・TextAssetのパス取得
var path = ResourcesLoader.GetTextAssetName (ResourcesLoader.eTextAsset.TestText);

・TextAssetの読み込み
var text_asset = ResourcesLoader.LoadTextAsset (ResourcesLoader.eTextAsset.TestText);

注意点

エラーとなってしまうパターンがあります。
※スクリプトは生成されますが、エラーがでます

拡張子を省いたファイル名が同名になってしまう場合

例えばResourcesフォルダ内に「Test.png」と「Test.mp3」というファイルがあった場合、
enum宣言時に使用される名前はファイル名から拡張子を省いたものなので、同名となってしまい、エラーになります。
全アセットからの読み込みが出来なくなりますが、
ResourcesLoaderCreaterクラスのparametersからAllLoaderParameterを削除することでエラーが出なくなります。

変換されたenum名が同名になってしまう場合

ファイル名にenumで使用できない文字が含まれていた場合、「_」に置き換えるようになっています。
置き換え後同名になってしまうとエラーになります。

例)下記の場合、同名となってしまいエラー
ファイル名:1Test.png → _Test
ファイル名:2Test.png → _Test

Git

この機能のGitです。
本体:https://github.com/Tanakancolle/DogProject
依存:https://github.com/Tanakancolle/Core

8
5
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
8
5