2

我正在尝试在 android Portrait 模式下显示全屏 Webcamtexture。但是当我在我的设备上构建和测试它时,它的旋转和 videoRotationAngle 是 90

我使用 RawImage 作为纹理。我在一些帖子上看到您必须旋转变换并获得正确的角度。但问题是,如果我旋转 RawImage UI,它将不再是全屏视图。

  var camImage:UnityEngine.UI.RawImage;
  var baseRotation:Quaternion;
  var webcamTexture:WebCamTexture;
  var rotation:UnityEngine.UI.Text;


function Start () {

         webcamTexture = new WebCamTexture(Screen.width,Screen.height);
         camImage.texture=webcamTexture;
         camImage.material.mainTexture = webcamTexture;
         webcamTexture.Play();
         rotation.text= webcamTexture.videoRotationAngle.ToString(); // to check the angle


}
4

4 回答 4

3

我知道这是迟到的回复,但可能会帮助面临类似问题的人。

如果您使用 RawImage 作为纹理,下面的代码应该可以帮助您在 Potrait 和 Landscape 模式下实现渲染。

只需确保将原始图像的“纵横比调整器”中的“纵横比模式”设置为“宽度控制高度”或“高度控制宽度”(根据方向以较大者为准)

代码片段

using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.Collections;

public class DeviceCameraController : MonoBehaviour
{
    public RawImage image;
    public AspectRatioFitter imageFitter;

    //set it to either FRONT or BACK
    string myCamera = "BACK";

    // Device cameras
    WebCamDevice frontCameraDevice;
    WebCamDevice backCameraDevice;
    WebCamDevice activeCameraDevice;

    WebCamTexture frontCameraTexture;
    WebCamTexture backCameraTexture;
    WebCamTexture activeCameraTexture;

    // Image rotation
    Vector3 rotationVector = new Vector3(0f, 0f, 0f);

    // Image uvRect
    Rect defaultRect = new Rect(0f, 0f, 1f, 1f);
    Rect fixedRect = new Rect(0f, 1f, 1f, -1f);

    // Image Parent's scale
    Vector3 defaultScale = new Vector3(1f, 1f, 1f);
    Vector3 fixedScale = new Vector3(-1f, 1f, 1f);

    void Start()
    {
        // Check for device cameras
        if (WebCamTexture.devices.Length == 0)
        {
            Debug.Log("No devices cameras found");
            return;
        }

        // Get the device's cameras and create WebCamTextures with them
        frontCameraDevice = WebCamTexture.devices.Last();
        backCameraDevice = WebCamTexture.devices.First();

        frontCameraTexture = new WebCamTexture(frontCameraDevice.name);
        backCameraTexture = new WebCamTexture(backCameraDevice.name);

        // Set camera filter modes for a smoother looking image
        frontCameraTexture.filterMode = FilterMode.Trilinear;
        backCameraTexture.filterMode = FilterMode.Trilinear;

        // Set the camera to use by default
        if (myCamera.Equals("FRONT"))
            SetActiveCamera(frontCameraTexture);
        else if (myCamera.Equals("BACK"))
            SetActiveCamera(backCameraTexture);
        else // default back
            SetActiveCamera(backCameraTexture);
    }

    // Set the device camera to use and start it
    public void SetActiveCamera(WebCamTexture cameraToUse)
    {
        if (activeCameraTexture != null)
        {
            activeCameraTexture.Stop();
        }

        activeCameraTexture = cameraToUse;
        activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device =>
            device.name == cameraToUse.deviceName);

        image.texture = activeCameraTexture;
        image.material.mainTexture = activeCameraTexture;

        activeCameraTexture.Play();
    }

    // Make adjustments to image every frame to be safe, since Unity isn't 
    // guaranteed to report correct data as soon as device camera is started
    void Update()
    {
        // Skip making adjustment for incorrect camera data
        if (activeCameraTexture.width < 100)
        {
            Debug.Log("Still waiting another frame for correct info...");
            return;
        }

        // Rotate image to show correct orientation 
        rotationVector.z = -activeCameraTexture.videoRotationAngle;
        image.rectTransform.localEulerAngles = rotationVector;

        // Set AspectRatioFitter's ratio
        float videoRatio =
            (float)activeCameraTexture.width / (float)activeCameraTexture.height;
        imageFitter.aspectRatio = videoRatio;

        // Unflip if vertically flipped
        image.uvRect =
            activeCameraTexture.videoVerticallyMirrored ? fixedRect : defaultRect;

    }
}

如果您遇到任何问题,请告诉我。

于 2019-04-27T08:17:34.927 回答
1

好吧,您有两个选择:您可以选择纵向或横向并阻止屏幕旋转,或者您可以自动旋转对象并根据屏幕边界拉伸它。有关详细信息,请参阅 Screen.height 和 Screen.length。

于 2016-03-13T13:33:32.823 回答
0

(mCamera.videoRotationAngle + 90) % 360将正确旋转纹理,然后分配一个localScale交换纹理的高度和宽度以固定大小。

于 2017-05-19T05:39:12.850 回答
-1

你需要翻转和旋转它。如果您喜欢 CameraCaptureKit ( https://www.assetstore.unity3d.com/en/#!/content/56673 ),您会发现有不同的方法可以实现您想要的。在 CameraCaptureKit 中,有一个自定义的 RawImage 组件,它本质上是摆弄 RawImahge 的 UV 坐标,而不是旋转它。这样,您可以将图像旋转 90 度或 270 度并翻转它,而无需担心手机是否以横向模式运行或其他任何情况。

于 2016-05-03T20:26:40.923 回答