首页 > 其他 > 详细

手指缩放

时间:2018-04-13 17:31:08      阅读:202      评论:0      收藏:0      [点我收藏+]

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;
            }
        }
    }
}

 

手指缩放

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!