- Home /
How to correctly use Handles.Slider()?
I'm trying to do some basic Editor GUI functionality for one of my objects. I've been reading about the Handles.Slider() function to automatically create draggable handles that look like the default Transform ones, but I can't get it to work for some reason. It draws the handle and you can select it, but it won't drag anywhere. This is what the code looks like:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(Foo))]
public class FooEditor : Editor {
public override void OnInspectorGUI ()
{
EditorGUIUtility.LookLikeInspector();
base.OnInspectorGUI ();
}
public void OnSceneGUI()
{
Foo testObject = (Foo)target;
Handles.color = Color.magenta;
Vector3 vectorPoint = Handles.Slider( testObject.transform.position, Vector3.left );
if( GUI.changed )
EditorUtility.SetDirty(testObject);
}
}
Any suggestions? Thanks.
EDIT: I may be misunderstanding something, but is it possible to have my object stationary and then drag just the handles so the become longer/shorter? That is basically all I am trying to achieve.
Answer by Bunny83 · Apr 09, 2013 at 11:16 PM
Well, either:
public void OnSceneGUI()
{
Foo testObject = (Foo)target;
Handles.color = Color.magenta;
testObject.transform.position = Handles.Slider( testObject.transform.position, Vector3.left );
if( GUI.changed )
EditorUtility.SetDirty(testObject);
}
or if you want to drag a different "value" something like this:
Foo testObject;
Vector3 somePosition;
public void OnEnable()
{
testObject = (Foo)target;
somePosition = testObject.transform.position;
}
public void OnSceneGUI()
{
Handles.color = Color.magenta;
somePosition = Handles.Slider( somePosition , Vector3.left );
}
So it actually works exactly like all other GUI elements which are supposed to change a value. You have to pass the current value as parameter and the function itself returns the modified value.