Unity3D-FingerGestures控制相机 Pinch Drag Click Swip等

本文详细介绍了如何在Unity3D环境中利用Camera控制器实现手指手势操作,包括触摸点击、拖动、缩放和平移等功能。通过定义宏定义文件BattleConstants,实现了对相机移动范围的精确控制,确保了不同设备上的兼容性和用户体验。文中还提供了关键代码片段的解释,帮助开发者快速理解和应用手势控制逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  参考网址      http://dp0304.com/unity3d/2013/07/28/fingergestures/



FingerGestures 的具体用法 可以参照:
(1)http://blog.youkuaiyun.com/luyuncsd123/article/details/14123977
(2)http://www.xuanyusong.com/archives/1869

1.   Main Camera  transform 上挂载的  文件
Unity3D-FingerGestures控制相机 <wbr>Pinch <wbr>Drag <wbr>Click <wbr>Swip等



2.   Camera的移动边界处理文件  CameraController.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Sensor;
using Common;

namespace Battle 
{
    public class CameraController : MonoBehaviour {
        
        // zoom speed
        int dragSpeed = BattleConstants.ZOOM_SPEED_OTHER;
        //Drag with, height
        float xMinSize = BattleConstants.X_MINUS_BOUNDARY;
        float zMinSize = BattleConstants.Z_MINUS_BOUNDARY;
        //MenuBarHeight
        float zBoundary = BattleConstants.MISSION_BATTLE_UI_BOUNDARY;
        //NGUI layer no
        private const int NGUI_Layer = 8;

        private Vector3 BeginPosition = Vector3.zero;
        private Vector3 EndPosition   = Vector3.zero;
        private Vector3 tempVelocity  = Vector2.zero; //Velocity
        private float   m_smoothTime;      //Smooth Time

        private bool    m_PinchFlag  =  false;
        private bool    m_SwipeFlag  =  false;
        private bool    m_DragFlag   =  true;

        private string  m_BGSelectDell = "BGSelectDell";
        private Dictionary < float ,int > dirctionary = new Dictionary <</span>float,int> ();

        void Start()
        {
            m_PinchFlag  =  false;
            m_SwipeFlag  = false;
            m_DragFlag   = true;
            EndPosition  = transform.position;

            dirctionary.Add (5000.0f, 1000);
            dirctionary.Add (4000.0f, 950);
            dirctionary.Add (3000.0f, 900);
            dirctionary.Add (2000.0f, 850);
            dirctionary.Add (1500.0f, 800);
            dirctionary.Add (1000.0f, 750);
            dirctionary.Add (900.0f, 700);
            dirctionary.Add (800.0f, 650);
            dirctionary.Add (700.0f, 600);
            dirctionary.Add (600.0f, 550);
            dirctionary.Add (500.0f, 500);
            dirctionary.Add (400.0f, 450);
            dirctionary.Add (300.0f, 400);
            dirctionary.Add (200.0f, 350);
            dirctionary.Add (100.0f, 300);
            dirctionary.Add (0.0f,   250);
        }

        // onClick
        void OnTap( TapGesture gesture ) 
        {
            EndPosition = transform.position;
            m_PinchFlag  =  false;
            m_SwipeFlag  =  false;
            m_DragFlag   =  true;
        }

        /// <</span>summary>
        /// Raises the drag event.
        /// FingerGuestureドラッグ
        /// </</span>summary>
        /// <</span>param name="gesture">Gesture.</</span>param>
        void OnDrag(DragGesture gesture) 
        { 
            EndPosition = transform.position;
            m_SwipeFlag = false;

            if (gesture.StartSelection == null || gesture.StartSelection.layer == NGUI_Layer || gesture.StartSelection.name == m_BGSelectDell)
            {
                return;
            }

            if(m_PinchFlag == true)
            {
                return;
            }
            Vector3 screenPos2D = gesture.DeltaMove;
            screenPos2D.z = 0f;            

            //change to screen position
            Vector3 cameraPos2D = Camera.main.WorldToScreenPoint(Camera.main.transform.position);
            cameraPos2D = cameraPos2D - screenPos2D;                

            //move camera position
            Camera.main.transform.position = CalculateOffset ( Camera.main.ScreenToWorldPoint(cameraPos2D));
        }                                    

