You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
11 months ago
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class DragSize : MonoBehaviour, IDragHandler
|
||
|
{
|
||
|
public enum Mode { Height,Width,Left,Right}
|
||
|
public Mode mode;
|
||
|
public float MinSize =85f;
|
||
|
public float MaxSize =850f;
|
||
|
private float NewSize;
|
||
|
|
||
|
private RectTransform panelRectTransform;
|
||
|
void Start()
|
||
|
{
|
||
|
panelRectTransform = transform.parent as RectTransform;
|
||
|
}
|
||
|
|
||
|
public void OnDrag(PointerEventData data)
|
||
|
{
|
||
|
if (Input.GetMouseButton(0))
|
||
|
{
|
||
|
if (panelRectTransform == null)
|
||
|
return;
|
||
|
if (NewSize < MinSize) NewSize = MinSize;
|
||
|
if (NewSize > MaxSize) NewSize = MaxSize;
|
||
|
switch (mode)
|
||
|
{
|
||
|
case Mode.Width:
|
||
|
break;
|
||
|
case Mode.Height:
|
||
|
NewSize = Input.mousePosition.y;
|
||
|
if (NewSize < MinSize) NewSize = MinSize;
|
||
|
if (NewSize > MaxSize) NewSize = MaxSize;
|
||
|
panelRectTransform.sizeDelta = new Vector2(panelRectTransform.sizeDelta.x, NewSize);
|
||
|
break;
|
||
|
case Mode.Left:
|
||
|
NewSize = Input.mousePosition.x ;
|
||
|
if (NewSize < MinSize) NewSize = MinSize;
|
||
|
if (NewSize > MaxSize) NewSize = MaxSize;
|
||
|
panelRectTransform.offsetMin = new Vector2(NewSize, panelRectTransform.offsetMin.y);
|
||
|
break;
|
||
|
case Mode.Right:
|
||
|
NewSize = Input.mousePosition.x - Screen.width;
|
||
|
if (NewSize < MinSize) NewSize = MinSize;
|
||
|
if (NewSize > MaxSize) NewSize = MaxSize;
|
||
|
panelRectTransform.offsetMax = new Vector2(NewSize, panelRectTransform.offsetMax.y);
|
||
|
break;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|