unity3d移动摄像机,支持PC、手机

unity3d游戏开发经常会遇到移动摄像机的情况,特别是场景太大,屏幕很难完全展示。

以下代码整合了PC和移动设备移动摄像机的方法,支持两点触控缩放

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour {

    // 记录手指触屏的位置
    Vector2 m_screenpos = new Vector2();

    // Use this for initialization
    void Start () {

  // 允许多点触控
        Input.multiTouchEnabled = true;
 }
	
	// Update is called once per frame
    void Update () {

#if !UNITY_EDITOR && ( UNITY_IOS || UNITY_ANDROID )

        MobileInput(); 
#else
        DesktopInput();
#endif


    }

    // 桌面系统鼠标操作
    void DesktopInput()
    {
        // 记录鼠标左键的移动距离
        float mx = Input.GetAxis("Mouse X");
        float my = Input.GetAxis("Mouse Y");


        if (  mx!= 0 || my !=0 )
        {
            //松开鼠标左键 
            if (Input.GetMouseButton(0))
            {
                Camera.main.transform.Translate(new Vector3(mx*Time.deltaTime, my * Time.deltaTime, 0));
            }
        }
    }

    // 移动平台触屏操作
    void MobileInput()
    {

        if (Input.touchCount <= 0)
            return;

        // 1个手指触摸屏幕
        if (Input.touchCount == 1)
        {
            
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                // 记录手指触屏的位置
                m_screenpos = Input.touches[0].position;
                
            }
            // 手指移动
            else if (Input.touches[0].phase == TouchPhase.Moved)
            {
                // 移动摄像机
                Camera.main.transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, Input.touches[0].deltaPosition.y * Time.deltaTime, 0));
            }

            // 手指离开屏幕 判断移动方向
            if (Input.touches[0].phase == TouchPhase.Ended && 
                Input.touches[0].phase != TouchPhase.Canceled)
            {

                Vector2 pos = Input.touches[0].position;

                // 手指水平移动
                if (Mathf.Abs(m_screenpos.x - pos.x) > Mathf.Abs(m_screenpos.y - pos.y))
                {
                    if (m_screenpos.x > pos.x){
                        //手指向左划动
                    }
                    else{

                        //手指向右划动
                    }
                }
                else   // 手指垂直移动
                {
                    if (m_screenpos.y > pos.y){
                        //手指向下划动
                    }
                    else{
                        //手指向上划动
                    }
                   
                }

            }

        }
        else if ( Input.touchCount >1 )
        {
            // 记录两个手指的位置
            Vector2 finger1 = new Vector2();
            Vector2 finger2 = new Vector2();

            // 记录两个手指的移动
            Vector2 mov1 = new Vector2();
            Vector2 mov2 = new Vector2();
      
            for (int i=0; i<2; i++ )
            {
                Touch touch = Input.touches[i];

                if (touch.phase == TouchPhase.Ended )
                    break;

                if ( touch.phase == TouchPhase.Moved )
                {
             
                    float mov = 0;
                    if (i == 0)
                    {
                        finger1 = touch.position;
                        mov1 = touch.deltaPosition;
                       
                    }
                    else
                    {
                        finger2 = touch.position;
                        mov2 = touch.deltaPosition;

                        if (finger1.x > finger2.x)
                       {
                           mov = mov1.x;
                       }
                       else
                       {
                           mov = mov2.x;
                       }

                       if (finger1.y > finger2.y)
                       {
                           mov+= mov1.y;
                       }
                       else
                       {
                           mov+= mov2.y;
                       }

                        Camera.main.transform.Translate(0, 0, mov * Time.deltaTime);
                    }
                   
                   
                }
            }
        }

 
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值