场景Camera根据Canvas宽高自适应

using UnityEngine;

public class CameraFieldOfView : MonoBehaviour
{
    private Camera m_Camera;

    //默认宽高
    public float m_DefaultWidth = 1920;
    public float m_DefaultHeight = 1080;

    void Start()
    {
        float height = Screen.height;
        float width = Screen.width;

        m_Camera = this.GetComponent<Camera>();

        //宽高比发生变化
        if (height / width != m_DefaultHeight / m_DefaultWidth)
        {
            float temp_width = m_DefaultWidth * (height / m_DefaultHeight);
            float width_scale = (temp_width - width) / width;
            m_Camera.fieldOfView = m_Camera.fieldOfView + m_Camera.fieldOfView * width_scale;
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容