Created
November 21, 2014 17:39
-
-
Save DarkDesire/8fb871372057209e00d2 to your computer and use it in GitHub Desktop.
UI PANES, PANELS AND WINDOWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
using System.Collections; | |
public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler { | |
private Vector2 pointerOffset; | |
private RectTransform canvasRectTransform; | |
private RectTransform panelRectTransform; | |
void Awake () { | |
Canvas canvas = GetComponentInParent <Canvas>(); | |
if (canvas != null) { | |
canvasRectTransform = canvas.transform as RectTransform; | |
panelRectTransform = transform.parent as RectTransform; | |
} | |
} | |
public void OnPointerDown (PointerEventData data) { | |
panelRectTransform.SetAsLastSibling (); | |
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out pointerOffset); | |
} | |
public void OnDrag (PointerEventData data) { | |
if (panelRectTransform == null) | |
return; | |
Vector2 pointerPostion = ClampToWindow (data); | |
Vector2 localPointerPosition; | |
if (RectTransformUtility.ScreenPointToLocalPointInRectangle ( | |
canvasRectTransform, pointerPostion, data.pressEventCamera, out localPointerPosition | |
)) { | |
panelRectTransform.localPosition = localPointerPosition - pointerOffset; | |
} | |
} | |
Vector2 ClampToWindow (PointerEventData data) { | |
Vector2 rawPointerPosition = data.position; | |
Vector3[] canvasCorners = new Vector3[4]; | |
canvasRectTransform.GetWorldCorners (canvasCorners); | |
float clampedX = Mathf.Clamp (rawPointerPosition.x, canvasCorners[0].x, canvasCorners[2].x); | |
float clampedY = Mathf.Clamp (rawPointerPosition.y, canvasCorners[0].y, canvasCorners[2].y); | |
Vector2 newPointerPosition = new Vector2 (clampedX, clampedY); | |
return newPointerPosition; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
public class FocusPanel : MonoBehaviour, IPointerDownHandler { | |
private RectTransform panel; | |
void Awake () { | |
panel = GetComponent <RectTransform> (); | |
} | |
public void OnPointerDown (PointerEventData data) { | |
panel.SetAsLastSibling (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler { | |
public Vector2 minSize; | |
public Vector2 maxSize; | |
private RectTransform rectTransform; | |
private Vector2 currentPointerPosition; | |
private Vector2 previousPointerPosition; | |
void Awake () { | |
rectTransform = transform.parent.GetComponent<RectTransform>(); | |
} | |
public void OnPointerDown (PointerEventData data) { | |
rectTransform.SetAsLastSibling(); | |
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition); | |
} | |
public void OnDrag (PointerEventData data) { | |
if (rectTransform == null) | |
return; | |
Vector2 sizeDelta = rectTransform.sizeDelta; | |
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition); | |
Vector2 resizeValue = currentPointerPosition - previousPointerPosition; | |
sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y); | |
sizeDelta = new Vector2 ( | |
Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x), | |
Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y) | |
); | |
rectTransform.sizeDelta = sizeDelta; | |
previousPointerPosition = currentPointerPosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment