- Home /
What is Selection.SetActiveObjectWithContext for?
UnityEditor.Selection has a function SetActiveObjectWithContext() and a counterpart field activeContext.
What is this for? The documentation is not exactly forthcoming.
I would imagine it has to do with context in the respect of lighting https://docs.unity3d.com/$$anonymous$$anual/StandardShaderContextAndContent.html
I'm not sure though
Answer by inferno222 · Sep 28, 2017 at 10:26 AM
I found out you can use it to select scriptable objects with it, which can't be done directly with Selection.activeGameObject
because it requires a gameObject.
For example: Selection.SetActiveObjectWithContext(Your_Scriptable_Object, null);
It will be nice to know what the context is used for.
Answer by codestage · Nov 28, 2017 at 09:42 PM
Docs are pretty empty for this, yeah =(
My guess: it just allows to store some additional object in current selection for more flexibility.
E.g. you could wish to select some object and store its parent or other dependency or any other related Unity object as context for further usage.
@inferno222 for non-GameObject selection there is another property:
Selection.activeObject
and it can be freely used without Selection.SetActiveObjectWithContext()
usage.
Answer by Bunny83 · Sep 11, 2018 at 12:51 PM
Since the question got bumped already
The point of the context is to provide additional information that is not part of the selected object. So it could be a ScriptableObject instance that gives more information about the selected "thing". Of course its use is context specific.
Imagine you have a ScriptableObject or MonoBehaviour that has an array of strings. If you need in some custom editor to select one of those strings you could reference the scriptableobject / MonoBehaviour as the selected object and create a special ScriptableObject instance that holds the information about the index which string you actually meant. Keep in mind that the Selection class is the main link between different editor tools.
Have you actually looked at the Selection class documentation? If so the very first property it has is the activeContext. There you access the context if one has been set. The only callback that the Selection class provides is the selectionChanged delegate.
Answer by Bill-Sansky · Sep 11, 2018 at 11:49 AM
More information for those coming here late: The difference between
Selection.activeObject = obj;
and
Selection.SetActiveObjectWithContext(obj, obj);
is that the second one will also change Selection.objects to be only the active object: always good to know!