赞
踩
//图片集合 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; } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。