- Home /
InspectorEditor: Find target object that has focus
Hello
I'm working on an InspectorEditor for my asset.
When multiple objects are selected with varying data, they need to be synced before the user can edit the data.
For that, I check all the serializedObject.targetObjects
if they have similar data.
When the data is not synced, I want the data of the focused object to be used for all other selected objects.
I found, that the order of the objects in the targetObjects
array does not reflect the order at which they have been clicked. So the focused element can be anywhere in that array.
Is there a way how I can determine which of those objects is the focused object?
For clarity:
If you select multiple objects in the scene view, there is one object that has a light blue mesh and (when pivot mode is selected) contains the tool handle. I call that one the focused object.
Thank you Chillersanim
Answer by chillersanim · Oct 02, 2016 at 11:16 AM
To answer my own question:
The serializedObject.targetObjects
does not reflect the selection order.
But the Selection.activeObject
does.
So by comparing the serializedObject.targetObjects
with the Selection.activeObject
you can find the currently focused object.
Usually the gameObject containing the component is selected, but it is also possible to select a component directly through code.
So it's important to compare the gameObject AND the component with the Selection.activeObject
.
I've writen this method to find the index of the focused serializedObject.targetObjects
:
public int FocusedTargetIndex()
{
var selected = Selection.activeObject;
for (var i = 0; i < serializedObject.targetObjects.Length; i++)
{
var current = (MonoBehaviour)serializedObject.targetObjects[i];
if(current.gameObject == selected || current == selected)
{
return i;
}
}
return -1;
}
This code has worked for me.
If someone finds an error/bug/whatever, please let me know :) .
Greetings
Chillersanim
Your answer
Follow this Question
Related Questions
Draw more EditorGUILayout components? 1 Answer
TexturePropertySingleLine in Editor class 0 Answers
EditorGUILayout.TextField text get's deleted instantly. 2 Answers
Dynamic Target for Object field (and others) in Custom Inspector 0 Answers
[AttributeUsage(AttributeTargets.Field)] - how do I use it? 0 Answers