At long last!!! Took me too long to figure this out, but this coding is getting easier the more I experiment… practise makes perfect eh ? 
Basically I wanted button in editor window to select objects as I hated having to traverse an expanded Hierarchy list to find main objects. I will try to enhance this by having list of types, like quick picking lights etc…
Heres the code :
class SelectorWindow extends EditorWindow {
var selected: GameObject;
@MenuItem ("Window/Selector")
static function ShowWindow () {
EditorWindow.GetWindow (SelectorWindow);
}
function OnGUI () {
EditorGUILayout.BeginVertical ();
GUILayout.Label ("Select :", EditorStyles.boldLabel);
if (GUI.Button (Rect (10,30,100,25), "FPS Controller")) {
selected = GameObject.Find("First Person Controller");
Selection.activeObject=selected;
}
if (GUI.Button (Rect (120,30,100,25), "SkyLight")) {
selected = GameObject.Find("SkyLight");
Selection.activeObject=selected;
}
if (GUI.Button (Rect (230,30,100,25), "Terrain")) {
selected = GameObject.Find("Terrain");
Selection.activeObject=selected;
}
if (GUI.Button (Rect (340,30,100,25), "Deselect All")) {
Selection.objects = new UnityEngine.Object[0];
}
EditorGUILayout.EndVertical ();
}
}
Let me know on here if you have used this script and found it helpful please… I seem to be the first to post this kind of editor script solution, I couldnt find answers on here at all…
I have’nt tried it yet but was wondering if this works on a tag system? 
Hi Black Mantis, yep it can be done with tags too. Adapted code here :
class SelectorTagsWindow extends EditorWindow {
var selected: GameObject;
var listLights:Array;
@MenuItem ("Window/SelectorTags")
static function ShowWindow () {
EditorWindow.GetWindow (SelectorTagsWindow);
}
function OnGUI () {
EditorGUILayout.BeginVertical ();
GUILayout.Label ("Select :", EditorStyles.boldLabel);
if (GUI.Button (Rect (10,30,100,25), "SkyLight")) {
selected = GameObject.FindWithTag("SkyLightTag");
Selection.activeObject=selected;
EditorGUIUtility.PingObject(selected);
}
EditorGUILayout.EndVertical ();
}
}
By the way, for people who might not know, the filename has to exactly reflect this part of code:
class SelectorTagsWindow
for this example, the file should be called :
SelectorTagsWindow.js
Also, this script should be placed in “Assets/Editor” folder of your project.
Hey guys, I’m working with editor selection in my script right now too. For the most part I believe I understand it, but I’m getting hung up on NOT having something selected.
Heres what I’m trying to do:
-
If I have an object selected, display a custom menu that applies to that object (get all the attributes).
-
If nothing is selected, the custom menu replaces all the values with default values.
I have everything working, except how to check if the selection is null. How can you check if Selection = none?
Nevermind, I figured out a solution to my problem. Basically I used:
if (Selection.activeGameObject)
{
GUILayout.Label("SELECTED OBJECT:\t\t"+Selection.activeTransform.name);
}
else
{
GUILayout.Label("nothing selected . . . ");
}
1 Like