标签:ScreenOrientation 适配 float height break Camera PersPective aspect camera
导语:
在屏幕适配之正交摄像机(Orthographic Camera)下游戏区域适配中我们学习了如何在正交摄像机下进行游戏区域的适配,下面我们来学习如何在透视摄像机下如何进行游戏区域的适配。
1.创建一个Camera(摄像机),将Camera的Projection设置为PersPective
2.设置Field of View 为10(开发者可根据需要自行调整FieldOfView的值)
3.上代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraAutoFix : MonoBehaviour { [SerializeField] private Transform background; private float designOrthographicSize; private float designHeight; private float designWidth; private float designAspect; private Camera _camera; public void Awake() { FixScreen(); } private void FixScreen() { ScreenOrientation designAutoRotation = Screen.orientation; _camera = this.gameObject.GetComponent<Camera>(); bool isOrthographic = _camera.orthographic; float aspect = Screen.width / (float)Screen.height; designHeight = 1920f; designWidth = 1080f; designAspect = designWidth / designHeight; if (isOrthographic) { OrthographicFix(designAutoRotation, aspect); return; } PerspectiveFix(designAutoRotation, aspect); } private void OrthographicFix(ScreenOrientation designAutoRotation, float aspect) { designOrthographicSize = 9.6f; float widthOrthographicSize = designOrthographicSize * designAspect; switch (designAutoRotation) { case ScreenOrientation.Portrait: if (aspect < designAspect) { _camera.orthographicSize = widthOrthographicSize / aspect; } if (aspect > designAspect) { _camera.orthographicSize = designOrthographicSize; //_camera.orthographicSize = designOrthographicSize * (aspect / designAspect); } break; case ScreenOrientation.AutoRotation: break; case ScreenOrientation.LandscapeLeft: break; case ScreenOrientation.LandscapeRight: break; case ScreenOrientation.PortraitUpsideDown: break; default: break; } } private void PerspectiveFix(ScreenOrientation designAutoRotation,float aspect) { float designFieldOfView = 10.0f; switch (designAutoRotation) { case ScreenOrientation.Portrait: if (aspect < designAspect) { _camera.fieldOfView = designFieldOfView * (designAspect / aspect); } break; case ScreenOrientation.AutoRotation: break; case ScreenOrientation.LandscapeLeft: break; case ScreenOrientation.LandscapeRight: break; case ScreenOrientation.PortraitUpsideDown: break; default: break; } if (background) { Vector3 scale = background.localScale; float scaleX = scale.x * (aspect / designAspect); background.localScale = new Vector3(scaleX * (_camera.fieldOfView / designFieldOfView), scaleX / aspect * (_camera.fieldOfView / designFieldOfView), scale.z); } } public struct CamereArea { public float height; public float width; public override string ToString() { return "(height:" + height + ";width:" + width + ")"; } } public CamereArea GetCameraAreaWithDistance(float distance) { float halfFOV = (_camera.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = _camera.aspect; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; CamereArea camereArea = new CamereArea() { height = height, width = width }; return camereArea; } public Vector3 ConvertWordPositionToCameraPosition(Vector3 wordPosition, float aimZ) { CamereArea camereArea = GetCameraAreaWithDistance(wordPosition.z - _camera.transform.position.z); CamereArea camereAreaAim = GetCameraAreaWithDistance(aimZ - _camera.transform.position.z); //Debug.Log("camereArea=" + camereArea + ",camereAreaAim=" + camereAreaAim); float heightRadio = camereAreaAim.height / (camereArea.height); float widthRadio = camereAreaAim.width / (camereArea.width); // Debug.Log("wordPosition:(" + wordPosition.x + "," + wordPosition.x + "," + wordPosition.z + ")"); // Debug.Log("heightRadio=" + heightRadio + ",widthRadio=" + widthRadio); Vector3 convertPosition = new Vector3(wordPosition.x * widthRadio, wordPosition.y * heightRadio, wordPosition.z); return convertPosition; } }
标签:ScreenOrientation,适配,float,height,break,Camera,PersPective,aspect,camera 来源: https://www.cnblogs.com/sy-liu/p/12753799.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。