Unity:インスペクターのスクリプトに画像を表示

最終更新日



はじめに

以下の画像を見てもらうとわかりますが、インスペクターの特定のスクリプトに画像が配置できます。

すごいですね、最終的な成果物にはまったく関係ないですが Asset Store などでライブラリを公開する場合などにこういったスクリプトを含めるとすごくできる感が出ますのでおすすめです!

Unityを知らない方は、ぜひ こちらの記事 をご参照ください。

便利機能一覧

以下に、他にもある便利機能のリンクを載せますのでぜひご参照ください。

使い方

Editor スクリプトを用意

Editorフォルダを作成して、以下のスクリプトと画像を配置します。

using System;

using UnityEngine;
using UnityEditor;

namespace DigitalRuby.Earth
{
    [CustomEditor(typeof(TestScript))]
    public class LogoView : Editor
    {
        private Texture2D logo;

        public override void OnInspectorGUI()
        {
            if (logo == null)
            {
                string[] guids = AssetDatabase.FindAssets("LogoView");
                foreach (string guid in guids)
                {
                    string path = AssetDatabase.GUIDToAssetPath(guid);
                    logo = AssetDatabase.LoadMainAssetAtPath(path) as Texture2D;
                    if (logo != null)
                    {
                        break;
                    }
                }
            }
            if (logo != null)
            {
                const float maxLogoWidth = 430.0f;
                EditorGUILayout.Separator();
                float w = EditorGUIUtility.currentViewWidth;
                Rect r = new Rect();
                r.width = Math.Min(w - 40.0f, maxLogoWidth);
                r.height = r.width / 4f;
                Rect r2 = GUILayoutUtility.GetRect(r.width, r.height);
                r.x = ((EditorGUIUtility.currentViewWidth - r.width) * 0.5f) - 4.0f;
                r.y = r2.y;
                GUI.DrawTexture(r, logo, ScaleMode.StretchToFill);
                EditorGUILayout.Separator();
            }

            DrawDefaultInspector();
        }
    }
}

対象スクリプトを用意

ロゴを表示するための空のスクリプトを用意します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour {

}

使ってみる

適当なゲームオブジェクトを作成して、TestScript をアタッチすると、見事にロゴが表示されるようになりました、すごいですね!!

番外:クリックアクションを取得する

実はロゴをクリックしたときのイベントを追加できます。

以下のように追加すると、Googleをブラウザで開きます。

GUI.DrawTexture(r, logo, ScaleMode.StretchToFill);

          ↓

GUI.DrawTexture(r, logo, ScaleMode.StretchToFill);
if (GUI.Button(r, "", new GUIStyle()))
{
     Application.OpenURL("https://www.google.com/");
}

関連シリーズ







よければ、SNSにシェアをお願いします!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

コメントする