Ejemplo n.º 1
0
    //根据运行环境调整相机UI的旋转镜像缩放,这段简单的代码无法覆盖所有情况
    public void SelfAdjusSize()
    {
        Vector2 targetResolution  = RawImg_BackGroud.canvas.GetComponent <CanvasScaler>().referenceResolution;
        Vector2 currentResolution = NatCam.Camera.PreviewResolution;

#if !UNITY_IOS || UNITY_EDITOR || UNITY_STANDALONE
        if (DispatchUtility.Orientation == Orientation.Rotation_0 || DispatchUtility.Orientation == Orientation.Rotation_180)
        {
            RawImg_BackGroud.rectTransform.sizeDelta = new Vector2(targetResolution.y * currentResolution.x / currentResolution.y, targetResolution.y);
        }
        else
        {
            RawImg_BackGroud.rectTransform.sizeDelta = new Vector2(targetResolution.y, targetResolution.y * currentResolution.y / currentResolution.x);
        }

        if (NatCam.Camera.Facing == Facing.Front)
        {
#if UNITY_ANDROID
            NatCam.SetFlipx(false);
            NatCam.SetFlipy(false);
#endif
            RawImg_BackGroud.rectTransform.rotation = baseRotation * Quaternion.AngleAxis((int)DispatchUtility.Orientation * 90, Vector3.forward);
#if UNITY_EDITOR || UNITY_STANDALONE
            RawImg_BackGroud.uvRect = new Rect(1, 0, -1, 1);    //镜像处理
#else
            RawImg_BackGroud.uvRect = new Rect(0, 0, 1, 1);
#endif
        }
        else
        {
#if UNITY_ANDROID
            NatCam.SetFlipx(true);
            NatCam.SetFlipy(false);
#endif
            RawImg_BackGroud.rectTransform.rotation = baseRotation * Quaternion.AngleAxis((int)DispatchUtility.Orientation * 90, Vector3.back);
#if UNITY_EDITOR || UNITY_STANDALONE
            RawImg_BackGroud.uvRect = new Rect(0, 0, 1, 1);
#else
            RawImg_BackGroud.uvRect = new Rect(0, 1, 1, -1);            //镜像处理
#endif
        }
#else
        NatCam.SetFlipx(false);
        NatCam.SetFlipy(false);
        RawImg_BackGroud.rectTransform.sizeDelta = new Vector2(targetResolution.y * currentResolution.y / currentResolution.x, targetResolution.y);
#endif
    }