unity3d移动摄像机,支持PC、手机
2014-03-30 10:24
411 查看
unity3d游戏开发经常会遇到移动摄像机的情况,特别是场景太大,屏幕很难完全展示。
以下代码整合了PC和移动设备移动摄像机的方法,支持两点触控缩放
以下代码整合了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); } } } } } }
相关文章推荐
- 使用mathjax在博客中完美显示数学公式,支持PC,手机浏览器
- 利用角摩电子书控件开发电子书生成网站源码,支持手机、PC和IPAD手持电脑的电子书格式
- PHP判断客户端是PCweb端还是移动手机端方法
- PHP判断客户端是PCweb端还是移动手机端方法
- Unity3D开发之控制摄像机移动
- Google Talk 移动手机版 支持VoIP v4.10.29
- VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
- DirectX11 学习笔记7 - 支持自由移动的摄像机
- magento手机移动设备模板切换的应用和支持
- 手机网民超PC,淘金移动互联网正当时
- Unity3D里实现物体移动到目标点,并且摄像机跟着移动的简单方案
- 移动手机卡支持NFC支付 小米2A刷地铁体验
- Unity3D之移动平台烘培后电脑和手机显示不一样解决方案
- Unity3d 滑动事件--向上、向下、向左、向右(PC好用但是手机滑动不是很理想)
- 手机移动终端与PC进行音视频聊天和监控的开发
- 《黄聪:手机移动站SEO优化教程》4、如何实现手机移动网站和PC站点的自主适配
- 超炫酷的HTML5视频播放器 支持手机移动页面
- 用户从手机的浏览器访问www.baidu.com,看到的可能跟桌面PC电脑,是不太一样的网页效果,会更适合移动设备使用。请简要分析一下,实现这种网页区分显示的原因及技术原理。
- VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之36---支持BREW的手机