Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

Selection.SetCustomSelection(string,string,Object,Object[])

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Parameters

Parameter Description
key A unique string that identifies which selection handler to call.
data The data that represents selection.
selectedEntityId The object to select as EntityID (optional).
selectedEntityIds The objects to select as EntityIDs (optional).

Description

Records a custom selection in the Editor's selection history.

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.

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); } } }