纳金网

标题: 通过手势拉近相机 [打印本页]

作者: 烟雨    时间: 2015-5-29 02:23
标题: 通过手势拉近相机
  1. using UnityEngine;
  2. using System.Collections;

  3. // 输入管理类, 统一鼠标点击和触屏操作。
  4. public class InputM : MonoBehaviour
  5. {
  6.         // 鼠标位置
  7.         public static Vector3 mousePosition;

  8.         // 鼠标单击
  9.         private static bool isMouseButtonDown = false;
  10.         public static bool GetMouseButtonDown()
  11.         {
  12.                 return isMouseButtonDown;
  13.         }

  14.         // 鼠标长按时间
  15.         private static bool isMouseButton = false;
  16.         public static bool GetMouseButton()
  17.         {
  18.                 return isMouseButton;
  19.         }

  20.         // 滚轮
  21.         private static float mouseScrollWheelAxis = 0f;
  22.         public static float GetMouseScrollWheelAxis()
  23.         {
  24.                 return mouseScrollWheelAxis;
  25.         }

  26. #if UNITY_ANDROID || UNITY_IPHONE
  27.         private float scrollValue = 0.02f;                // 触屏两点模拟滚轮
  28.         private Vector2 pos0, pos1;
  29.         private Touch[] touch;
  30. #endif
  31.         void Update()
  32.         {
  33. #if UNITY_ANDROID || UNITY_IPHONE               
  34.                 isMouseButtonDown = Input.GetMouseButtonDown(0);
  35.                 isMouseButton = Input.GetMouseButton (0);               
  36.                 mousePosition = Input.mousePosition;

  37.                 touch = Input.touches;
  38.                 if (touch.Length < 2)
  39.                 {
  40.                         mouseScrollWheelAxis = 0;
  41.                 }
  42.                 else
  43.                 {
  44.                         Vector2 newPos0 = touch[0].position, newPos1 = touch[1].position;
  45.                         if (touch[0].phase != TouchPhase.Began && touch[1].phase != TouchPhase.Began)
  46.                         {
  47.                                 float xOldDis = Mathf.Abs(pos0.x - pos1.x), yOldDis = Mathf.Abs(pos0.y - pos1.y)
  48.                                         , xDis = Mathf.Abs(newPos0.x - newPos1.x), yDis = Mathf.Abs(newPos0.y - newPos1.y);
  49.                                 mouseScrollWheelAxis = (xOldDis - xDis + yOldDis -  yDis) * scrollValue;
  50.                         }
  51.                         pos0 = newPos0;
  52.                         pos1 = newPos1;
  53.                 }
  54. #else
  55.                 isMouseButtonDown = Input.GetMouseButtonDown(0);
  56.                 isMouseButton = Input.GetMouseButton (0);
  57.                 mousePosition = Input.mousePosition;

  58.                 mouseScrollWheelAxis = Input.GetAxis ("Mouse ScrollWheel");
  59. #endif
  60.         }
  61. }
复制代码
需要变化的相机上加上
  1. void Update()
  2.         {
  3.                 float fov = pCamera.fieldOfView;
  4.                 fov += InputM.GetMouseScrollWheelAxis() * sensitivity;
  5.                 fov = Mathf.Clamp(fov, minFov, maxFov);
  6.                 pCamera.fieldOfView = fov;
  7.         }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5