关于Unity3D的PC游戏窗口化时的分辨率解决方案

 void Start()
    {
        //16:9任意拉伸
        aspect16_9 = 1.777777778f;
        Camera.main.aspect = aspect16_9;
    }

  private void OnPreRender()
    {
        //窗口化中拉伸窗口时等比例缩放以保持16:9
        float currentAspect = System.Convert.ToSingle((double)Screen.width / Screen.height);
        if (currentAspect > Camera.main.aspect)
        {
            Camera.main.pixelRect = new Rect(0, 0, Screen.height * 16 / 9.0f, Screen.height);
            Camera.main.pixelRect = new Rect(Screen.width / 2 - Camera.main.pixelWidth / 2, Screen.height / 2 - Camera.main.pixelHeight / 2, Camera.main.pixelWidth, Camera.main.pixelHeight);
        }
        else if (currentAspect < Camera.main.aspect)
        {
            Camera.main.pixelRect = new Rect(0, 0, Screen.width, Screen.width / 16.0f * 9);
            Camera.main.pixelRect = new Rect(Screen.width / 2 - Camera.main.pixelWidth / 2, Screen.height / 2 - Camera.main.pixelHeight / 2, Camera.main.pixelWidth, Camera.main.pixelHeight);
        }
    }

你可能感兴趣的:(关于Unity3D的PC游戏窗口化时的分辨率解决方案)