Version: 2023.2
언어: 한국어

EditorWindow.OnSelectionChange()

매뉴얼로 전환

설명

Called whenever the selection has changed.


Saves the current selection and load it later with a simple click.

// Simple example that lets you save the current selection and load it.

using UnityEditor;
using UnityEngine.UIElements;

public class OnSelectionChangeExample : EditorWindow
{
    private int[] selectionIDs;

    [MenuItem("Examples/Selection Saver")]
    private static void Init()
    {
        OnSelectionChangeExample window = (OnSelectionChangeExample)GetWindow(typeof(OnSelectionChangeExample));
        window.Show();
    }

    void CreateGUI()
    {
        var buttonSave = new Button();
        buttonSave.text = "Save";
        buttonSave.clicked += () => SaveSelection();
        rootVisualElement.Add(buttonSave);
        
        var buttonLoad = new Button();
        buttonLoad.text = "Load";
        buttonLoad.clicked += () => LoadLastSavedSelection();
        rootVisualElement.Add(buttonLoad);

        // Initialize selectionIDs at least once
        OnSelectionChange();
    }

    void OnSelectionChange()
    {
        selectionIDs = Selection.instanceIDs;
    }

    private void SaveSelection()
    {
        var saveStr = "";

        foreach (int i in selectionIDs)
        {
            saveStr += i + ";";
        }

        saveStr = saveStr.TrimEnd(char.Parse(";"));
        EditorPrefs.SetString("SelectedIDs", saveStr);
    }

    private void LoadLastSavedSelection()
    {
        string[] strIDs = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));

        int[] ids = new int[strIDs.Length];
        for (var i = 0; i < strIDs.Length; i++)
        {
            if (!string.IsNullOrEmpty(strIDs[i]))
                ids[i] = int.Parse(strIDs[i]);
        }

        Selection.instanceIDs = ids;
    }
}