如何在Android纵向模式下将WebcamTexture显示为全屏?

时间:2016-03-13 10:13:06

标签: unity3d unity3d-gui

我正在尝试在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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

如果您将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;

    }
}

让我知道您是否遇到任何问题。

答案 3 :(得分:-1)

您需要翻转并旋转它。如果你喜欢CameraCaptureKit(https://www.assetstore.unity3d.com/en/#!/content/56673),你会看到有不同的方法可以达到你想要的效果。在CameraCaptureKit中有一个自定义的RawImage组件,它可以使用RawImahge的UV坐标进行捣乱,而不是哄骗它。通过这种方式,您可以旋转图像90或270并将其翻转,而无需担心手机是以横向模式运行还是以任何方式运行。