unity2D平面摄像机滑动缩放
生活随笔
收集整理的這篇文章主要介紹了
unity2D平面摄像机滑动缩放
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
比如2D游戲 地圖很大,像植物大戰(zhàn)僵尸關(guān)卡選擇界面,需要對可見的區(qū)域進行滑動,縮放等,
1....滑動:通過正交攝像機的orthographicSize和位置和實際大小(可視范圍大小),計算出距離世界坐標(biāo)原點偏移量 來計算出位置
2....縮放,對正交相機的orthographicSize進行修改,達到放縮目的。同樣也要計算攝像機實際大小()可視范圍大小)
3.....正交相機大小的算法 ? 攝像機實際視界寬度(米)=size*2*屏幕寬高比 ?
當(dāng)然該方法不限于正交相機 透視相機也可以利用這種思路來計算,當(dāng)然還要考慮像素和米的換算關(guān)系,以下代碼塊像素米比例是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;//滑動接口,參數(shù)是偏移量void Slide(Vector2 dp){this.Slide(dp.x, dp.y);}//滑動接口,參數(shù)是偏移量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;}}// 設(shè)置x偏移量,偏移量是從世界坐標(biāo)原點開始計算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);}// 設(shè)置y偏移量,偏移量是從世界坐標(biāo)原點開始計算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);}}?
轉(zhuǎn)載于:https://my.oschina.net/kkkkkkkkkkkkk/blog/1537139
總結(jié)
以上是生活随笔為你收集整理的unity2D平面摄像机滑动缩放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用 docker 搭建 web 服务环
- 下一篇: 特征点的基本概念和如何找到它们