Advertisement
Eriknem

slider

Nov 17th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. [RequireComponent (typeof (BoxCollider))]
  6. public class Slider : MonoBehaviour {
  7.  
  8.     public Transform knob;
  9.     public Text textMesh;
  10.     public string sliderName;
  11.  
  12.     private Vector3 targetPos;
  13.  
  14.     private float sliderPercent;
  15.     private float sliderLength;
  16.  
  17.     // Use this for initialization
  18.     void Start () {
  19.         sliderLength = GetComponent<BoxCollider> ().size.x-0.4f;
  20.         targetPos = knob.position;
  21.    
  22.     }
  23.    
  24.     // Update is called once per frame
  25.     void Update () {
  26.         knob.position = Vector3.Lerp (knob.position, targetPos, Time.deltaTime*7);
  27.  
  28.         sliderPercent = Mathf.Clamp01((knob.localPosition.x + sliderLength / 2) / sliderLength);
  29.  
  30.         textMesh.text = sliderName + ": " + sliderPercent;
  31.     }
  32.  
  33.     void OnTouchStay (Vector3 point)
  34.     {
  35.         targetPos = new Vector3 (point.x, targetPos.y, targetPos.z);
  36.     }
  37.  
  38.     public float GetSliderPercent() {
  39.         return sliderPercent;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement