日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unity2D平面摄像机滑动缩放

發布時間:2025/5/22 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity2D平面摄像机滑动缩放 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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平面摄像机滑动缩放的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 老司机深夜福利网站 | 日韩mv欧美mv国产网站 | 精品国产乱码久久久久久免费 | 亚洲图片二区 | 亚洲精品少妇 | 国产精品污www在线观看 | 国产一区视频在线观看免费 | 久久成年人视频 | 国产无遮挡又黄又爽 | 在线观看日韩国产 | 超碰97国产| 天堂在线观看 | 色女生影院 | 成人做爰视频www网站小优视频 | 亚洲伊人婷婷 | 欧美精品 日韩 | 久久麻豆视频 | 国产欧美精品在线观看 | 9191在线视频| 亲子乱aⅴ一区二区三区 | 兔费看少妇性l交大片免费 日韩高清不卡 | 狠狠人妻久久久久久综合 | 亚洲综合在线视频 | 涩涩综合 | 日韩成人久久 | 喷水了…太爽了高h | 成人午夜免费观看 | 老外一级片 | wwxx日本 | 国产成人91 | 日产精品久久久一区二区 | 97se亚洲综合 | 欧美11一13sex性hd | 扒开jk护士狂揉免费 | 久久国产人妻一区二区免色戒电影 | 午夜88 | 亚洲啊v在线 | 99av视频| 久久一二三四区 | 日本亚洲网站 | 亚洲aⅴ在线观看 | 成人免费视频播放 | 欧美性受xxxx黑人xyx | 美女调教视频 | 蜜桃又黄又粗又爽av免 | 亚洲免费福利视频 | 怡红院毛片| 亚洲人网 | 岛国av噜噜噜久久久狠狠av | 嫩草影院av | 欧美少妇15p | 深夜福利av | 一本一道久久a久久精品综合 | 成人免费精品视频 | jzzijzzij亚洲成熟少妇在线播放 狠狠躁日日躁夜夜躁2022麻豆 | 欧美黑吊大战白妞欧美大片 | 四虎黄色网 | 亚洲精品乱码久久久久99 | 性色av一区二区三区免费 | 一级黄色录相 | 最新理伦片eeuss影院 | 欧美男同又粗又长又大 | 乱淫的女高中暑假调教h | 精品无码人妻一区二区免费蜜桃 | 欧美日韩一级二级三级 | 另类专区亚洲 | 欧洲美熟女乱又伦 | 麻豆传媒一区二区三区 | 天堂网www| 一级网站在线观看 | 亚洲欧美高清视频 | 成人h在线 | 亚洲午夜视频在线 | 国产精品嫩草影院桃色 | 成人午夜福利视频 | 免费h漫禁漫天天堂 | 五月天激情啪啪 | 国产亚洲成人精品 | 午夜特片网| 国内9l自拍 | 色呦呦国产精品 | 免费亚洲精品 | 午夜伦理在线观看 | 成人小视频免费在线观看 | 用力使劲高潮了888av | 免费在线观看黄色网址 | 免费观看毛片 | 成人av资源| 久久精品免费一区二区 | 久久久久国产精品夜夜夜夜夜 | 精品国产乱码久久久久久闺蜜 | 国产精品夜夜爽 | 日本久久精品 | 亚洲午夜精品久久久 | 久久久久亚洲av无码a片 | 人妻精品久久久久中文字幕69 | 亚洲成人中文字幕在线 | 国产微拍一区 | 在线观看av的网站 |