Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace UI_controls {
- public class PanelMoveSize : MonoBehaviour {
- public enum SizableType {
- HorizontalLeft = 0,
- HorizontalRight = 1,
- VerticalTop = 2,
- VerticalBottom = 4
- }
- public enum MovableType {
- Top = 0,
- Bottom = 1,
- Left = 2,
- Right = 3
- }
- public float stickDistance = 20f;
- public float minWidth;
- public float maxWidth;
- public float minHeight;
- public float maxHeight;
- public RectTransform rTr { get; private set; }
- public Vector2 scale => parentCanvas.localScale;
- private RectTransform parentCanvas;
- private Image image;
- void Start() {
- rTr = (RectTransform) transform;
- parentCanvas = (RectTransform) gameObject.GetComponentInParent<Canvas>().transform;
- image = GetComponent<Image>();
- image.sprite = MainDatabase.PanelViewSo.background;
- image.color = MainDatabase.PanelViewSo.backgroundColor;
- }
- public void Resize(Vector2 delta, SizableType type) {
- var sd = rTr.sizeDelta;
- switch (type) {
- case SizableType.HorizontalLeft:
- sd.x = delta.x;
- break;
- case SizableType.HorizontalRight:
- sd.x = delta.x;
- break;
- case SizableType.VerticalTop:
- sd.y = delta.y;
- break;
- case SizableType.VerticalBottom:
- sd.y = delta.y;
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(type), type, null);
- }
- sd.x = Mathf.Clamp(sd.x, minWidth, maxWidth < minWidth ? sd.x : maxWidth);
- sd.y = Mathf.Clamp(sd.y, minHeight, maxHeight < minHeight ? sd.y : maxHeight);
- Vector2 ap;
- var sd2 = sd - rTr.sizeDelta;
- switch (type) {
- case SizableType.HorizontalLeft:
- ap = new Vector2(sd2.x * (1f - rTr.pivot.x), 0f);
- break;
- case SizableType.HorizontalRight:
- ap = new Vector2(sd2.x * rTr.pivot.x, 0f);
- break;
- case SizableType.VerticalTop:
- ap = new Vector2(0f, sd2.y * rTr.pivot.y);
- break;
- case SizableType.VerticalBottom:
- ap = new Vector2(0f, -sd2.y * (1f - rTr.pivot.y));
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(type), type, null);
- }
- rTr.sizeDelta = sd;
- CheckOnScreen(rTr.anchoredPosition + ap);
- }
- public void Move(Vector2 position) {
- var e = position;
- CheckOnScreen(e);
- }
- public void CheckOnScreen(Vector2 newPos) {
- var rect = rTr.rect;
- if (rect.xMin + newPos.x < stickDistance) newPos.x = -rect.xMin;
- if (rect.xMax + newPos.x > parentCanvas.sizeDelta.x - stickDistance) newPos.x = parentCanvas.sizeDelta.x - rect.xMax;
- if (newPos.y + rect.yMax > -stickDistance) newPos.y = -(rect.yMax);
- if (newPos.y + rect.yMin < -parentCanvas.sizeDelta.y + stickDistance) newPos.y = -(parentCanvas.sizeDelta.y + rect.yMin);
- rTr.anchoredPosition = newPos;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement