码迷,mamicode.com
首页 > 其他好文 > 详细

手指缩放

时间:2018-04-13 18:01:17      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:getc   com   star   screen   else   功能   ||   frame   orm   

unity 实现两个手指缩放功能有很多插件,比如easyTouch、FingerGestures、TouchKit等这些均为功能比较多插件,有时候单纯为了一个手指缩放的单一功能又没有必要导入插件,所以一下为代码,缩放通过控制scale来实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VideoScreenShrink : MonoBehaviour {
    public float shrinkScale = 0.5f;                       //缩放速度

    private Vector2 initPostion1;
    private Vector2 initPostion2;

    private Vector2 tempPostion1;
    private Vector2 tempPostion2;

    private bool isInited;

    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 1)
        {
            if(!isInited)
            {
                initPostion1 = Input.GetTouch(0).position;
                initPostion2 = Input.GetTouch(1).position;
                isInited = true;
            }
         
            if(Input.GetTouch(0).phase == TouchPhase.Moved||
                Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                tempPostion1 = Input.GetTouch(0).position;
                tempPostion2 = Input.GetTouch(1).position;

                Vector3 iniScale = GetComponent<RectTransform>().localScale;

                float initDis = Vector2.Distance(initPostion1, initPostion2);
                float tempDis = Vector2.Distance(tempPostion1, tempPostion2);
                float offset = tempDis - initDis;
                float offsetScale = (offset / initDis) * shrinkScale;
                Vector3 scale = new Vector3(iniScale.x + offsetScale,iniScale.y + offsetScale,1);

                GetComponent<RectTransform>().localScale = new Vector3(Mathf.Clamp(scale.x, 0.3f, 1),
                    Mathf.Clamp(scale.y, 0.3f, 1), 1);                                                  //比例控制在0.3·1,可手动修改
            }
            else
            {
                isInited = false;
            }
        }
    }
}

 

手指缩放

标签:getc   com   star   screen   else   功能   ||   frame   orm   

原文地址:https://www.cnblogs.com/llstart-new0201/p/8821614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!