Selection is a static class whose primary function is to get the object selected by the mouse in the Project window or the currently displayed object of the Inspector window. It has a field
Activegameobject
Activeinstanceid
Activeobject
Activetransform
Gameobjects
Instanceids
Objects
Transforms
Where Activegameobject and Activetransform are the gameobject and transform that correspond to the current display object of the Inspector window. Join is not currently a gameobject, then Activegameobject is empty, Activetransform is the same. Activeinstanceid is the unique ID of the selected file in the project, and the unique file can be found by this ID. Objects is a multi-choice case of all selected objects, these objects can be of various types, generally, unresolved objects, such as folders, ordinary files, the default is the object type. Compared to gameobjects, the official is more recommended to use transforms, their role is only to select all the objects to do type matching filter, return the results after filtering.
In these fields, the use of more is activeobject,objects, because it is a base class, so we can do a variety of judgments and processing. Like what:
var objs=selection.objects;
foreach (Var obj in Objs)
{
if (obj is Gameobject)
Debug.log (Obj.name);
}
The above code is the traversal of all selected objects, if the object is Gameobject (prefab is also gameobject), output the name of the object.
In addition, selection provides a contains method to determine whether the selected object does contain an object, and also provides a getfiltered method for filtering.
Unity Plugin Development--selection