[Unityエディター拡張] エディター拡張の種類

ヨメレバCSS
オリジナルCSS

エディター拡張にはおおまかに次の種類があります。

設定などをするウィンドウを作ります。
Editorディレクトリの下に、CustomWindowというクラスを作成し、次のようにします。
public class CustomWindow : EditorWindow {

	[MenuItem ("Window/CustomWindow")]
	public static void MenuShowWindow() {
		EditorWindow.GetWindow(typeof(CustomWindow));
	}

	void OnGUI() {
		EditorGUILayout.LabelField ("CustomWindow", "Hello");
	}
}

それでメニューバーの「Window→CustomWindow」を選択すると、

1_1_customwindow
というウィンドウが表示されます。
このウィンドウの内容が、OnGUIで定義されているものになっています。
・PropertyDrawer
プロパティとしているクラスのインスペクタをカスタマイズする感じ。
public class PropertyDrawerSample : MonoBehaviour {
	public PropertyDrawerChild child;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

というクラスと

[System.Serializable]
public class PropertyDrawerChild : System.Object {
	public int no = 1;
	public string name = "childname";
}

というクラスを定義して、PropertyDrawerSampleをGameObjectにひっつけてインスペクタで見ると、

1_2_propertydrawer_before

というふうになっています。

ここで、
[CustomPropertyDrawer(typeof(PropertyDrawerChild))]
public class PropertyDrawerChildDrawer : PropertyDrawer {

	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
		EditorGUI.BeginProperty(position, label, property);

		position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);

		var noRect = new Rect (position.x, position.y, 30, position.height);
		EditorGUI.PropertyField (noRect, property.FindPropertyRelative ("no"), GUIContent.none);

		var nameRect = new Rect (position.x+35, position.y, 50, position.height);
		EditorGUI.PropertyField (nameRect, property.FindPropertyRelative ("name"), GUIContent.none);

		EditorGUI.EndProperty ();
	}
}

というクラスをEditorに作成すると、

1_2_propertydrawer_after

こうなります。
・カスタムエディタ

インスペクタの表示を変更していきます。

public class InspectorSample : MonoBehaviour {
	public int no;
	public string name;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

というクラスを作ってGameObjectに追加すると、インスペクタは

1_3_customeditor_before

となります。

ここで、

[CustomEditor(typeof(InspectorSample))]
public class InspectorSampleEditor : Editor {
	public override void OnInspectorGUI() {
		InspectorSample obj = target as InspectorSample;

		obj.no = EditorGUILayout.IntField ("custom no", obj.no);
		obj.name = EditorGUILayout.TextField ("custom name", obj.name);

		if (GUI.changed) {
			EditorUtility.SetDirty (obj);
		}
	}
}

というクラスをEditor以下に定義すると、

1_3_customeditor_after

こうなります。

まずは基本になっていると思われる、カスタムエディタから見ていきたいと思います。
スポンサーリンク
GoogleAdSence レクタングル(大)

シェアする

スポンサーリンク
GoogleAdSence レクタングル(大)