I’m currently writing a Custom RangeAttribute for Vector2 and want the X and Y Value each to be adjustable by a slider in the inspector.
However the second slider doesn’t seem to be working, i.e. when I try dragging the second slider, the value of the first slider will be changed instead.
[CustomPropertyDrawer(typeof(Vector2RangeAttribute))]
public class Vector2RangeAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var slider2Pos = new Rect(position);
slider2Pos.position += new Vector2(0, 25);
var rangeAttribute = (Vector2RangeAttribute)attribute;
var valX = property.vector2Value.x;
var valY = property.vector2Value.y;
valX = EditorGUI.Slider(position, valX, rangeAttribute.minX, rangeAttribute.maxX);
valY = EditorGUI.Slider(slider2Pos, valY, rangeAttribute.minY, rangeAttribute.maxY);
property.vector2Value = new Vector2(valX, valY);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 40f;
}
}
Here is my Vector2RangeAttribute Class, for which I want to create the Custom Property Drawer:
public class Vector2RangeAttribute : PropertyAttribute
{
public readonly float minX;
public readonly float maxX;
public readonly float minY;
public readonly float maxY;
public Vector2RangeAttribute(float fMinX, float fMaxX, float fMinY, float fMaxY)
{
minX = fMinX;
maxX = fMaxX;
minY = fMinY;
maxY = fMaxY;
}
}
Anyone know how to create multiple sliders for one Property? Or is that not possible?
I tested with only one slider for the X value and that worked fine so I’m really stumped at what’s wrong.
Edit: Scratch that, I’m stupid. I forgot to set the Height of the position Rect to be smaller than the whole property drawer, so of course I couldn’t control the second slider
.
Just had to put position.height = 20; at the start of OnGUI.
Found it 5 minutes after posting this, but couldn’t find it in an hour of trying before
.