I’m having some trouble with EditorGUI/EditorGUILayout. I’ve got a setup where I want the user to be able to edit the variables of an object with an Inspector-like interface in an EditorWindow. So, I thought it a good idea to use FloatField, IntField and TextField for the variables. But the problem is that when the user selects a new object, all the fields change to show the values of the new object, except the field that has focus! It still shows the value of the previous object.
Is this a bug, or am I doing something wrong?
Here’s the object I’m trying to edit:
using System;
using UnityEngine;
public class TestClass : MonoBehaviour
{
public string firstName;
public string lastName;
public int age;
public float weight;
}
And here’s the code for the EditorWindow:
using System;
using UnityEditor;
using UnityEngine;
public class Editor : EditorWindow
{
[MenuItem ("Custom/Editor")]
static void Edit ()
{
Editor editor = new Editor();
editor.position = new Rect(200, 200, 600, 500);
editor.autoRepaintOnSceneChange = true;
editor.Show();
}
void OnGUI()
{
if (Selection.activeGameObject != null)
{
TestClass selected = (TestClass)Selection.activeGameObject.GetComponent(typeof(TestClass));
if (selected != null)
{
selected.firstName = EditorGUILayout.TextField("First Name", selected.firstName);
selected.lastName = EditorGUILayout.TextField("Last Name", selected.lastName);
selected.age = EditorGUILayout.IntField("Age", selected.age);
selected.weight = EditorGUILayout.FloatField("Weight", selected.weight);
}
}
}
}
Any help appreciated. Thanks.