本日はUnity枠です。
今回はUnityのEditor拡張機能を使用して作成可能なEditorWindowに表示されるUIでUnityのインスペクターウィンドウなどと同様にマテリアルを表示させていきます。
〇EditorWindowの作成
EditorWindowを表示させる最小コードは次のようになります。
using UnityEngine; using UnityEditor; public class EditorWindowMaterialTest : EditorWindow { [MenuItem("Window/OpenWindow")] static void OpenWindow() { GetWindow<EditorWindowMaterialTest>("Material View"); } private void OnGUI() { } }
これによって上部ワールドメニューWindowにOpenWindowが表示されます。
表示されるウィンドウのUIはOnGUI()メソッドにて定義され、現在は何も記述していないため何も表示されません。
今回はこの表示にinspectorウィンドウなどと同様にマテリアルをオブジェクトとして表示する実装を紹介します。
〇ObjectField
マテリアルやゲームオブジェクトなどUnityで定義済みの形式のファイルを表示するためにはEditorGUILayout.ObjectField() を使用します。
次のように記述することでマテリアルをオブジェクトの形で表示されます。
using UnityEngine; using UnityEditor; public class EditorWindowMaterialTest : EditorWindow { [MenuItem("Window/OpenWindow")] static void OpenWindow() { GetWindow<EditorWindowMaterialTest>("Material View"); } private Material material; private void OnGUI() { EditorGUILayout.ObjectField(material, typeof(Material), false); } }
EditorGUILayout.ObjectFieldはUnityEditorで提供されているメソッドで次のように3つの引数を与えて使用します。
EditorGUILayout.ObjectField("オブジェクト", "タイプ", bool "(シーン内のオブジェクトを選択可能か?)")
今回はオブジェクトのタイプとして"Material"を選択しました。
本日は以上です