2019独角兽企业重金招聘Python工程师标准>>>

比如2D游戏 地图很大,像植物大战僵尸关卡选择界面,需要对可见的区域进行滑动,缩放等,

1....滑动:通过正交摄像机的orthographicSize和位置和实际大小(可视范围大小),计算出距离世界坐标原点偏移量 来计算出位置

2....缩放,对正交相机的orthographicSize进行修改,达到放缩目的。同样也要计算摄像机实际大小()可视范围大小)

3.....正交相机大小的算法   摄像机实际视界宽度(米)=size*2*屏幕宽高比

当然该方法不限于正交相机 透视相机也可以利用这种思路来计算,当然还要考虑像素和米的换算关系,以下代码块像素米比例是1,即1像素=1米

using System.Collections;
using System.Collections.Generic;
using UnityEngine;// for map mouse and touch slide and zoom
[RequireComponent(typeof(Camera))]
public class MapCameraSlider : MonoBehaviour
{Camera camera = null;float SCREEN_WIDTH = 1136f;//屏幕宽度float SCREEN_HEIGHT = 640f; // 屏幕高度const float max_allow_width = 1024f * 3f; // 最大允许滑动的宽度const float max_allow_height = 1024f * 3f; // 最大允许滑动的高度float distanceScale;Vector3 pos1;Vector3 pos2;const float SCALEPOSTION = 1f; // 全局位置缩放大小bool touchBeg = false;void Start(){SCREEN_HEIGHT = Screen.height;SCREEN_WIDTH = Screen.width;camera = this.GetComponent<Camera>();if (camera == null){Debug.LogError("need camera component");}}void Update(){if (touchBeg){timeTouch += Time.deltaTime;}// ------------------------------------for mobile touch inputif (Input.touchCount == 1){ // slidevar data = Input.GetTouch(0);this.Slide(data.deltaPosition * SCALEPOSTION);pos1 = Vector3.zero;pos2 = Vector3.zero;distanceScale = 0f;if (data.phase == TouchPhase.Began){StopAllCoroutines();touch_beg = data.deltaPosition * SCALEPOSTION;timeTouch = 0.000001f;touchBeg = true;}if (data.phase == TouchPhase.Ended){touchBeg = false;this.StartAutoSlide(touch_beg, data.position);}}else if (Input.touchCount == 2){// scaletouchBeg = false;var d1 = Input.GetTouch(0);var d2 = Input.GetTouch(1);if (d1.phase == TouchPhase.Began){pos1 = d1.position;distanceScale = Vector3.Distance(pos1, pos2);}if (d2.phase == TouchPhase.Began){pos2 = d2.position;distanceScale = Vector3.Distance(pos1, pos2);}if (pos2 != Vector3.zero && pos2 != Vector3.zero){float dis = Vector3.Distance(pos1, pos2);if (d1.phase == TouchPhase.Moved || d2.phase == TouchPhase.Moved){if (dis > distanceScale){this.ZoomOut(Time.deltaTime * 500f);}else if (dis < distanceScale){this.ZoomIn(Time.deltaTime * 500f);}}distanceScale = dis;}}else{touchBeg = false;pos1 = Vector3.zero;pos2 = Vector3.zero;distanceScale = 0f;}//--------------------------------- for pc mouse input//slideif (Input.GetMouseButton(0)){if (lastMousePos == Vector2.zero){timeTouch = 0.000001f;StopAllCoroutines();lastMousePos = Input.mousePosition;touch_beg = lastMousePos;return;}else{timeTouch += Time.deltaTime;Vector2 pos = Input.mousePosition;this.Slide(1.25f * (pos - lastMousePos));lastMousePos = pos;}}else{if (lastMousePos != Vector2.zero){this.StartAutoSlide(touch_beg, Input.mousePosition);}lastMousePos = Vector2.zero;}//----------------------for pc zoomif (Input.GetAxis("Mouse ScrollWheel") < 0){this.ZoomOut(Time.deltaTime * 500f);this.Slide(0f, 0f);}//Zoom inif (Input.GetAxis("Mouse ScrollWheel") > 0){this.ZoomIn(Time.deltaTime * 500f);this.Slide(0f, 0f);}}float maxtime = 3f;//开始惯性动画void StartAutoSlide(Vector2 orign, Vector2 ended){if (Mathf.Abs(timeTouch) < 0.01f){return;}maxtime = 3f;StopAllCoroutines();Debug.Log("slide map ,speed = " + (Vector2.Distance(orign, ended) / timeTouch / 100f) + "  touchTime=" + timeTouch + " posended" + ended + "  beg" + orign);StartCoroutine(RunSliderAction(ended.x - orign.x, ended.y - orign.y, Vector2.Distance(orign, ended) / timeTouch / 100f));timeTouch = 0.000001f;}Vector2 touch_beg;float timeTouch = 0.000001f;IEnumerator RunSliderAction(float dx, float dy, float speed){float time = 0f;float dis = 0f;while (time < maxtime && speed >= 0f){yield return new WaitForEndOfFrame();time += Time.deltaTime;dis += Time.deltaTime * 0.01f;//  Debug.LogError(pos + "      " + time + "    speed=" + speed);speed -= Time.deltaTime * 10f;this.Slide(Time.deltaTime * speed * dx, dy * Time.deltaTime * speed);}}Vector2 lastMousePos = Vector2.zero;//滑动接口,参数是偏移量void Slide(Vector2 dp){this.Slide(dp.x, dp.y);}//滑动接口,参数是偏移量void Slide(float dx, float dy){dx = -dx;dy = -dy;var pos = transform.position;pos.x += dx;pos.y += dy;transform.position = pos;//实际宽度float real_width = camera.orthographicSize * 2 * SCREEN_WIDTH / SCREEN_HEIGHT;float real_height = real_width * SCREEN_HEIGHT / SCREEN_WIDTH;//process edgeif (transform.position.x <= real_width / 2f){//x leftthis.SetPosX(0);}if (transform.position.x >= max_allow_width - real_width / 2f){// x  rightthis.SetPosX(max_allow_width - real_width);}if (transform.position.y <= real_height / 2f){//  y upthis.SetPosY(0);}if (transform.position.y >= max_allow_height - real_height / 2f){// y downthis.SetPosY(max_allow_height - real_height);}}//放大接口void ZoomOut(float delta){camera.orthographicSize = camera.orthographicSize + delta;float real_width = camera.orthographicSize * 2 * SCREEN_WIDTH / SCREEN_HEIGHT;float real_height = real_width * SCREEN_HEIGHT / SCREEN_WIDTH;float max_size = max_allow_width * SCREEN_HEIGHT / 2f / SCREEN_WIDTH;if (real_width >= max_allow_width){camera.orthographicSize = max_size;}}//缩小接口void ZoomIn(float delta){camera.orthographicSize = camera.orthographicSize - delta;float real_width = camera.orthographicSize * 2 * SCREEN_WIDTH / SCREEN_HEIGHT;float real_height = real_width * SCREEN_HEIGHT / SCREEN_WIDTH;float min_size = 150f;if (camera.orthographicSize <= min_size){camera.orthographicSize = min_size;}}// 设置x偏移量,偏移量是从世界坐标原点开始计算void SetPosX(float offsetX){float real_width = camera.orthographicSize * 2 * SCREEN_WIDTH / SCREEN_HEIGHT;float real_height = real_width * SCREEN_HEIGHT / SCREEN_WIDTH;var posOld = transform.position;transform.position = new Vector3(real_width / 2f + offsetX, posOld.y, posOld.z);}// 设置y偏移量,偏移量是从世界坐标原点开始计算void SetPosY(float offsetY){float real_width = camera.orthographicSize * 2 * SCREEN_WIDTH / SCREEN_HEIGHT;float real_height = real_width * SCREEN_HEIGHT / SCREEN_WIDTH;var posOld = transform.position;transform.position = new Vector3(posOld.x, offsetY + real_height / 2f, posOld.z);}}

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/1537139

unity2D平面摄像机滑动缩放相关推荐

  1. echarts鼠标滑动缩放后会自动回弹(已解决)

    今天碰到的一个小坑:由于数据过多,在有限的视野内显示不完,需要滑动缩放完成数据的预览.跟着官方文档写之后确实是实现缩放和滑动,但是问题也就出现了,过了几秒后自动回弹原大小. 原代码:(会发生回弹) d ...

  2. Android自定义-滑动缩放渐变填充曲线折线图表

    文章目录 自定义 一.基础操作 1.新建类 2.坐标系 3.简单的折线图 二.修饰折线图 1.折线图添加文字修饰. 三. 任意区域`可点击`的折线图 1.画布区域点击事件 1.区域点击带来的精彩 四. ...

  3. Android高级自定义,手势滑动缩放/渐变填充/曲线折线图表

    /   今日科技快讯   / 近日网易公司发布2020年第四季度及2020财政年度业绩.根据财报,网易公司第四季度净收入为人民币197.6亿元,同比增长25.6%.第四季度,网易公司各项业务稳健发展. ...

  4. 基于ArcGIS JS API实现垂直滑动缩放条

    文章目录 需求背景 需求分析 效果图 完整代码 注意事项 严格来说并不是基于ArcGIS JS API,应该是基于Dojo的dijit里面的VerticalSlider和VerticalRule,但是 ...

  5. android横向滑动缩放,移动端实现内容左右滑动,并点击放大效果的问题

    需求如题,代码如下: /*html*/ 送鲜花 /*css*/ .box{ display: flex; align-items: center; width: 100%; height: 4rem; ...

  6. CSS——字体图标、平面转换、缩放

    一.字体图标 字体图标主要用于网页中通用.常用的一些小图标: 精灵图的缺点:1.图片文件大: 2.图片放大和缩小会有失真的问题: 3.一旦图片制作完成想要更换非常复杂. 字体图标iconfont,展示 ...

  7. Unity3D 摄像机滑动跟随

    using UnityEngine; using System.Collections; public class BallCamera : MonoBehaviour {     //跟随目标   ...

  8. Input.GetAxis(Mouse ScrollWheel)控制摄像机视野缩放

    Camera.main.fieldOfView += Input.GetAxis("Mouse ScrollWheel") * 10; Debug.Log(Camera.main. ...

  9. Unity2d平面动作游戏教程

    最终效果如下: 目录 玩家的基础移动 加入移动和跳跃动画 加入攻击动作 实现打击感 添加敌人受击动画与击退 添加敌人受击特效 攻击时的屏幕振动 首先实现最基础的功能,将环境拖拽进来,注意到环境的各个前 ...

最新文章

  1. android8.1获取蓝牙地址,[蓝牙] Android 8.1 获取蓝牙设备地址无效;02:00:00:00:00:00
  2. PHP绕过disable_function限制(一)
  3. Mac下使用ABTestingGateway快速搭建灰度网关
  4. 汇编语言--8086CPU
  5. Android 四大组件之——Acitivity(四) Activity是如何加载显示内容的?
  6. ZYNQ中断示例修改
  7. nacos未授权访问漏洞【原理扫描】
  8. 进行判断使用class_记一次使用 Arthas 热更新线上代码
  9. python可视化库matplotlib_Python数据可视化库-Matplotlib(一)
  10. pytorch学习笔记(二十二):Pooling
  11. linux中pstree命令的含义,pstree命令--Linux命令应用大词典729个命令解读
  12. JAVA实现二叉树带权路径长度和_哈夫曼树的构建与最小带权路径长度
  13. Tomcat:开启Tomcat服务CMD窗口乱码
  14. 关于http-server的备选方案-- browser-sync
  15. 软件测试——文档测试
  16. 【信号处理】Python实现BPSK、QPSK、8PSK、8QAM、16QAM、64QAM的调制和解调
  17. 常见排序算法、查找算法(中英文命名)
  18. JSP开发模型(JavaWed)
  19. 来吧,我和你聊聊操作系统
  20. RN vivo访问相册失败 warning : User cancelled image selection

热门文章

  1. 【转】C 编译器优化过程中的 Bug
  2. 第 30 章 lvs-rrd
  3. 【Nodejs篇一】Node js 简介
  4. Python Set Literals
  5. 高效编程之互斥锁和自旋锁
  6. autumn 0.5.1 : Python Package Index
  7. php在线读取pdf文件大小_南公子私藏PDF神器曝光
  8. 【面试题】你知道为什么HashMap是线程不安全的吗?
  9. Markdown 学习笔记
  10. C语言goto语句的使用