Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- [RequireComponent (typeof (BoxCollider))]
- public class Slider : MonoBehaviour {
- public Transform knob;
- public Text textMesh;
- public string sliderName;
- private Vector3 targetPos;
- private float sliderPercent;
- private float sliderLength;
- // Use this for initialization
- void Start () {
- sliderLength = GetComponent<BoxCollider> ().size.x-0.4f;
- targetPos = knob.position;
- }
- // Update is called once per frame
- void Update () {
- knob.position = Vector3.Lerp (knob.position, targetPos, Time.deltaTime*7);
- sliderPercent = Mathf.Clamp01((knob.localPosition.x + sliderLength / 2) / sliderLength);
- textMesh.text = sliderName + ": " + sliderPercent;
- }
- void OnTouchStay (Vector3 point)
- {
- targetPos = new Vector3 (point.x, targetPos.y, targetPos.z);
- }
- public float GetSliderPercent() {
- return sliderPercent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement