Unity 实时获取外部摄像头某个坐标的颜色

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

public class ColorGet : MonoBehaviour {
   
    public WebCamTexture cameraTexture;
    public string cameraName = "";
    public RawImage rawimage;

    private void Start()
    {
        StartCoroutine(PlayWebCam());
    }

    IEnumerator PlayWebCam()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            cameraTexture = new WebCamTexture(cameraName, 640, 480, 30);
            rawimage.texture = cameraTexture;         
            cameraTexture.Play();

           
        }
    }

    void Update()
    {
        if (!cameraTexture) return;
        Color textureCol = cameraTexture.GetPixel(320, 240)*255;
        Debug.Log(textureCol.ToString());
    }

}

 

2. 

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

public class ColorGet : MonoBehaviour
{

    public WebCamTexture cameraTexture;
    public string cameraName = "";
    public RawImage rawimage;
    public int webCameWidth = 640;
    public int webCameHeight = 480;

    private void Start()
    {
        StartCoroutine(PlayWebCam());
        rawimage.GetComponent().sizeDelta = new Vector2(webCameWidth, webCameHeight);
        rawimage.GetComponent().anchoredPosition = new Vector2(0, 0);
    }

    IEnumerator PlayWebCam()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            cameraTexture = new WebCamTexture(cameraName, webCameWidth, webCameHeight, 60);
            rawimage.texture = cameraTexture;
            cameraTexture.Play();


        }
    }

    void Update()
    {
        if (!cameraTexture) return;
        Color textureCol = cameraTexture.GetPixel(webCameWidth/2, webCameHeight/2) * 255;
        Debug.Log(textureCol.ToString());
    }

}

 

你可能感兴趣的:(unity)