Unity Editor Tips

Published by

on

EditorGUILayout.BeginHorizontal vs GUILayout.BeginHorizontal

EditorGUILayout version will return a Rect which can be used to do math

Getting Assets of a specific type

var vars = AssetDatabase.FindAssets("t:GameEventArgs")
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadAssetAtPath)
.Where(b => b).OrderBy(v => v.name).ToArray();

Use a base class

public abstract class BaseWindow : EditorWindow
{
}

public class NodeWindow : BaseWindow
{
}

Use what you know

class MyWindow : EditorWindow
{
static void Init()
{
var w = GetWindow();//sometimes...
w.Show();
//just use this for clarity
var w = ScriptableObject.CreateInstance();
w.Show();
}
}

Write C# code

When programming with a MonoBehaviour a common paradigm is to declare public fields for debugging purposes. When programming for the Editor use private when a field should not be accessed and public when someone does need access. It is also distracting when a non supported serialized type is assumed to show up in the inspector and I spend 30 minutes trying to make it show up.

Making buttons

GUI.Button

ButtonUse?Useful?
GUI.Buttonnot lazykeeping track of rect for controlid
GUILayout.Buttonlazyauto layout
EditorGUI.Buttonnot lazyeditable fields, keeping track of rect for control id
EditorGUILayout.Buttonlazyeditable fields, autolayout

if you have the rect then you can ensure the correct unique id for the control

GUIUtility.GetRect

Drawing things manually

Handles.Draw

SerializedObject and SerializedProperty

Allows the user to assign an Object reference to a field.

UnityEngine.Object obj;
EditorGUILayout.ObjectField(obj);

How to display useful information about obj

  1. Convert object to SerializedObject
var so = new SerializedObject(obj)
  1. Get the property you want to show from the created SerializedObject
var sp = so.FindProperty("fieldname")

Leave a comment