Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorWindow.OnSelectionChange()

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

Description

Called whenever the selection has changed.


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

#pragma strict
// Simple example that lets you save the current selection and load it.
private var selectionIDs: int[];
@MenuItem("Example/Selection Saver")
private static function Init() {
	var window: ExampleClass = ExampleClassGetWindow(ExampleClass);
	window.Show();
}
function OnGUI() {
	if (GUILayout.Button("Save"))
		SaveSelection();
	if (GUILayout.Button("Load"))
		LoadLastSavedSelection();
}
function OnSelectionChange() {
	selectionIDs = Selection.instanceIDs;
}
private function SaveSelection() {
	var saveStr: var = "";
	for (var i: int in selectionIDs) {
		saveStr += i.ToString() + ";";
	}
	saveStr = saveStr.TrimEnd(char.Parse(";"));
	EditorPrefs.SetString("SelectedIDs", saveStr);
}
private function LoadLastSavedSelection() {
	var strIDs: String[] = EditorPrefs.GetString("SelectedIDs").Split(char.Parse(";"));
	var ids: int[] = new int[strIDs.Length];
	for (var i: var = 0; i < strIDs.Length; i++)
		ids[i] = int.Parse(strIDs[i]);
	Selection.instanceIDs = ids;
}
// Simple example that lets you save the current selection and load it.
 
using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class ExampleClass : EditorWindow {

private int[] selectionIDs; [MenuItem ("Example/Selection Saver")] private static void Init() { ExampleClass window = (ExampleClass)GetWindow(typeof(ExampleClass)); window.Show(); } void OnGUI() { if(GUILayout.Button("Save")) SaveSelection(); if(GUILayout.Button("Load")) LoadLastSavedSelection(); } void OnSelectionChange() { selectionIDs = Selection.instanceIDs; } private void SaveSelection() { var saveStr = ""; foreach(int i in selectionIDs) { saveStr += i.ToString() + ";"; } 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++) ids[i] = int.Parse(strIDs[i]); Selection.instanceIDs = ids; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961