using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameControl : MonoBehaviour
{
[Tooltip("Camera的全屏Quad背景")]
private CameraBackground _cameraBackground;
[Tooltip("Camera的filedOfView")]
private float _fieldOfView = -1f;
[Tooltip("Camera的FieldOfView是否根据屏幕尺寸适配")]
[SerializeField]
private bool _isExpand = false;
[Tooltip("Camera的filedOfView")]
private Camera _camera;
public float ScreenAspectRatio
{
get
{
return (float)Screen.width / (float)Screen.height;
}
}
public float DesignAspectRatio
{
get
{
return 1280f / 720f;
}
}
public float ScaleFactor
{
get
{
if (ScreenAspectRatio < DesignAspectRatio)
{
return ScreenAspectRatio / DesignAspectRatio;
}
else
{
return 1f;
}
}
}
void Awake()
{
_camera = gameObject.GetComponent<Camera>();
_cameraBackground = gameObject.GetComponentInChildren<CameraBackground>();
}
private void Update()
{
if(_isExpand)
{
if (_fieldOfView < 0)
{
_fieldOfView = _camera.fieldOfView;
}
_camera.fieldOfView = _fieldOfView / ScaleFactor;
}
else if (_fieldOfView >= 0)
{
_camera.fieldOfView = _fieldOfView;
}
if (_cameraBackground != null)
{
_cameraBackground.resetSize();
}
}
}
源码链接:https://download.csdn.net/download/qq_30058057/21735799
|