自用工具 Unity UI双击放大,右键还原

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

/*
 *
 * 2d界面双击改变摄像机坐标,并且放大.右键还原
 */
public class Camera2DLook : MonoBehaviour
{
    
    #region 计时器参数
    //计时器,在一定的时间内双击有效
    private float time = 0f; 
    //计数器
    private int count = 0;
    // Use this for initialization
    #endregion

    public static bool IsBiger;//已经放大
    void Start () {
	
    }
	
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            
            count++;
            //当第一次点击鼠标,启动计时器
            if (count == 1)
            {
                time = Time.time;
 
            } 
            //当第二次点击鼠标,且时间间隔满足要求时双击鼠标
            if(2 == count && Time.time - time <= 1f&&!IsBiger) 
            {
               Click2ChangePosition();
                count = 0;
            }
            if(Time.time - time > 0.5f)
            {
                // time = 0f;
                count = 0;
            }

 
        }

        if (IsBiger&& Input.GetMouseButton(1))
        {
            GetComponent<Camera>().fieldOfView = 27f;
                
            IsBiger = false;
            GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0, 0, -2239f);
//z轴根据自己的需要改,z轴越小,说明离ui越近.近大远小
        }
    
    }
    public void Click2ChangePosition()
    {
        IsBiger = true;
       // Debug.Log("鼠标坐标"+Input.mousePosition);
      //  Debug.Log("坐标"+GetComponent<Transform>().position);
        Vector3 go=new Vector3(
            x: Input.mousePosition.x -960f ,
            y: Input.mousePosition.y -540f,
            z: -2239);
     //   Debug.Log("运算后" + go);
//上面那个960,540是分辨率大小/2

        GetComponent<RectTransform>().anchoredPosition3D = go;
        GetComponent<Camera>().fieldOfView = 15f;

    }
 
}

双击方法参考【Unity实用小方法】鼠标双击的判断_poda_的博客-CSDN博客_unity 判断双击

猜你喜欢

转载自blog.csdn.net/qq_58804985/article/details/126649159