Editor Scripting: best way to change variables of a custom editor?

Hello,

I am trying to learn editor scripting. So I made a CustomEditor which changes the size of a selected cube.

Now I am unsure about how to change the variable “size” from the editor, ideally I’d like to just type the number into the inspector. Which does not work, probably because custom inspectors create different instances for every selected object. The best solution I found so far is to use a slider, but that feels like a somewhat dirty hack, so I would like to know how this is supposed to be done.

// I have a custom Editor called CubeEditor, which is supposed to change the sizes of cubes
[CustomEditor(typeof(Cube))]
public class CubeEditor : Editor {


    public float size = 2;

    void OnSceneGUI()
    {
        //scale selected cube according to size
        Selection.activeTransform.localScale = (new Vector3(size, size, size));
    }

    public override void OnInspectorGUI()
    {
        //this is the best solution I have come up so far
        size =  GUILayout.HorizontalSlider(size,0f,100f);
        DrawDefaultInspector();       
    }


}

Thank You

@alexanderameye showed variant when size of Cube stored in special Class Size which attached to GameObject Cube and change it value through the property Editor.target.

I decide to create a script which customize Scale properties of Transform component for every object in Editor and change it value through the property Editor.serializedObject that demands to use methods for SerializedObject & SerializedProperty objects, but have some advantage - “Automatic handling of multi-object editing, undo, and Prefab overrides”.

using UnityEngine;
using UnityEditor;
using System.Collections;

// Custom Editor using SerializedProperties.
// Automatic handling of multi-object editing, undo, and Prefab overrides.
[CustomEditor(typeof(Transform))]
[CanEditMultipleObjects]
public class ChangeScaleBySlider : Editor
{
    const string scalePropertySerializedName = "m_LocalScale";
    SerializedProperty scaleProperty;

    void OnEnable()
    {
        // Setup the SerializedProperties
        this.scaleProperty = this.serializedObject.FindProperty(scalePropertySerializedName);
    }

    public override void OnInspectorGUI()
    {
        // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
        serializedObject.Update();

        // Show the default GUI controls except scaleProperty
        DrawPropertiesExcluding(serializedObject, scalePropertySerializedName);

        // Show the custom GUI controls
        float commonScale = this.scaleProperty.vector3Value.x;
        commonScale = EditorGUILayout.Slider("Scale", commonScale, 0, 10f);
        this.scaleProperty.vector3Value = new Vector3(commonScale, commonScale, commonScale);

        // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
        serializedObject.ApplyModifiedProperties();
    }
}

P.S>
The rotation field property drawer is handled a bit differently, because for rotation field the internal store format is Quaternion, but the Editor showing the euler rotation format. Therefore the use of DrawPropertiesExcluding(…) (and DrawDefaultInspector() also) doesn’t give possibility to receive standard GUI format. It is required to completely rewrite the graphical interface for the Transform component, if you what to receive GUI one to one with original (Here the link to corresponding article).

To change the size of a cube in edit-mode with a simple inspector variable, try this:

Script attached to the cube:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Size : MonoBehaviour
 {
	public float SizeOfCube = 1.0f;
}

Script in your Editor folder:
using UnityEditor;
using UnityEngine;

[CustomEditor( typeof(Size))]
public class SizeEditor : Editor
{

	public override void OnInspectorGUI()
	{
		Size size = target as Size;

		DrawDefaultInspector();

		size.transform.localScale = Vector3.one * size.SizeOfCube;
	}
}