```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ScaleController : MonoBehaviour
{
#region Zoom
private float lastDistance = 0;
private float twoTouchDistance = 0;
public RectTransform image;
Vector2 firstTouch = Vector3.zero;
Vector2 secondTouch = Vector3.zero;
private bool isTwoTouch = false;
[SerializeField, Header("缩放")]
float minExtend = 0f;
[SerializeField, Header("扩大")]
float maxExtend = 10.0f;
#endregion
#region Move
private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
[SerializeField]
private RectTransform panelRectTransform;
[SerializeField]
private RectTransform parentRectTransform;
private static int siblingIndex = 0;
#endregion
[SerializeField, Header("UI Camera,需要将Canves Render Mode 设置为 ScreenSpace-Camera")]
public Camera pressEventCamera;
[SerializeField]
public RawImage images;
[SerializeField]
bool isScrollWheel = false;
[SerializeField]
private float mouseExtendScale = 0.0f;
[SerializeField, Header("鼠标缩放计算")]
bool scrollScaleCal = false;
[SerializeField, Range(1, 10)]
int scaleFactor = 3;
public bool isScale = false;
private ScrollRectController rectController;
void Awake()
{
image = this.gameObject.GetComponent<RectTransform>();
panelRectTransform = transform as RectTransform;
parentRectTransform = panelRectTransform.parent as RectTransform;
}
void Start()
{
pressEventCamera = Camera.main;
images = this.transform.GetComponent<RawImage>();
}
private void Update()
{
if (image.localScale.x < 1)
{
image.transform.localScale = new Vector3(1, 1, 1);
}
}
private void LateUpdate()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
TouchExtend();
#elif UNITY_IOS || UNITY_ANDROID
TouchExtend();
#endif
}
void MouseExtend()
{
if (isScrollWheel)
{
float scrollValue = Input.GetAxis("Mouse ScrollWheel");
if (scrollValue < 0)
{
if (mouseExtendScale > 0)
{
mouseExtendScale = 0;
}
mouseExtendScale += scrollValue * scaleFactor;
scrollScaleCal = true;
Debug.Log("Zoom Out:\t" + mouseExtendScale);
}
if (scrollValue > 0)
{
if (mouseExtendScale < 0)
{
mouseExtendScale = 0;
}
mouseExtendScale += scrollValue * scaleFactor;
scrollScaleCal = true;
Debug.Log("Zoom In:\t" + scrollValue);
}
PictureScale();
}
if (Input.GetMouseButtonDown(0))
{
Vector2 mousePostion = Input.mousePosition;
OnPointerDown(mousePostion, pressEventCamera);
}
else if (Input.GetMouseButton(0))
{
Vector2 mousePostion = Input.mousePosition;
OnDrag(mousePostion, pressEventCamera);
}
}
void TouchExtend()
{
if (Input.touchCount > 1)
{
if (Input.GetTouch(1).phase == TouchPhase.Began)
{
isTwoTouch = true;
firstTouch = Input.touches[0].position;
secondTouch = Input.touches[1].position;
lastDistance = Vector2.Distance(firstTouch, secondTouch);
}
if (isTwoTouch)
{
isScale = true;
firstTouch = Input.touches[0].position;
secondTouch = Input.touches[1].position;
twoTouchDistance = Vector2.Distance(firstTouch, secondTouch);
if (image.localScale.x < 1)
{
image.transform.localScale = new Vector3(1, 1, 1);
Debug.Log("最小");
}
else
{
Vector3 curImageScale = new Vector3(image.localScale.x, image.localScale.y, 1);
float changeScaleDistance = (twoTouchDistance - lastDistance) / 100;
Vector3 changeScale = new Vector3(changeScaleDistance, changeScaleDistance, 0);
image.localScale = curImageScale + changeScale;
image.localScale = new Vector3(Mathf.Clamp(image.localScale.x, minExtend, maxExtend), Mathf.Clamp(image.localScale.y, minExtend, maxExtend), 1);
lastDistance = twoTouchDistance;
}
}
if (Input.GetTouch(1).phase == TouchPhase.Ended)
{
rectController.ClearAction();
isTwoTouch = false;
firstTouch = Vector3.zero;
secondTouch = Vector3.zero;
}
}
else if (Input.touchCount == 1)
{
Vector2 pos = Input.GetTouch(0).position;
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
if (images)
{
}
OnPointerDown(pos, pressEventCamera);
}
else if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (images)
{
}
}
}
}
private void PictureScale()
{
if (scrollScaleCal)
{
Vector3 curImageScale = new Vector3(image.localScale.x, image.localScale.y, 1);
float changeScaleDistance = mouseExtendScale / 100;
Vector3 changeScale = new Vector3(changeScaleDistance, changeScaleDistance, 0);
image.localScale = curImageScale + changeScale;
image.localScale = new Vector3(Mathf.Clamp(image.localScale.x, minExtend, maxExtend), Mathf.Clamp(image.localScale.y, minExtend, maxExtend), 1);
scrollScaleCal = false;
}
}
public void OnPointerDown(Vector2 position, Camera pressEventCamera)
{
siblingIndex++;
panelRectTransform.transform.SetSiblingIndex(siblingIndex);
originalPanelLocalPosition = panelRectTransform.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, position, pressEventCamera, out originalLocalPointerPosition);
}
public void OnDrag(Vector2 position, Camera pressEventCamera)
{
if (panelRectTransform == null || parentRectTransform == null)
return;
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, position, pressEventCamera, out localPointerPosition))
{
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
}
ClampToWindow();
}
void ClampToWindow()
{
Vector3 pos = panelRectTransform.localPosition;
Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;
pos.x = Mathf.Clamp(panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp(panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);
panelRectTransform.localPosition = pos;
}
}