Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SetCustomSelection

Records a custom selection in the Editor's selection history.
Read time 6 minutesLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule

SetCustomSelection(string, string)

Records a custom selection in the Editor's selection history.
public static void SetCustomSelection(string key, string data)

Parameters

A unique string that identifies which selection handler to call.

data

The data that represents selection.

Remarks

The Editor's selection history tracks regular Object-based selection. In the Editor, you can use the Previous Selection and Next Selection buttons in the toolbar to cycle through selection history. If you have an Editor window or an Editor tool that has an internal concept of selection and you want that to participate in the selection history, you can use Selection.RegisterCustomHandler and
SetCustomSelection
.
If you want to record your custom selection changes, call
SetCustomSelection
. Encode your selection data into the
data
string (for example, by serializing the selection into JSON through EditorJsonUtility) and pass any objects you would like to select. This records a selection entry in the global selection history. When the user navigates back to that entry, your custom handler is called to apply your selection, and selection history sets the recorded Object as selected.
Additional Resources: Selection.RegisterCustomHandler.

Examples

using UnityEngine;using UnityEditor;// An Editor window that is just a toolbar and displays these options: One, Two, and Three.// Changing the current option records a selection history entry, so// that the Previous Selection and Next Selection buttons work on this toolbar selection.public class CustomSelection : EditorWindow{ const string kSelectionKey = "CustomSelectionExample"; string[] labels = { "One", "Two", "Three" }; int selected = 0; [MenuItem("Window/Custom Selection Window")] static void Init() { var window = EditorWindow.GetWindow<CustomSelection>(); // Record custom selection handler function, to // apply selection to our toolbar whenever going back/forward // to the custom selection entry. Selection.RegisterCustomHandler(kSelectionKey, ApplySelection); window.Show(); } static void ApplySelection(string data, EntityId[] selectedEntityIDs) { // Selection History will set the correct window as focused if (focusedWindow is CustomSelection window) { // Data string is simply an integer here, for which // toolbar option is selected. int.TryParse(data, out var selected); window.selected = selected; } } void OnGUI() { EditorGUI.BeginChangeCheck(); selected = GUILayout.Toolbar(selected, labels); if (EditorGUI.EndChangeCheck()) { // When the toolbar option has changed, record a custom // selection entry. Entry data is simply the selected index as a string. // Passing a selection of EntityId.None will deselect the previously selected Object Selection.SetCustomSelection(kSelectionKey, selected.ToString(), EntityId.None); } }}

SetCustomSelection(string, string, EntityId)

Records a custom selection in the Editor's selection history.
public static void SetCustomSelection(string key, string data, EntityId selectedEntityId)

Parameters

A unique string that identifies which selection handler to call.

data

The data that represents selection.

selectedEntityId

The object to select as EntityID (optional).

Remarks

The Editor's selection history tracks regular Object-based selection. In the Editor, you can use the Previous Selection and Next Selection buttons in the toolbar to cycle through selection history. If you have an Editor window or an Editor tool that has an internal concept of selection and you want that to participate in the selection history, you can use Selection.RegisterCustomHandler and
SetCustomSelection
.
If you want to record your custom selection changes, call
SetCustomSelection
. Encode your selection data into the
data
string (for example, by serializing the selection into JSON through EditorJsonUtility) and pass any objects you would like to select. This records a selection entry in the global selection history. When the user navigates back to that entry, your custom handler is called to apply your selection, and selection history sets the recorded Object as selected.
Additional Resources: Selection.RegisterCustomHandler.

Examples

using UnityEngine;using UnityEditor;// An Editor window that is just a toolbar and displays these options: One, Two, and Three.// Changing the current option records a selection history entry, so// that the Previous Selection and Next Selection buttons work on this toolbar selection.public class CustomSelection : EditorWindow{ const string kSelectionKey = "CustomSelectionExample"; string[] labels = { "One", "Two", "Three" }; int selected = 0; [MenuItem("Window/Custom Selection Window")] static void Init() { var window = EditorWindow.GetWindow<CustomSelection>(); // Record custom selection handler function, to // apply selection to our toolbar whenever going back/forward // to the custom selection entry. Selection.RegisterCustomHandler(kSelectionKey, ApplySelection); window.Show(); } static void ApplySelection(string data, EntityId[] selectedEntityIDs) { // Selection History will set the correct window as focused if (focusedWindow is CustomSelection window) { // Data string is simply an integer here, for which // toolbar option is selected. int.TryParse(data, out var selected); window.selected = selected; } } void OnGUI() { EditorGUI.BeginChangeCheck(); selected = GUILayout.Toolbar(selected, labels); if (EditorGUI.EndChangeCheck()) { // When the toolbar option has changed, record a custom // selection entry. Entry data is simply the selected index as a string. // Passing a selection of EntityId.None will deselect the previously selected Object Selection.SetCustomSelection(kSelectionKey, selected.ToString(), EntityId.None); } }}

SetCustomSelection(string, string, ReadOnlySpan<EntityId>)

Records a custom selection in the Editor's selection history.
public static void SetCustomSelection(string key, string data, ReadOnlySpan<EntityId> selectedEntityIds)

Parameters

A unique string that identifies which selection handler to call.

data

The data that represents selection.

selectedEntityIds

The objects to select as EntityIDs (optional).

Remarks

The Editor's selection history tracks regular Object-based selection. In the Editor, you can use the Previous Selection and Next Selection buttons in the toolbar to cycle through selection history. If you have an Editor window or an Editor tool that has an internal concept of selection and you want that to participate in the selection history, you can use Selection.RegisterCustomHandler and
SetCustomSelection
.
If you want to record your custom selection changes, call
SetCustomSelection
. Encode your selection data into the
data
string (for example, by serializing the selection into JSON through EditorJsonUtility) and pass any objects you would like to select. This records a selection entry in the global selection history. When the user navigates back to that entry, your custom handler is called to apply your selection, and selection history sets the recorded Object as selected.
Additional Resources: Selection.RegisterCustomHandler.

Examples

using UnityEngine;using UnityEditor;// An Editor window that is just a toolbar and displays these options: One, Two, and Three.// Changing the current option records a selection history entry, so// that the Previous Selection and Next Selection buttons work on this toolbar selection.public class CustomSelection : EditorWindow{ const string kSelectionKey = "CustomSelectionExample"; string[] labels = { "One", "Two", "Three" }; int selected = 0; [MenuItem("Window/Custom Selection Window")] static void Init() { var window = EditorWindow.GetWindow<CustomSelection>(); // Record custom selection handler function, to // apply selection to our toolbar whenever going back/forward // to the custom selection entry. Selection.RegisterCustomHandler(kSelectionKey, ApplySelection); window.Show(); } static void ApplySelection(string data, EntityId[] selectedEntityIDs) { // Selection History will set the correct window as focused if (focusedWindow is CustomSelection window) { // Data string is simply an integer here, for which // toolbar option is selected. int.TryParse(data, out var selected); window.selected = selected; } } void OnGUI() { EditorGUI.BeginChangeCheck(); selected = GUILayout.Toolbar(selected, labels); if (EditorGUI.EndChangeCheck()) { // When the toolbar option has changed, record a custom // selection entry. Entry data is simply the selected index as a string. // Passing a selection of EntityId.None will deselect the previously selected Object Selection.SetCustomSelection(kSelectionKey, selected.ToString(), EntityId.None); } }}