Skip to main content
added 855 characters in body
Source Link
Daniel Lip
  • 1.8k
  • 4
  • 40
  • 81

UPDATE what I tried:

var selections = Selection.objects.OfType<GameObject>().ToList(); int currentSelectionCount = selections.Count; if (currentSelectionCount != lastSelectionCount) { lastSelectionCount = currentSelectionCount; for (var i = 0; i < currentSelectionCount; ++i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); Selection.objects = new UnityEngine.Object[0]; } 

But when I select object in the editor in hierarchy it's immediately unselecting the object since the line:

Selection.objects = new UnityEngine.Object[0]; 

UPDATE what I tried:

var selections = Selection.objects.OfType<GameObject>().ToList(); int currentSelectionCount = selections.Count; if (currentSelectionCount != lastSelectionCount) { lastSelectionCount = currentSelectionCount; for (var i = 0; i < currentSelectionCount; ++i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); Selection.objects = new UnityEngine.Object[0]; } 

But when I select object in the editor in hierarchy it's immediately unselecting the object since the line:

Selection.objects = new UnityEngine.Object[0]; 
added 634 characters in body
Source Link
Daniel Lip
  • 1.8k
  • 4
  • 40
  • 81

The script is EditorWindow type.

private void OnGUI() { var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); } } 

When I select with the mouse a object in the Editor Hierarchy it will keep making the loop nonstop since selections.Count is large then 0.

I tried to add after the loop:

selections = new List<GameObject>(); 

But it didn't help.

I want that it will make the loop and the whole code inside the if (selections.Count > 0) only once each time when selecting one or more objects in the editor.

If I select one object make the whole code once if I then selected another more object make the whole code over again for the two selected objects and so on but make it once. Now it will keep making the loop over and over again even if I didn't select more other objects.

Since in the editor the first object is still selected it will keep making the code the loop over and over again.

This is what I tried:

var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0 && isSelected == false) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); isSelected = true; } 

The flag boolean isSelected is global variable and set to false as init.

The script is EditorWindow type.

private void OnGUI() { var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); } } 

When I select with the mouse a object in the Editor Hierarchy it will keep making the loop nonstop since selections.Count is large then 0.

I tried to add after the loop:

selections = new List<GameObject>(); 

But it didn't help.

I want that it will make the loop and the whole code inside the if (selections.Count > 0) only once each time when selecting one or more objects in the editor.

If I select one object make the whole code once if I then selected another more object make the whole code over again for the two selected objects and so on but make it once. Now it will keep making the loop over and over again even if I didn't select more other objects.

Since in the editor the first object is still selected it will keep making the code the loop over and over again.

The script is EditorWindow type.

private void OnGUI() { var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); } } 

When I select with the mouse a object in the Editor Hierarchy it will keep making the loop nonstop since selections.Count is large then 0.

I tried to add after the loop:

selections = new List<GameObject>(); 

But it didn't help.

I want that it will make the loop and the whole code inside the if (selections.Count > 0) only once each time when selecting one or more objects in the editor.

If I select one object make the whole code once if I then selected another more object make the whole code over again for the two selected objects and so on but make it once. Now it will keep making the loop over and over again even if I didn't select more other objects.

Since in the editor the first object is still selected it will keep making the code the loop over and over again.

This is what I tried:

var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0 && isSelected == false) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); isSelected = true; } 

The flag boolean isSelected is global variable and set to false as init.

Source Link
Daniel Lip
  • 1.8k
  • 4
  • 40
  • 81

How can I make the loop only once when it's inside OnGUI?

The script is EditorWindow type.

private void OnGUI() { var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); } } 

When I select with the mouse a object in the Editor Hierarchy it will keep making the loop nonstop since selections.Count is large then 0.

I tried to add after the loop:

selections = new List<GameObject>(); 

But it didn't help.

I want that it will make the loop and the whole code inside the if (selections.Count > 0) only once each time when selecting one or more objects in the editor.

If I select one object make the whole code once if I then selected another more object make the whole code over again for the two selected objects and so on but make it once. Now it will keep making the loop over and over again even if I didn't select more other objects.

Since in the editor the first object is still selected it will keep making the code the loop over and over again.