IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> unity 使用Touch点击跟随移动以及放大缩小图片 -> 正文阅读

[游戏开发]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;
            }
        }
    }
}
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 11:09:28  更:2021-07-23 11:10:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 3:17:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码