Featured image of post Unity Editor Scripting Cheat Sheet

Unity Editor Scripting Cheat Sheet

Asset Management

Asset: An object imported from project file. One project file can be imported into many assets, like a png tileset image can be imported into many sprite assets.

  • AssetDatabase CRUD for project assets
    • Operates on project relative paths and asset GUIDs
  • Resources Allows you to find and access Objects including assets
    • Can load assets without project relative paths
  • AssetPostprocessor & AssetModificationProcessor Take action after asset import
  • PrefabUtility Inspect / Modify Prefabs
    • Instantiate prefabs in the scene as “prefab instances” and not regular game objects
    • Create prefab variants
    • Add components
    • Apply overrides
    • Supports Undo out of the box
  • UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget Loads assets from outside of the project folder
  • Special Folder Names

Entry points

UI

Entry points

  • MenuItem Create a menu item
  • ContextMenu Create object specific context menu items. Works on MonoBehaviour and ScriptableObject classes. Right click on object inspector header to show menu
  • ContextMenuItemAttribute Same as above but works on serialized fields
  • IHasCustomMenu Defines a method to add custom menu items to an Editor Window
  • EditorApplication.contextualPropertyMenu Callback raised whenever the user contex-clicks on a property in an Inspector
  • UnityEditor.SceneManagement.SceneHierarchyHooks Add menu items to Hierarchy window
    • addItemsToCreateMenu
    • addItemsToGameObjectContextMenu
    • addItemsToSceneHeaderContextMenu
    • addItemsToSubSceneHeaderContextMenu

Editors

Windows

Addons

UI Systems

Scene View UI

Utility

Serialization

Native Popups

Icons

Certain functions that load or set editor icons will find the appropriate texture for the active editor theme if certain conditions are met

  • Icon textures for light and dark themes are in the same directory
  • Dark theme icon name starts with the prefix: “d_”
  • The icon name, path or texture passed to the function was pointing to the light version of the image

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Assuming the editor theme is set to dark
// and there are two icons located at Assets/myIcon.png and Assets/d_myIcon.png

// This will return the Assets/d_myIcon.png because Editor theme is set to dark
var darkIcon = EditorGUIUtility.IconContent("Assets/myIcon.png").image;

// This will return the Assets/myIcon.png because AssetDatabase.LoadAssetAtPath always returns the exact asset
var icon = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/myIcon.png");

var monoImporter = (MonoImporter)AssetImporter.GetAtPath("Assets/SomeScript.cs");
// This will set the icon to Assets/d_myIcon.png because Editor theme is set to dark
monoImporter.SetIcon(icon);

Advanced / Tricks

Misc

Resources

Useful Internals

Those are internal classes/methods that I found and thought might be useful. As with any internal stuff your need to keep in mind that this functionality is subject to change.

  • ScriptAttributeUtility.GetFieldInfoFromProperty
  • ScriptAttributeUtility.GetDrawerTypeForType
  • You can create an assembly with a name like “Unity.InternalAPIEditorBridge.” that has access to the editor’s internal API. To find possible indexes search editor source code

Obscure

Built with Hugo
Theme Stack designed by Jimmy