如何从 phone 摄像机获取 Live Feed 到 Unity

How to get Live feed from phone camera to Unity

我需要从 phone 到 unity 的实时摄像头。我已经尝试过 webcamTexture,但它无法按照我想要的方式工作。它放大太多了,我每次都必须构建 运行 才能看到我对分辨率所做的更改的结果。此外,如果有人是专家并帮助我为我的学位建立我的最后一年项目,我将不胜感激。 这是我用于使用相机的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CameraScript : MonoBehaviour
{
    private bool camAvailable;
    private WebCamTexture backCam;
    private Texture defaultBackground;
    public RawImage background;
    public AspectRatioFitter fit;
    public int width = 1080;
    public int height = 2400;
    // Start is called before the first frame update
    void Start()
    {
        defaultBackground = background.texture;
        WebCamDevice[] devices = WebCamTexture.devices;
        if(devices.Length ==0)
        {
            Debug.Log("No Camera Available");
            camAvailable = false;
            return;
        }
        for (int i =0; i<devices.Length; i++)
        {
            if(!devices[i].isFrontFacing)
            {
                backCam = new WebCamTexture(devices[i].name, width, height);
            }

        }
        if(backCam == null)
        {
            Debug.Log("Unable to find the Back Camera");
            return;
        }
        backCam.Play();
        background.texture = backCam;
        camAvailable = true;
       
    }

    // Update is called once per frame
    void Update()
    {
        if (!camAvailable)
            return;
        float ratio = (float)backCam.width / (float)backCam.height;
        fit.aspectRatio = ratio;
        float scaleY = backCam.videoVerticallyMirrored ? -1f: 1f;
        background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
        int orient = -backCam.videoRotationAngle;
        background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
        
    }

这只是数学问题,我可以与您分享此脚本以进行正确计算。多年来它已经在许多项目中使用。

你只需要指定一个四边形,四边形就会在你的主相机的远背景平面上。

类似的想法也可以应用于 UI Rect。 理论上你应该可以实现这个result(video call testing in C#).

void CalculateBackgroundQuad()
{
    Camera cam = Camera.main;
    ScreenRatio = (float)Screen.width / (float)Screen.height;

    BackgroundQuad.transform.SetParent(cam.transform);
    BackgroundQuad.transform.localPosition = new Vector3(0f, 0f, cam.farClipPlane / 2f);

    float videoRotationAngle = webCamTexture.videoRotationAngle;

    BackgroundQuad.transform.localRotation = baseRotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.forward);

    float distance = cam.farClipPlane / 2f;
    float frustumHeight = 2.0f * distance * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);

    BackgroundQuad.transform.localPosition = new Vector3(0f, 0f, distance);
    Vector3 QuadScale = new Vector3(1f, frustumHeight, 1f);

    //adjust the scaling for portrait Mode & Landscape Mode
    if (videoRotationAngle == 0 || videoRotationAngle == 180)
    {
        //landscape mode
        TextureRatio = (float)(webCamTexture.width) / (float)(webCamTexture.height);
        if (ScreenRatio > TextureRatio)
        {
            float SH = ScreenRatio / TextureRatio;
            float TW = TextureRatio * frustumHeight * SH;
            float TH = frustumHeight * (webCamTexture.videoVerticallyMirrored ? -1 : 1) * SH;
            QuadScale = new Vector3(TW, TH, 1f);
        }
        else
        {
            float TW = TextureRatio * frustumHeight;
            QuadScale = new Vector3(TW, frustumHeight * (webCamTexture.videoVerticallyMirrored ? -1 : 1), 1f);
        }
    }
    else
    {
        //portrait mode
        TextureRatio = (float)(webCamTexture.height) / (float)(webCamTexture.width);
        if (ScreenRatio > TextureRatio)
        {
            float SH = ScreenRatio / TextureRatio;
            float TW = frustumHeight * -1f * SH;
            float TH = TW * (webCamTexture.videoVerticallyMirrored ? 1 : -1) * SH;
            QuadScale = new Vector3(TW, TH, 1f);
        }
        else
        {
            float TW = TextureRatio * frustumHeight;
            QuadScale = new Vector3(frustumHeight * -1f, TW * (webCamTexture.videoVerticallyMirrored ? 1 : -1), 1f);
        }
    }
    BackgroundQuad.transform.localScale = QuadScale;
}