- Home /
EditorGUILayout.ObjectField cannot be changed.
I am making a custom inspector for a class. Currently I am creating a list like so.
public void DrawConnectedNodesGUI(NavigationNode nodeComponent)
{
if (nodeComponent == null) return;
if (nodeComponent.connectedNodes == null)
{
nodeComponent.connectedNodes = new List<NavigationNode>();
}
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Sort Name"))
{
nodeComponent.SortConnectedNodesByName();
}
if (GUILayout.Button("Sort Distance"))
{
nodeComponent.SortConnectedNodesByDistance(); // Create extension method?
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("+"))
{
nodeComponent.connectedNodes.Add(null);
}
if (nodeComponent.connectedNodes.Count == 0)
{
GUILayout.Label("<No connections>");
}
for (int i = 0; i < nodeComponent.connectedNodes.Count; i++)
{
EditorGUILayout.BeginHorizontal();
var result = EditorGUILayout.ObjectField( nodeComponent.connectedNodes[i], typeof(NavigationNode), true) as NavigationNode;
if (GUILayout.Button("-"))
{
if (result == null)
{
nodeComponent.connectedNodes.RemoveAt(i);
}
else
{
nodeComponent.DisonnectFromNode(result, true);
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
}
While the ObjectFields are instantiated properly, I cannot edit them. Dragging and dropping a reference from the hierarchy or project folder makes the field light up like it's about to receive, but it does not change. Picking an object from the popup menu does not work either. Objects added to the list by other methods appear in the inspector, but they cannot be edited either.
Well I figured it out. You need to set the original object equal to the result of the ObjectField call.
nodeComponent.connectedNodes[i] = EditorGUILayout.ObjectField( nodeComponent.connectedNodes[i], typeof(NavigationNode), true) as NavigationNode;
Answer by jmasinteATI · Jan 19, 2017 at 08:14 PM
Like @Davidenishball said, you have to assign the ObjectField object to the target's property. One more bit of info: if you specify a type that is different than what you drag into the field, it also will not update.
Here's my example:
public override void OnInspectorGUI()
{
var customCamera = (target as CustomCamera);
customCamera.CameraBoundingBox = EditorGUILayout.ObjectField("CameraBoundingBox ", customCamera.CameraBoundingBox , typeof(MeshCollider), true) as MeshCollider;
}
Your answer
Follow this Question
Related Questions
How to combine two buttons in one. Editor window 2 Answers
How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer
Is there an event being fired off when the Inspector is being resized? 1 Answer
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer