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.delta