unity 使用Touch点击跟随移动以及放大缩小图片

//图片集合
public List<Sprite> TE = new List<Sprite>(); 
//图片物体
public GameObject a;

bool BA = false;

int I = 0;


// Start is called before the first frame update
void Start()
{
    a.GetComponent<Image>().sprite = TE[0];
}
float flo;

float flo1;

float AB=0;

Vector3 trv3;

Vector2 transv3;
// Update is called once per frame
void Update()
{
    //若单指触屏
    if (Input.touchCount>0&&Input.touchCount<=1)
    {
        //获取touch
        Touch TO = Input.GetTouch(0);

        switch (TO.phase)
        {
            //刚接触屏幕 获取现在的大小和位置
            case TouchPhase.Began:
                trv3 = a.transform.localScale;
                transv3 = a.GetComponent<RectTransform>().anchoredPosition;
                break;
                //若手指移动
            case TouchPhase.Moved:
                //TO.rawPosition是刚接触屏幕时坐标 刚接触屏幕时坐标与现在的坐标做比较 求出差值
                Vector2 v2 = TO.rawPosition - TO.position;
                //若此时image坐标没有超出屏幕范围 当image坐标为(0,0)时在屏幕中间
                if (a.GetComponent<RectTransform>().anchoredPosition.x>-(Screen.width/2)&& a.GetComponent<RectTransform>().anchoredPosition.x < Screen.width / 2 && a.GetComponent<RectTransform>().anchoredPosition.y < Screen.height/2&& a.GetComponent<RectTransform>().anchoredPosition.y >-Screen.height / 2)
                {
                    //跟随手指移动
                    a.GetComponent<RectTransform>().anchoredPosition -= v2 / 100;
                }
                //下面是若超出了屏幕范围 则赋值于屏幕边缘
                else if (a.GetComponent<RectTransform>().anchoredPosition.x <= -Screen.width)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(-Screen.width+1, a.GetComponent<RectTransform>().anchoredPosition.y);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.x >= Screen.width)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(Screen.width-1, a.GetComponent<RectTransform>().anchoredPosition.y);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.y >= Screen.height)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(a.GetComponent<RectTransform>().anchoredPosition.x, Screen.height-1);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.y <= -Screen.height)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(a.GetComponent<RectTransform>().anchoredPosition.x, -Screen.height+1);
                }

                break;
            case TouchPhase.Stationary:
                break;
                //手指离开屏幕
            case TouchPhase.Ended:
                //若大小和位置没有变化 说明只是点击屏幕  则切换image图片
                if (a.transform.localScale == trv3&&a.GetComponent<RectTransform>().anchoredPosition==transv3)
                {
                    I++;
                    if (I < TE.Count)
                    {
                        a.GetComponent<Image>().sprite = TE[I];
                        a.GetComponent<Image>().SetNativeSize();
                        a.GetComponent<RectTransform>().sizeDelta = new Vector2(a.GetComponent<RectTransform>().rect.width / 50, a.GetComponent<RectTransform>().rect.height / 50);
                    }
                    else
                    {
                        I = 0;
                        a.GetComponent<Image>().sprite = TE[I];
                        a.GetComponent<Image>().SetNativeSize();
                        a.GetComponent<RectTransform>().sizeDelta = new Vector2(a.GetComponent<RectTransform>().rect.width / 50, a.GetComponent<RectTransform>().rect.height / 50);
                    }
                }
                    break;
            case TouchPhase.Canceled:
                break;
            default:
                break;
        }
    }
    //若俩指触屏  则放大缩小图片
  else  if (Input.touchCount>1)
    {
        //获取指头1
        Touch to = Input.GetTouch(0);
        //获取指头2
        Touch to1 = Input.GetTouch(1);

        switch (to.phase)
        {
            //若指头1在移动  若放大缩小图片
            case TouchPhase.Moved:

                BA = true;
              break;
                //若停止移动 则取消放大缩小
            case TouchPhase.Stationary:
                BA = false;
              break;
        }
        switch (to1.phase)
        {
            case TouchPhase.Began:
                //获取指头1和指头2之间的初始坐标差值
                AB = Mathf.Abs(to.rawPosition.x - to1.rawPosition.x);
                break;
            case TouchPhase.Moved:
                BA = true;
              break;
            case TouchPhase.Stationary:
                BA = false;
              break;
        }
        //指头1和指头2现在的坐标差值
        float v2 = Mathf.Abs(to.position.x - to1.position.x);
        //若有指头移动
        if (BA==true)
        {
            //若现在的坐标差值大于之前的坐标差值 则放大图片
            if (v2 > AB&&a.transform.lossyScale.x<=100)
            {
                a.transform.localScale += new Vector3(0.1f,0.1f,0.1f);
                //现在的坐标差值赋值给AB
                AB = v2;
            }
            //若现在的坐标差值小于之前的坐标差值  则缩小图片
            else if (v2 < AB && a.transform.lossyScale.x >= 1f)
            {
                a.transform.localScale -= new Vector3(0.1f,0.1f,0.1f);
                AB = v2;
            }
        }
    }
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值