unity UGUI实现以鼠标中心放大,按鼠标左键移动

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace Framework
{
    public class MyScrollRect : ScrollRect
    {
        private float preX;
        private float preY;
        private int touchNum = 0;
        public override void OnBeginDrag(PointerEventData eventData)
        {
            if (Input.touchCount > 1)
            {
                return;
            }

            base.OnBeginDrag(eventData);
        }

        public override void OnDrag(PointerEventData eventData)
        {
            if (Input.touchCount > 1)
            {
                touchNum = Input.touchCount;
                return;
            }
            else if (Input.touchCount == 1 && touchNum > 1)
            {
                touchNum = Input.touchCount;
                base.OnBeginDrag(eventData);
                return;
            }

            base.OnDrag(eventData);
        }

        void Update()
        {
            if (Input.touchCount == 2)
            {
                Touch t1 = Input.GetTouch(0);
                Touch t2 = Input.GetTouch(1);

                Vector3 p1 = t1.position;
                Vector3 p2 = t2.position;

                float newX = Mathf.Abs(p1.x - p2.x);
                float newY = Mathf.Abs(p1.y - p2.y);

                if (t2.phase == TouchPhase.Began)
                {
                    preX = newX;
                    preY = newY;
                }
                else if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
                {
                    RectTransform rt = base.content;
                    float scale = (newX + newY - preX - preY) / (rt.rect.width * 0.9f) + rt.localScale.x;

                    if (scale > 1 && scale < 3)
                    {
                        rt.localScale = new Vector3(scale, scale, 0);

                        float maxX = base.content.rect.width * scale / 2 - base.viewport.rect.width / 2;
                        float minX = -maxX;

                        float maxY = base.content.rect.height * scale / 2 - base.viewport.rect.height / 2;
                        float minY = -maxY;

                        Vector3 pos = rt.position;

                        if (pos.x > maxX)
                        {
                            pos.x = maxX;
                        }
                        else if (pos.x < minX)
                        {
                            pos.x = minX;
                        }

                        if (pos.y > maxY)
                        {
                            pos.y = maxY;
                        }
                        else if (pos.y < minY)
                        {
                            pos.y = minY;
                        }

                        rt.position = pos;
                    }
                }

                preX = newX;
                preY = newY;
            }
        }

        //实现接口
        public override void OnScroll(PointerEventData eventData)
        {
            ScrollEvent();
        }
        //以鼠标中心放大缩小
        public void ScrollEvent()
        {
            float delX = Input.mousePosition.x - transform.position.x;
            float delY = Input.mousePosition.y - transform.position.y;

            float scaleX = delX / GetComponent<RectTransform>().rect.width / transform.localScale.x;
            float scaleY = delY / GetComponent<RectTransform>().rect.height / transform.localScale.y;

            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                transform.localScale += Vector3.one * 0.1f;
            }
            else if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                if (this.transform.localScale.x < 0.2f || transform.localScale.y < 0.2f)
                {
                    return;
                }
                transform.localScale += Vector3.one * -0.1f;
            }

            GetComponent<RectTransform>().pivot += new Vector2(scaleX, scaleY);
            transform.position += new Vector3(delX, delY, 0);
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值