Search Unity

EditorWindow.OnSelectionChange() is missing a C# example

Discussion in 'Documentation' started by Shredsauce, Jul 25, 2016.

  1. Shredsauce

    Shredsauce

    Joined:
    Apr 4, 2016
    Posts:
    23
    https://docs.unity3d.com/ScriptReference/EditorWindow.OnSelectionChange.html

    I've translated it here

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. // Simple example that lets you save the current selection and load it.
    6.  
    7. public class SelectionChange : EditorWindow {
    8.    
    9.     private int[] selectionIDs;
    10.  
    11.     [MenuItem ("Example/Selection Saver")]
    12.  
    13.     private static void Init() {
    14.         SelectionChange window = (SelectionChange)GetWindow(typeof(SelectionChange));
    15.         window.Show();
    16.     }
    17.  
    18.     void OnGUI() {
    19.         if(GUILayout.Button("Save"))
    20.             SaveSelection();
    21.         if(GUILayout.Button("Load"))
    22.             LoadLastSavedSelection();
    23.     }
    24.  
    25.     void OnSelectionChange() {
    26.         selectionIDs = Selection.instanceIDs;
    27.     }
    28.  
    29.     private void SaveSelection() {
    30.         var saveStr = "";
    31.         foreach(int i in selectionIDs) {
    32.             saveStr += i.ToString() + ";";
    33.         }
    34.         saveStr = saveStr.TrimEnd(char.Parse(";"));
    35.         EditorPrefs.SetString("SelectedIDs",saveStr);
    36.     }
    37.  
    38.     private void LoadLastSavedSelection() {
    39.         string[] strIDs = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));
    40.  
    41.         int[] ids = new int[strIDs.Length];
    42.         for(var i = 0; i < strIDs.Length; i++)
    43.             ids[i] = int.Parse(strIDs[i]);
    44.         Selection.instanceIDs = ids;
    45.     }
    46. }
     
  2. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    158
    Good spot. Thanks for the report, and for the translation! We'll fix it.
     
  3. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    158
    Hiya, this has been updated and will go out in the next version of the docs, which should be on the 19th August.

    Thanks again for the report!
     
unityunity