Talk:DragSlider

Update
I'm currently working on a suite of GUI scripts, so this script will be updated along with a series of other parts - checkboxes, text fields, etc. When I'm done, I'll post all of it here. --spaniard 23:09, 29 July 2006 (GMT)

The suite will all be based on some basic classes and should be configurable for any situation or object easily. I haven't had much time to focus on it recently, so it's taking longer than I had originally hoped. Current elements include: The other aspects I'm planning are: If there are other elements that people would like to see, please let me know or edit this list. --spaniard 03:00, 18 August 2006 (GMT)
 * Text field
 * Slider
 * Color picker
 * Checkbox
 * Moveable window panes
 * Basic text buttons
 * Saving and loading of any element through PlayerPrefs
 * Radio buttons (provides same functionality as a drop down menu)
 * Text box (scrollable, word wrap, etc.)
 * Drop down menus
 * Progress bar
 * Scrollable windows
 * Tabs
 * Added progress bar, removed radio buttons --spaniard 00:24, 5 September 2006 (GMT)

Alternate Version
Below is an edited version of the script that uses coroutines rather than the Update method.

// MoveSlider.cs by spaniard 2006-07-19

using UnityEngine; using System.Collections;

public class MoveSlider : MonoBehaviour { // This script requires that the sliderBar and sliderWidget have the same transform // Positioning is done using pixelOffsets public GUITexture sliderBar = null; public GUITexture sliderWidget = null; public float minValue = 0; public float maxValue = 100; public float currentValue = 0; private bool connectedToMouse = false; private float originalX = 0; private float originalMouseX = 0; private float currentX = 0; private float halfWidgetWidth = 0;     // used to centre the widget at either end void Start { halfWidgetWidth = (sliderWidget.pixelInset.xMax - sliderWidget.pixelInset.xMin) / 2; currentX = sliderWidget.pixelInset.xMin + halfWidgetWidth; }

void OnMouseDown { connectedToMouse = true; originalMouseX = Input.mousePosition.x;     originalX = sliderWidget.pixelInset.xMin + halfWidgetWidth; StartCoroutine("UpdatePosition"); }  void OnMouseUp  { connectedToMouse = false; }  IEnumerator UpdatePosition  { do { // calculate currentX based on mouse position currentX = originalX - (originalMouseX - Input.mousePosition.x); // translate from the pixel values to the value currentValue = (((currentX - sliderBar.pixelInset.xMin) / (sliderBar.pixelInset.xMax - sliderBar.pixelInset.xMin)) * (maxValue - minValue)) + minValue; // make sure the value isn't out of bounds if (currentValue < minValue) { currentValue = minValue; } else if (currentValue > maxValue) { currentValue = maxValue; }        // update where the sliderWidget is drawn from currentValue (in case the value is changed externally) currentX = (((currentValue - minValue) / (maxValue - minValue)) * (sliderBar.pixelInset.xMax - sliderBar.pixelInset.xMin)) + sliderBar.pixelInset.xMin; sliderWidget.pixelInset = new Rect ((currentX - halfWidgetWidth), sliderWidget.pixelInset.yMin, (currentX + halfWidgetWidth) - (currentX - halfWidgetWidth), sliderWidget.pixelInset.yMax - sliderWidget.pixelInset.yMin);

yield return new WaitForSeconds(0.05f); } while (connectedToMouse); }

}

I noticed a few errors in this version of the script and fixed them (hopefully):


 * StartCoroutine has a capital S.
 * In C#, coroutines must return the type 'IEnumerator'.
 * Coroutines must have a yield statement somewhere within their loop or they'll hang the program. I added WaitForSeconds with a smallish pause, which should be a good tradeoff between responsiveness and efficiency.

I haven't had time to actually run the code, so I can't guarantee that it works, but it should be more right than before. Hope this helps. :) --NCarter 08:42, 20 July 2006 (GMT)


 * Thanks for that, I haven't had time to test it yet either, so thanks for the corrections! --spaniard 22:02, 20 July 2006 (GMT)

DraggableGUIElement
I just thought you may want to check out DraggableGUIElement as it is almost like a DragSlider. Just in case it gives you some ideas. --Aarku 06:43, 18 August 2006 (GMT)
 * Originally I had used DraggableGUIElement as a jumping off point since it gave me the basic funtionality I was looking for. I've modified the code for the current version of the slider, and I will likely use similar code to implement some of the remaining items. --spaniard 22:40, 21 August 2006 (GMT)