KpoKec

Panel resize (child control)

Jul 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using Toolz;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.Rendering.PostProcessing;
  6. using UnityEngine.UI;
  7.  
  8. namespace UI_controls {
  9.     [RequireComponent(typeof(Image))]
  10.     public class ControlSizable : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IDragHandler, IBeginDragHandler, IEndDragHandler {
  11.         [SerializeField]
  12.         private PanelMoveSize control;
  13.         [SerializeField]
  14.         private PanelMoveSize.SizableType type;
  15.         private Vector2 startPos;
  16.         private Vector2 startSize;
  17.         private Image   image;
  18.  
  19.         private void Start() {
  20.             if (type == PanelMoveSize.SizableType.HorizontalRight && control.minWidth  <= 0) control.minWidth  += ((RectTransform) transform).sizeDelta.x;
  21.             if (type == PanelMoveSize.SizableType.HorizontalLeft  && control.minWidth  <= 0) control.minWidth  += ((RectTransform) transform).sizeDelta.x;
  22.             if (type == PanelMoveSize.SizableType.VerticalTop     && control.minHeight <= 0) control.minHeight += ((RectTransform) transform).sizeDelta.y;
  23.             if (type == PanelMoveSize.SizableType.VerticalBottom  && control.minHeight <= 0) control.minHeight += ((RectTransform) transform).sizeDelta.y;
  24.         }
  25.  
  26.         public void OnPointerEnter(PointerEventData eventData) {
  27.             //
  28.         }
  29.  
  30.         public void OnPointerExit(PointerEventData eventData) {
  31.             //
  32.         }
  33.  
  34.         public void OnDrag(PointerEventData eventData) {
  35.             var p = eventData.position;
  36.             p.x /= control.scale.x;
  37.             p.y /= control.scale.y;
  38.             control.Resize(startSize + p - startPos, type);
  39.         }
  40.  
  41.         public void OnBeginDrag(PointerEventData eventData) {
  42.             startPos   =  eventData.position;
  43.             startPos.x /= control.scale.x;
  44.             startPos.y /= control.scale.y;
  45.             startSize  =  control.rTr.sizeDelta;
  46.         }
  47.  
  48.         public void OnEndDrag(PointerEventData eventData) {
  49.         }
  50.     }
  51. }
Add Comment
Please, Sign In to add comment