Advertisement
cc2k

Untitled

Oct 31st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using UnityEngine.UI;
  6.  
  7. public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  8. {
  9.  
  10.     public Vector2 dragOffset = new Vector2(0f, 0f);
  11.     public Transform parentToReturnTo = null;
  12.     public Transform placeHolderParent = null;
  13.     GameObject placeHolder = null;
  14.  
  15.     public void OnBeginDrag(PointerEventData eventData)
  16.     {
  17.         //  Debug.Log("OnBeginDrag");
  18.  
  19.         placeHolder = new GameObject();
  20.         placeHolder.transform.SetParent(this.transform.parent);
  21.         LayoutElement le = placeHolder.AddComponent<LayoutElement>();
  22.         le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
  23.         le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
  24.         le.flexibleHeight = 0;
  25.         le.flexibleWidth = 0;
  26.  
  27.         placeHolder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
  28.  
  29.  
  30.         parentToReturnTo = this.transform.parent;
  31.         placeHolderParent = parentToReturnTo;
  32.         this.transform.SetParent(this.transform.parent.parent);
  33.  
  34.         GetComponent<CanvasGroup>().blocksRaycasts = false;
  35.  
  36.         dragOffset = eventData.position - (Vector2)this.transform.position;
  37.     }
  38.  
  39.     public void OnDrag(PointerEventData eventData)
  40.     {
  41.         //Debug.Log("OnDrag");
  42.  
  43.         this.transform.position = eventData.position;
  44.  
  45.         if (placeHolder.transform.parent != placeHolderParent )
  46.         {
  47.             placeHolder.transform.SetParent(placeHolderParent);
  48.         }
  49.  
  50.  
  51.         int newSiblingIndex = placeHolderParent.childCount ;
  52.  
  53.         for(int i =0; i < placeHolderParent.childCount; i++)
  54.         {
  55.             if(this.transform.position.x < placeHolderParent.GetChild(i).position.x)
  56.             {
  57.                 newSiblingIndex = i;
  58.                 if(placeHolder.transform.GetSiblingIndex()< newSiblingIndex)
  59.                 {
  60.                     newSiblingIndex--;
  61.                 }
  62.  
  63.          
  64.                 break;
  65.             }
  66.         }
  67.  
  68.  
  69.         placeHolder.transform.SetSiblingIndex(newSiblingIndex);
  70.  
  71.         this.transform.position = eventData.position - dragOffset;
  72.     }
  73.  
  74.     public void OnEndDrag(PointerEventData eventData)
  75.     {
  76.         //Debug.Log("OnEndDrag");
  77.         Debug.Log("name: " + parentToReturnTo.name);
  78.  
  79.         if (!parentToReturnTo.name.Equals("Tabletop opponent")) {
  80.             this.transform.SetParent(parentToReturnTo);
  81.             this.transform.SetSiblingIndex(placeHolder.transform.GetSiblingIndex());
  82.         } else
  83.         {
  84.             this.transform.SetParent(placeHolderParent);
  85.             this.transform.SetSiblingIndex(placeHolder.transform.GetSiblingIndex());
  86.         }
  87.         GetComponent<CanvasGroup>().blocksRaycasts = true;
  88.    
  89.         Destroy(placeHolder);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement