Custom Class Property Drawer

I’ve created a simple custom class - basically it’s a variable that can handle a range. It’s from a blog by grapefruit games here.

The issue I’m having is that I can’t get the custom property drawer to work. I’m getting odd errors, like:

Assets/Editor/RangeAttributeDrawer.cs(44,108): error CS1526: A new expression requires () or [] after type
Assets/Editor/RangeAttributeDrawer.cs(44,113): error CS8032: Internal compiler error during parsing, Run with -v for details

Here’s both scripts - the custom drawer AND the class I’m trying to create the custom drawer for:

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer (typeof(MinMaxRangeAttribute))]
class RangeAttributeDrawer : PropertyDrawer
{

	// These constants describe the height of the help box and the text field.
	const int helpHeight = 30;
	const int textHeight = 16;
	
	// Provide easy access to the RangeAttribute for reading information from it.
	MinMaxRangeAttribute rangeAttribute { get { return ((MinMaxRangeAttribute)attribute); } }
	
	// Here you must define the height of your property drawer. Called by Unity.
	public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
	{
		return base.GetPropertyHeight (property, label) + 16;
	}
	
	public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
	{
		// Now draw the property as a Slider or an IntSlider based on whether it’s a float or integer.
		var range = attribute as MinMaxRangeAttribute;
		SerializedProperty minValue = property.FindPropertyRelative ("minVal");
		SerializedProperty maxValue = property.FindPropertyRelative ("maxVal");
		var newMin = minValue.floatValue;
		var newMax = maxValue.floatValue;
			
		var xDivision = position.width * 0.33f;
		var yDivision = position.height * 0.5f;
		EditorGUI.LabelField (new Rect (position.x, position.y, xDivision, yDivision), label);


		SerializedProperty minLimit = property.FindPropertyRelative ("minLimit");
		SerializedProperty maxLimit = property.FindPropertyRelative ("maxLimit");

		EditorGUILayout.LabelField ("Min Val:", minValue.ToString ());
		EditorGUILayout.LabelField ("Max Val:", maxValue.ToString ());

		EditorGUI.LabelField( new Rect( position.x, position.y + yDivision, position.width, yDivision ), range.minLimit.ToString( "0.##" ) );
		EditorGUI.LabelField( new Rect( position.x + position.width - 28f, position.y + yDivision, position.width, yDivision ), range.maxLimit.ToString( "0.##" ) );
		EditorGUI.MinMaxSlider( new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision ), ref newMin, ref newMax, range.minLimit, range.maxLimit );
		
		EditorGUI.LabelField( new Rect( position.x + xDivision, position.y, xDivision, yDivision ), "From: " );
		newMin = Mathf.Clamp( EditorGUI.FloatField( new Rect( position.x + xDivision + 30, position.y, xDivision – 30, yDivision ), newMin ), range.minLimit, newMax );
		EditorGUI.LabelField( new Rect( position.x + xDivision * 2f, position.y, xDivision, yDivision ), "To: " );
		newMax = Mathf.Clamp( EditorGUI.FloatField( new Rect( position.x + xDivision * 2f + 24, position.y, xDivision – 24, yDivision ), newMax ), newMin, range.maxLimit );
		
		minValue.floatValue = newMin;
		maxValue.floatValue = newMax;
	}
}

And the custom attribute here:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class MinMaxRangeAttribute : PropertyAttribute
{

	public float minLimit;
	public float minVal;
	public float maxVal;
	public float maxLimit;

	public MinMaxRangeAttribute (float minL, float minV, float maxV, float maxL)
	{
		this.minLimit = minL;
		this.minVal = minV;
		this.maxVal = maxV;
		this.maxLimit = maxL;
	}

	public float RandomVal ()
	{
		return Random.Range (minVal, maxVal);
	}

	public bool IsWithinRange (float value)
	{
		if (value <= maxLimit && value >= minLimit) {
			return true;
		} else {
			return false;
		}
	}
}

I was part-way through finishing the custom drawer, but those errors are stopping me from making progress (so there may be a few mis-named variables in there from the cut-n-paste code from the blog.)

Can anyone help me out by checking the code for errors or cleaning it up for me? I’m halfway to wondering if the MonoDevelop editor is just not happy with the cut n paste and is making up errors to wind me up…

You’ve supplied 3 arguments to the new Rect function on line 44:

EditorGUI.MinMaxSlider( new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision ), ref newMin, ref newMax, range.minLimit, range.maxLimit );

And in two other places further down. The compiler’s spitting out real bad error messages here for some reason.

Also, get Visual Studio (Community Edition’s free). I copy-pasted your code, got a red line under the line with the error, moused over it and got a popup saying “UnityEngine.Rect does not contain a constructor that takes 3 arguments”. MonoDevelop left you with no clues whatsoever and you had to come here for help.