Hey there.
I am making a Custom Editor class that links game objects in the scene view using Handles.DrawLine. Each GameObject to be linked has a script attached which contains a list of GameObjects called mpChildren.
The list is filled by dragging and dropping GameObjects from the hierarchy onto the variable in the Inspector. I want to do a check on a GameObject being added to see if it is already in the list. Is there a callback function in the Editor to say when an object has been dropped on a variable in the Inspector?
Cheers,
Johnty
GameObject go = EditorGUILayout.ObjectField(null, typeof(GameObject), true);
if (go != null)
{
// Do things here.
}
Where it says “Do things here” you would add the GameObject “go” to your list, or check mpChildren.Contains(go), or whatever it is you need to do.
Sweet cheers. Is this done in the InspectorGUI function call in the Editor script? And do I need to overwrite the Inspector or will that function just recieve a callback when an ObjectField is changed?
Just put it in the OnInspectorGUI. You can add it to the normal Inspector GUI by calling base.OnInspectorGUI() at the top of the method.
private List<GameObject> linkedGameObjects;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Add GameObject");
GameObject go = (GameObject)EditorGUILayout.ObjectField(null, typeof(GameObject), true);
EditorGUILayout.EndHorizontal();
if (go != null)
{
if (!linkedGameObjects.Contains(go))
linkedGameObjects.Add(go);
}
int removeIndex = -1;
for (int i = 0; i < linkedGameObjects.Count; i++)
{
if (GUILayout.Button(linkedGameObjects[i].name))
{
removeIndex = i;
}
}
if (removeIndex > -1)
linkedGameObjects.RemoveAt(removeIndex);
}
That code can be used to add GameObjects to the script, display the GameObjects linked to the script, and remove them.