I made a basic class
public class EditorSerializaion : Editor
{
public void DrawSerializedField(string name, string title)
{
SerializedProperty property;
property = serializedObject.FindProperty(name);
EditorGUILayout.PropertyField(property, new GUIContent(title));
serializedObject.ApplyModifiedProperties();
}
}
And I want to call it from the Custom editor class
[CustomEditor(typeof(BasicComponentScript))]
public class BaiscComponentEditor : Editor
{
EditorSerializaion editorSerializaion;
public override void OnInspectorGUI()
{
editorSerializaion.DrawSerializedField("pos", "Posotion");
}
}
But it won't work
(in the DrawSerializedField
function the serializedObject.FindProperty(name)
return null
but I do it in the custom inspector with the same name it works)
The solution is to pass the serializedObject
from the first object and it didn't work for me on the start because you need to create a new instance of object.
public class EditorSerializaion : Editor
{ // This has changed
public void DrawSerializedField(SerializedObject sb,string name, string title)
{
SerializedProperty property;
// This has changed
property = sb.FindProperty(name);
EditorGUILayout.PropertyField(property, new GUIContent(title));
// This has changed
sb.ApplyModifiedProperties();
}
}
[CustomEditor(typeof(BasicComponentScript))]
public class BaiscComponentEditor : Editor
{
EditorSerializaion editorSerializaion;
// This is new
private void OnEnable()
{
editorSerializaion = new EditorSerializaion();
}
public override void OnInspectorGUI()
{ // This has changed
editorSerializaion.DrawSerializedField(serializedObject,"pos", "Posotion");
}
}
DrawSerializedField
function to be static and then you won't need to create an instance of it