赞
踩
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- /// <summary>
- /// 鼠标拖动UI 滚轮缩放大小
- /// </summary>
-
- public class PageDragToZoom : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IBeginDragHandler, IDragHandler
- {
- /// <summary>
- /// 缩放速度
- /// </summary>
- private float wheelSpeed = 0.1f;
- /// <summary>
- /// 是否缩放(鼠标进入退出 判断)
- /// </summary>
- public bool isTrue;
-
- Vector3 _originScale;//初始大小
- Vector3 _originPoint;//初始位置
- public Button _closeButton;//复位
-
- Vector3 offPos;
- Vector3 arragedPos;
-
- //Vector3 _scaleOne;
- //public Transform _ThisTransform;
- void Start()
- {
- _originPoint = this.transform.position;
- _originScale = this.transform.localScale;
- _closeButton.onClick.AddListener(CloseImage);
- //_scaleOne = transform.localScale;
- }
-
- void Update()
- {
- //UI最大可以放到两倍
- if (transform.localScale.x >= 2 || transform.localScale.x >= 2)
- {
- transform.localScale = new Vector3(2, 2, 2);
- }
- //UI最小可以缩小两倍
- if (transform.localScale.x <= 0.2f || transform.localScale.x <= 0.2f)
- {
- transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
- }
- //通过鼠标滚轮键来对照片进行缩放
- if (isTrue == true)
- {
- // Debug.Log("通过鼠标滚轮键来对照片进行缩放");
- transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
- }
- }
- /// <summary>
- /// 回到初始状态
- /// </summary>
- private void CloseImage()
- {
- this.transform.position = _originPoint;
- this.transform.localScale = _originScale;
-
- }
- /// <summary>
- /// 鼠标进入
- /// </summary>
- /// <param name="eventData"></param>
- public void OnPointerEnter(PointerEventData eventData)
- {
- isTrue = true;
- }
- /// <summary>
- /// 鼠标退出
- /// </summary>
- /// <param name="eventData"></param>
- public void OnPointerExit(PointerEventData eventData)
- {
- isTrue = false;
- }
-
-
-
- /// <summary>
- /// 开始拖拽的时候
- /// </summary>
- /// <param name="eventData"></param>
- public void OnBeginDrag(PointerEventData eventData)
- {
- if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
- , eventData.enterEventCamera, out arragedPos))
- {
- offPos = transform.position - arragedPos;
- }
- }
- /// <summary>
- /// 拖拽中
- /// </summary>
- /// <param name="eventData"></param>
- public void OnDrag(PointerEventData eventData)
- {
- transform.position = offPos + Input.mousePosition;
-
- }
-
-
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。