        /// <</span>summary>
        /// Changes the ratio.
        /// FingerGesture Pinch
        /// </</span>summary>
        /// <</span>param name="gesture">Gesture.</</span>param>
        void OnPinch( PinchGesture gesture )
        {
            if(gesture.Phase == ContinuousGesturePhase.Started)
            {
                BeginPosition = this.transform.position;
                m_PinchFlag = true;
            }
            if(gesture.Phase == ContinuousGesturePhase.Updated)
            {
                if (this.transform.camera.orthographicSize > BattleConstants.ORTHOGRAPHICSIZECBELOW && gesture.Delta > 0) 
                {
                    if (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZECBELOW < gesture.Delta * BattleConstants.PINCHSCALEFACTOR) 
                    {
                        this.transform.camera.orthographicSize = BattleConstants.ORTHOGRAPHICSIZECBELOW;
                    }
                    else 
                    {
                        this.transform.camera.orthographicSize -= gesture.Delta * BattleConstants.PINCHSCALEFACTOR;
                    }
                } 
                else if (transform.camera.orthographicSize < BattleConstants.ORTHOGRAPHICSIZECABOVE && gesture.Delta < 0) 
                {
                    if (BattleConstants.ORTHOGRAPHICSIZECABOVE - this.transform.camera.orthographicSize < Mathf.Abs(gesture.Delta) * BattleConstants.PINCHSCALEFACTOR) 
                    {
                        this.transform.camera.orthographicSize = BattleConstants.ORTHOGRAPHICSIZECABOVE;
                    } 
                    else 
                    {
                        this.transform.camera.orthographicSize -= gesture.Delta * BattleConstants.PINCHSCALEFACTOR;
                    }
                }

                //TODO add by kin 
                xMinSize = BattleConstants.X_MINUS_BOUNDARY + (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZEDEFAULT) / 2;
                zMinSize = BattleConstants.Z_MINUS_BOUNDARY + (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZEDEFAULT);
                zBoundary = BattleConstants.MISSION_BATTLE_UI_BOUNDARY * this.transform.camera.orthographicSize / BattleConstants.ORTHOGRAPHICSIZEDEFAULT;

                Camera.main.transform.position = BeginPosition; //CalculateOffset (Camera.main.transform.position);
            }
            if(gesture.Phase == ContinuousGesturePhase.Ended)
            {
                Invoke("setPinchFlagFalse", 0.15f);
            }
        }
        void setPinchFlagFalse()
        {
            this.m_PinchFlag = false;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <</span>summary>
        /// Raises the swipe event.
        /// </</span>summary>
        /// <</span>param name="gesture">Gesture.</</span>param>
        void OnSwipe( SwipeGesture gesture ) 
        {

            if (gesture.StartSelection == null || gesture.StartSelection.layer == NGUI_Layer || gesture.StartSelection.name == m_BGSelectDell)
            {
                return;
            }
            if(m_PinchFlag == true)
            {
                return;
            }

            float velocity = gesture.Velocity;

            Vector3 screenPos2D = velocity >= 4000 ? gesture.Move : gesture.Move * 1.2f;

            screenPos2D.z = 0f;

            //change to screen position
            Vector3 cameraPos2D = Camera.main.WorldToScreenPoint(Camera.main.transform.position);
            cameraPos2D = cameraPos2D - screenPos2D;                
            
            //move camera position
            EndPosition = CalculateOffset ( Camera.main.ScreenToWorldPoint(cameraPos2D));

            float distance = Vector3.Distance (transform.position, EndPosition);

            foreach (KeyValuePair<</span>float, int> kvp in dirctionary )
            {
                if(velocity > kvp.Key)
                {
                    this.m_smoothTime = distance / kvp.Value;
                }
            }
            m_SwipeFlag = true;
        }

        void Update ()
        {
            if(m_SwipeFlag == true)
            {
                transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, EndPosition.x, ref tempVelocity.x, this.m_smoothTime), 
                                                 transform.position.y,
                                                 Mathf.SmoothDamp(transform.position.z, EndPosition.z, ref tempVelocity.z, this.m_smoothTime));

                if(Vector3.Distance( transform.position, EndPosition) <= 1.0f)
                {
                    transform.position = EndPosition;
                    m_SwipeFlag = false;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <</span>summary>
        /// Calculates the offset.
        /// カメラドラッグ範囲制御
        /// </</span>summary>
        /// <</span>returns>The offset.</</span>returns>
        /// <</span>param name="tmpPosition">Tmp position.</</span>param>
        Vector3 CalculateOffset (Vector3 tmpPosition)
        {    
            if (tmpPosition.x < xMinSize) 
            {
                tmpPosition.x = xMinSize;
            }
            if (tmpPosition.x > xMinSize * -1 ) 
            {
                tmpPosition.x = xMinSize * -1;
            }
            if (tmpPosition.z < zMinSize + zBoundary)
            {
                tmpPosition.z = zMinSize + zBoundary;
            }
            if (tmpPosition.z > zMinSize * -1 ) 
            {
                tmpPosition.z = zMinSize * -1;
            }
            
            return tmpPosition;
        }
    }
}


文件中用的   宏定义 文件  BattleConstants.cs

       
 #region About Battle Camera --------------------------------------
        public const float ORTHOGRAPHICSIZECABOVE         = 140.0f;
        public const float ORTHOGRAPHICSIZECBELOW         = 60.0f;
        public const float ORTHOGRAPHICSIZEDEFAULT         = 100.0f;
        public const float PINCHSCALEFACTOR                      = 0.2f;

        public const int ZOOM_SPEED_IPHONE       = 15;
        public const int ZOOM_SPEED_ANDROID     = 15;
        public const int ZOOM_SPEED_OTHER         = 5;
        
        //DragCameraController.cs
        public const int DRAG_SPEED_IPHONE        = 100;
        public const int DRAG_SPEED_ANDROID     = 100;
        public const int DRAG_SPEED_OTHER         = 5;
        
        
        //mod by kin
        public const float X_MINUS_BOUNDARY        = -140f;
        public const float X_PLUS_BOUNDARY          = 140f;
        public const float Z_MINUS_BOUNDARY        = -100f;
        public const float Z_PLUS_BOUNDARY          = 100f;
        public const float MISSION_BATTLE_UI_BOUNDARY = -30f;
        #endregion

3. 解释
    1.      private Dictionary float ,int dirctionary new Dictionary <</span>float,int();

             存放者健值对  通过放开屏幕时 速度的大小 来确定滑动的时间

     2.     对选择的NGUI 过滤
  if (gesture.StartSelection == null || gesture.StartSelection.layer == NGUI_Layer || gesture.StartSelection.name == m_BGSelectDell)
            {
                return;
            }

  如果选择的对象不是Map 而是NGUI的Layer层或者选择的frefabs名字为 m_BGSelectDell  的情况下  不处理

3.    CalculateOffset 次函数是移动后是否超过 屏幕的边界  超过了就从新计算 终点坐标


Unity3D-FingerGestures控制相机 <wbr>Pinch <wbr>Drag <wbr>Click <wbr>Swip等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值