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...
Answer by Baste · Sep 14, 2015 at 10:54 PM
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.
I'm using Visual Studio on the desktop, but ironically I'm developing this problem project on my laptop and haven't upgraded to Unity 5.2 yet. I will do it tonight and see if it helps.
Looking at your reply though, you say I've supplied 3 arguments to the Rect function - I can't see how? Looking at the code above, I've supplied the following arguments:
1) position.x + 24f
2) position.y + yDivision
3) position.width - 48f
4) yDivision
That's four by my counting?
Turns out default Visual studio wasn't enough, and I was a bit too quick on the draw.
Looking at this from work, and using VS with Resharper, I got a better answer:
new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision )
Unexpected character '–'
Are you writing your code in notepad or wordpad or $$anonymous$$S word? Or any other text program that automatically turns the $$anonymous$$us symbol '-' into the dash symbol '–'? That's the error, anyways; you don't have a $$anonymous$$us there, but another symbol that looks almost completely identical, but isn't the same one.
Lemme put them next to each other for you:
This is a $$anonymous$$us symbol: -
This is not a $$anonymous$$us symbol: –
Replacing all of the dashes with $$anonymous$$uses makes your stuff compile. You should also probably figure out what's causing them to show up, and fix that. If you're just going to code quickly in a text editor, use Notepad++, not Notepad, as the former actually respects what keys you press.
That was it - sorted out the strange error by replacing the dashes with proper $$anonymous$$uses in the code. The dashes were showing up as normal $$anonymous$$uses...
The code is throwing up some other wierd stuff now - to the point that I'm binning it and starting from scratch on my own. Heading off to find the custom drawer Unity tutorial video.
Thanks for your help Baste!
Ahh.. yes, I can see how that happened. I was copy and pasting the code straight from the blog, so that's clearly where the $$anonymous$$uses have been reformatted as dashes in the blog, and thus causing errors in $$anonymous$$onoDevelop. I'll try it out shortly, although I'm sure I'd removed the dashes in $$anonymous$$onoDevelop and tried $$anonymous$$uses in their place and it was still unhappy... Anyhoo - I'll get back with the results shortly.
Your answer
Follow this Question
Related Questions
Property drawer with multiple fields: only first field has prefab override implemented 1 Answer
Draw rect color disappearing when any input is detected 1 Answer
How do you update a property's int value based on two (or more) other properties in the Inspector? 0 Answers
How can I color a PrefixLabel? 0 Answers