Define which control to edit a variable with by simple attribute?

Is there a simple way to tell the inspector to use a particular control for editing a given variable?

i.e something along the lines of:

[Slider(min = 0, max = 100)]
public int Something;

For some reason I think I remember seeing that somewhere, but I’ve tried searching and could only find complicated ways to do that. Or did I imagine seeing it…?

I think you imagined it. You need to create a custom editor if you want to do clever things like that!

Very likely that I did. For the record though what I'm trying to do isn't very clever. I'm referring to the simpler cases.

2 Answers

2

Sure would be nice, haven’t seen such thing (might be a future update, who knows).

You could although do this:

1. Create a folder named Editor, in that folder create this script (JavaScript) named customInspectorHelper:

#pragma strict
@CustomEditor(customInspector) 

class customInspectorHelper extends Editor {

	var serObj : SerializedObject;	
	var sliderVal : SerializedProperty;

	function OnEnable () {
		serObj = new SerializedObject (target);
		sliderVal = serObj.FindProperty("sliderVal");
	}

	function OnInspectorGUI () {
		serObj.Update ();

		GUILayout.Label("Pimp My Inspector");
		sliderVal.intValue = EditorGUILayout.IntSlider ("Slider Value", sliderVal.intValue, 0, 100);
		
		serObj.ApplyModifiedProperties();
	}
}

This will add features to the Inspector for the component customInspector which you create in step 2.

2. Create another script (JavaScript) outside of the Editor-folder named customInspector:

#pragma strict

class customInspector extends MonoBehaviour {
	public var sliderVal : int = 0;

    //functions goes here
}

This is the script that goes on the actual GameObject. A couple of lines more than your actual request but it’s really clean if you think about it.

Thanks for the detailed answer! :-) I found the long way with the Editor extensions in my searches. I was just wondering if there is a simpler solution for the simpler end of the spectrum.

You're welcome! There's often a bit of dirty work to make stuff simple, although the platform itself saves us from most of it. :-)

Oh yes I wasn't having a go I'm constantly impressed with Unity. It's amazing what we were able to create with it in such short time.

[Range(0,100)]
public int Something;