1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using UnityEngine;
- namespace XGame.Framework.Map
- {
- /// <summary>
- /// 相机的fieldOfView设置参数
- /// 相机默认以高度自适应,以保证垂直方向的显示内容一致,
- /// 竖屏游戏地图需要固定宽度以保证水平方向的显示内容一致,
- /// 横屏游戏不需要该设置
- /// </summary>
- [Serializable]
- public struct CameraFieldOfViewOptions
- {
- /// <summary>
- /// 是否固定宽度
- /// Camera.FieldOfViewAxis.Horizontal
- /// </summary>
- [Header("是否固定宽度")]
- public bool isLockHorizontal;
- /// <summary>
- /// orthographicSize参考值,正交相机有效
- /// 修正时以该值为基础根据分辨率重新计算
- /// </summary>
- [Header("orthographicSize参考值,正交相机有效")]
- public float size;
- /// <summary>
- /// fov参考值,透视相机有效
- /// 取相机的FOVAxis==Horizontal时FieldOfView的值
- /// 赋值完相机需要设置为FOVAxis=Vertical
- /// 相机视口度数
- /// </summary>
- [Header("fov参考值,透视相机有效")]
- public float fieldOfView;
- ///// <summary>
- ///// 地图相机的设计分辨率
- ///// 只用来计算长宽比
- ///// </summary>
- //[Header("地图相机的设计分辨率,只用来计算长宽比")]
- //public Vector2Int designSize;
- /// <summary>
- /// 修正fov
- /// 以相机的FOVAxis==Vertical计算
- /// </summary>
- /// <param name="camera"></param>
- public void FixFieldOfView(Camera camera)
- {
- if (!isLockHorizontal)
- return;
- if (camera.orthographic)
- {
- camera.orthographicSize = size / camera.aspect * 0.5f;
- }
- else
- {
- var target = Mathf.Tan(fieldOfView * Mathf.PI / 360f) / camera.aspect;
- camera.fieldOfView = Mathf.RoundToInt(Mathf.Atan(target) * Mathf.Rad2Deg * 2);
- }
- }
- }
- }
|