Advertisement
Guest User

Unity 4.6 Beta Draggable Gui Class

a guest
Aug 22nd, 2014
6,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections;
  5.  
  6. public class Draggable : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
  7.     public Transform target;
  8.     private bool isMouseDown = false;
  9.     private Vector3 startMousePosition;
  10.     private Vector3 startPosition;
  11.     public bool shouldReturn;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.    
  16.     }
  17.  
  18.     public void OnPointerDown(PointerEventData dt) {
  19.         isMouseDown = true;
  20.  
  21.         Debug.Log ("Draggable Mouse Down");
  22.  
  23.         startPosition = target.position;
  24.         startMousePosition = Input.mousePosition;
  25.     }
  26.  
  27.     public void OnPointerUp(PointerEventData dt) {
  28.         Debug.Log ("Draggable mouse up");
  29.  
  30.         isMouseDown = false;
  31.  
  32.         if (shouldReturn) {
  33.             target.position = startPosition;
  34.         }
  35.     }
  36.  
  37.     // Update is called once per frame
  38.     void Update () {
  39.         if (isMouseDown) {
  40.             Vector3 currentPosition = Input.mousePosition;
  41.  
  42.             Vector3 diff = currentPosition - startMousePosition;
  43.  
  44.             Vector3 pos = startPosition + diff;
  45.  
  46.             target.position = pos;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement