Advertisement
KpoKec

PanelMoveResize (main)

Jul 11th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5.  
  6. namespace UI_controls {
  7.     public class PanelMoveSize : MonoBehaviour {
  8.         public enum SizableType {
  9.             HorizontalLeft  = 0,
  10.             HorizontalRight = 1,
  11.             VerticalTop     = 2,
  12.             VerticalBottom  = 4
  13.         }
  14.  
  15.         public enum MovableType {
  16.             Top    = 0,
  17.             Bottom = 1,
  18.             Left   = 2,
  19.             Right  = 3
  20.         }
  21.  
  22.         public float stickDistance = 20f;
  23.  
  24.         public float minWidth;
  25.         public float maxWidth;
  26.         public float minHeight;
  27.         public float maxHeight;
  28.  
  29.         public  RectTransform rTr   { get; private set; }
  30.         public  Vector2       scale => parentCanvas.localScale;
  31.         private RectTransform parentCanvas;
  32.         private Image         image;
  33.  
  34.         void Start() {
  35.             rTr          = (RectTransform) transform;
  36.             parentCanvas = (RectTransform) gameObject.GetComponentInParent<Canvas>().transform;
  37.             image        = GetComponent<Image>();
  38.             image.sprite = MainDatabase.PanelViewSo.background;
  39.             image.color  = MainDatabase.PanelViewSo.backgroundColor;
  40.         }
  41.  
  42.         public void Resize(Vector2 delta, SizableType type) {
  43.             var sd = rTr.sizeDelta;
  44.             switch (type) {
  45.                 case SizableType.HorizontalLeft:
  46.                     sd.x = delta.x;
  47.                     break;
  48.                 case SizableType.HorizontalRight:
  49.                     sd.x = delta.x;
  50.                     break;
  51.                 case SizableType.VerticalTop:
  52.                     sd.y = delta.y;
  53.                     break;
  54.                 case SizableType.VerticalBottom:
  55.                     sd.y = delta.y;
  56.                     break;
  57.                 default:
  58.                     throw new ArgumentOutOfRangeException(nameof(type), type, null);
  59.             }
  60.             sd.x = Mathf.Clamp(sd.x, minWidth,  maxWidth  < minWidth ? sd.x : maxWidth);
  61.             sd.y = Mathf.Clamp(sd.y, minHeight, maxHeight < minHeight ? sd.y : maxHeight);
  62.  
  63.             Vector2 ap;
  64.             var     sd2 = sd - rTr.sizeDelta;
  65.             switch (type) {
  66.                 case SizableType.HorizontalLeft:
  67.                     ap = new Vector2(sd2.x * (1f - rTr.pivot.x), 0f);
  68.                     break;
  69.                 case SizableType.HorizontalRight:
  70.                     ap = new Vector2(sd2.x * rTr.pivot.x, 0f);
  71.                     break;
  72.                 case SizableType.VerticalTop:
  73.                     ap = new Vector2(0f, sd2.y * rTr.pivot.y);
  74.                     break;
  75.                 case SizableType.VerticalBottom:
  76.                     ap = new Vector2(0f, -sd2.y * (1f - rTr.pivot.y));
  77.                     break;
  78.                 default:
  79.                     throw new ArgumentOutOfRangeException(nameof(type), type, null);
  80.             }
  81.  
  82.             rTr.sizeDelta = sd;
  83.             CheckOnScreen(rTr.anchoredPosition + ap);
  84.         }
  85.  
  86.         public void Move(Vector2 position) {
  87.             var e = position;
  88.             CheckOnScreen(e);
  89.         }
  90.  
  91.         public void CheckOnScreen(Vector2 newPos) {
  92.             var rect = rTr.rect;
  93.  
  94.             if (rect.xMin + newPos.x < stickDistance) newPos.x                            = -rect.xMin;
  95.             if (rect.xMax + newPos.x > parentCanvas.sizeDelta.x - stickDistance) newPos.x = parentCanvas.sizeDelta.x - rect.xMax;
  96.  
  97.             if (newPos.y + rect.yMax > -stickDistance) newPos.y                            = -(rect.yMax);
  98.             if (newPos.y + rect.yMin < -parentCanvas.sizeDelta.y + stickDistance) newPos.y = -(parentCanvas.sizeDelta.y + rect.yMin);
  99.  
  100.             rTr.anchoredPosition = newPos;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement