2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Unity】Inspectorウィンドウから実行するスクリプト(カスタムエディタ)

Posted at

Unityの開発補助ツールを作りたくて、
Inspectorウィンドウのクリックをトリガーに何かするものを作りました。

作ったもの

Inspectorで入力してOK押すと、コンソールに入力した文字が表示されます。

Image from Gyazo

方法

スクリプトを作成

ファイル名は大文字が良さそう。

Hoge.cs

using UnityEngine;

public class Hoge : MonoBehaviour
{
    public string text; // Inspectorでユーザーが入力するところ

    public void onClick()
    {
        Debug.Log(text);
    }
}

Editorフォルダを作成し、
そこに以下のスクリプト(エディタースクリプトというらしい)を作成します。

hogeEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Hoge))]
public class hogeEditor : Editor
{
    public override void OnInspectorGUI()
    {
        Hoge hoge = (Hoge)target;

        // インターフェースの描画
        DrawDefaultInspector(); // 既存のインスペクターのフィールドを描画

        // OKボタン
        if (GUILayout.Button("OK"))
        {
            hoge.onClick(); // HogeのOnClickメソッドを呼び出す
        }
    }
}

 以上です。

2
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?