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

标签 unity3d unity3d-gui

我正在尝试在安卓人像模式下显示全屏网络摄像头纹理。 但是当我在我的设备上构建和测试它时,它的旋转和 videoRotationAngle 是 90

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

  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


}

最佳答案

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

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

只需确保将 Raw Image 的“Aspect Ratio Fitter”中的“Aspect Mode”设置为“Width Controls Height”或“Height Controls Width”(根据方向取大者)

代码片段

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;

    }
}

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

关于unity3d - 如何在 Android 纵向模式下将 WebcamTexture 显示为全屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35968995/

相关文章:

c# - 淡出 Unity UI 文本

android - Unity 5 UI Mask 问题 Android

c# - 如何停止此算法循环?

c# - 如何正确使用 CharacterController.Move() 来移动角色

c# - 方法可以拒绝 Vector2 和 Vector3 的错误类型吗

c# - 为什么在我的代码中将 json 值转换为字符串

ide - 你能推荐一个更好的 IDE 用于 Unity C# 编码吗?

unity3d - 我们必须统一上传不同分辨率的图标吗?

c# - 如何在unity3d中显示和保存表情符号?