Talk:DragSlider
From Unify Community Wiki
(Difference between revisions)
(→Alternate Version) |
(Update) |
||
Line 73: | Line 73: | ||
*Thanks for that, I haven't had time to test it yet either, so thanks for the corrections! --[[User:Spaniard|spaniard]] 22:02, 20 July 2006 (GMT) | *Thanks for that, I haven't had time to test it yet either, so thanks for the corrections! --[[User:Spaniard|spaniard]] 22:02, 20 July 2006 (GMT) | ||
+ | |||
+ | == 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. --[[User:Spaniard|spaniard]] 23:09, 29 July 2006 (GMT) |
Revision as of 23:09, 29 July 2006
Alternate Version
Below is an edited version of the script that uses coroutines rather than the Update method.
<csharp>// 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); }
}</csharp>
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)
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)