查看: 607|回复: 0
打印 上一主题 下一主题

[其他] 通过手势拉近相机

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53456
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-5-29 02:23:06 |只看该作者 |倒序浏览
  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.         }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-4 15:14 , Processed in 0.078734 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部