wangguan
2020-12-16 fe59d1a97e80284315bcdfa18a49059b38dfd137
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Fitter : MonoBehaviour
{
    private Camera myCamera;
    private Camera myCameraUI;
    private Camera myCamera3D;
 
    private float width = 1080f;
    private float height = 1920f;
 
    private float size = 64f;//在1080*1920情况下的大小
    private float ratio;
    // Start is called before the first frame update
    void Awake()
    {
        ratio = width / height;
 
        myCamera = GetComponent<Camera>();
        myCameraUI = GameObject.Find("UICamera").GetComponent<Camera>();
        myCamera3D = GameObject.Find("SceneCamera3D").GetComponent<Camera>();
 
        Calculate();
        //myCamera.orthographicSize = myCamera.orthographicSize * 1920 / 1080 * Screen.height / Screen.width;
 
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            Calculate();
        }
    }
 
    void Calculate()
    {
        float tmpRatio = (float)Screen.width / (float)Screen.height;
        float newRatio = ratio - tmpRatio + 1.0f;
        Debug.Log($"开始变化大小,ratio:{ratio}  tmpRatio:{tmpRatio}  newRatio{newRatio}");
        myCamera.orthographicSize = size * newRatio;
        if (myCameraUI != null){
            myCameraUI.orthographicSize = size * newRatio;
        }
        if (myCamera3D != null)
        {
            myCamera3D.fieldOfView = 60 * newRatio;
        }
    }
}