コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Inspector でアセットのパス文字列をアセットのドラッグ&ドロップで設定できる「AssetPathAttribute」紹介

はじめに

「AssetPathAttribute」を Unity プロジェクトに導入することで
Inspector でアセットのパス文字列を
アセットのドラッグ&ドロップで設定できるようになります

使い方

using UnityEngine;

public sealed class Example : MonoBehaviour
{
    [AssetPath.Attribute( typeof( Object           ) )] public string objectValue;
    [AssetPath.Attribute( typeof( GameObject       ) )] public string gameObjectValue;
    [AssetPath.Attribute( typeof( AudioClip        ) )] public string audioClipValue;
    [AssetPath.Attribute( typeof( ScriptableObject ) )] public string scriptableObject;
    [AssetPath.Attribute( typeof( Texture2D        ) )] public string texutre2D;
    [AssetPath.Attribute( typeof( Animator         ) )] public string animator;
    [AssetPath.Attribute( typeof( AudioSource      ) )] public string audioSource;
    [AssetPath.Attribute( typeof( Transform        ) )] public string transform;
}

上記のように string 型の変数に AssetPath.Attribute 属性を適用すると

f:id:baba_s:20171224123854p:plain

Inspector でアセットをドラッグ&ドロップして
パス文字列を設定できるようになります

関連記事