Extending Unity Editor - lochrist/DocHoliday GitHub Wiki
Based on Damian Campeanu Ligthning Talk
-
ScriptableObject
class MyObject : ScriptableObject{ }
-
ScriptableObject.CreateInstance<MyObject>()
and.DestroyImmediate()
- Mark fields [
SerializeField
], mark classes [Serializable
] -
ISerializationCallbackReceiver
hook into serialization system -
HideFlags
.DontUnloadUnusedAsset and .DontSaveInEditor - Change Detection and Reflection
-
Undo/Redo
-
Undo.RegisterCompleteObjectUndo
(obj) to record state prior to change -
Undo.ClearUndo
(obj) to clear all saved states -
Undo.undoRedoPerformed
to detect undo/redo
-
- At Runtime
-
JsonUtility.ToJson
(obj) andJsonUtility.FromJsonOverwrite
(json, obj)
-
- Make sure C# filename and class names are the same
- Custom Editor Window
- Inherit from
EditorWindow
- Add to main menu via [
MenuItem
("Windows/MyWin")]static void Func()
- Different window types with:
Show()
,ShowPopup()
,ShowUtility()
,ShowAsDropDown()
- Implement
OnEnable()
and add UI elements to rootVisualElement OnDisable()
- Inherit from
- Custom Inspector
- Inherit from Editor w/ [
CustomEditor
(typeof(MyScriptableObject))] (see Editor for more details) - Override
CreateInspectorGUI()
to create custom UI - Bind to
Editor.serializedObject
- Implement
Editor.OnSceneGUI()
to add gizmos to the scene
- Inherit from Editor w/ [
- Custom Property Drawer (for specific type)
- Inherit from
PropertyDrawer
-> [CustomPropertyDrawer(typeof(MyType))
] - Override
CreatePropertyGUI
(prop)]
- Inherit from
- Create/Save/Load Assets (ScriptableObjects)
[CreateAssetMenu(menuName = "Category/MyAsset")]
class MyAsset : ScriptableObject {}
- AssetDatabase.
CreateAsset
(obj, path) and .SaveAssets()
AssetDatabase.LoadAssetFromPath<MyAsset>("Assets/Folder/file.ext")
- Detect Asset Changes
- Inherit from
AssetModificationProcessor
- Implement
OnWillCreateAsset()
,OnWillSaveAssets()
, ...
- Inherit from
- Custom Asset Importing
- Inherit from
ScriptedImporter
w/[ScriptedImporter(version, "ext")]
- Override
OnImportAsset()
- Inherit from
- Open Custom Asset with Custom Tool
[OnOpenAsset(0)] static bool OnOpenAsset(int instanceID, int line)
- Shortcut Manager Integration
-
[Shortcut]
("Category/Function") attribute on any static method
-
- Settings Window
- Inherit from
SettingsProvider
, implementOnActivate()
/OnDeactivate()
-
[SettingsProvider]
attribute on static method, return yourSettingsProvider
- Inherit from
- Editor Tools
- Inherit from
EditorTool
w/ [EditorTool("name")] attribute - Override
EditorTool.OnToolGUI
(EditorWindow wnd)
- Inherit from
- GameObject Operations in the Editor
-
ObjectFactory
.CreateGameObject
() to create with undo support and defaults -
ObjectFactory.AddComponent
() to add component -
gameObject.transform.SetParent
(myParent) to re-parent -
GameObject.DestroyImmediate
() to destroy
-
-
Standard Unity Window Access
- EditorWindow.
GetWindow<SceneView>()
ProjectWindowUtil.GetContainingFolder(path)
- EditorWindow.
-
Cameras
-
Utilities
-
EditorApplication.isPlayingOrWillChangePlaymode
EditorApplication.playModeStateChanged
EditorApplication.applicationPath
EditorApplication.projectChanged
EditorApplication.update
-
Domain Reload