zoukankan      html  css  js  c++  java
  • Unity调用外部摄像头,全屏显示摄像头画面

    有两种方法,常用的是GUI方法,代码如下:

    public class CameraTest : MonoBehaviour {
    
        WebCamTexture camTexture;
    
        void Start () {
            StartCoroutine(CallCamera());
        }
    
        IEnumerator CallCamera()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                if (camTexture != null)
                    camTexture.Stop();
    
                WebCamDevice[] cameraDevices = WebCamTexture.devices;
    
                string deviceName = cameraDevices[0].name;
                Debug.Log(deviceName);
    
                camTexture = new WebCamTexture(deviceName);
                camTexture.Play();
            }
        }
    
    
        void OnGUI()
        {
            if(camTexture!=null)
            {
                GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), camTexture, ScaleMode.StretchToFill);
           }
        }
    }
    

      

    GUI已经被抛弃,效率,DrawCall,适配等各种差,现使用UGUI实现:

    public class CameraTest : MonoBehaviour {
    
        WebCamTexture camTexture;
    
        Image img;
    
        public GameObject quad;
    
        void Start () {
            img = GetComponentInChildren<Image>();
            StartCoroutine(CallCamera());
        }
    
        IEnumerator CallCamera()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                if (camTexture != null)
                    camTexture.Stop();
    
                WebCamDevice[] cameraDevices = WebCamTexture.devices;
    
                string deviceName = cameraDevices[0].name;
                Debug.Log(deviceName);
    
                camTexture = new WebCamTexture(deviceName);
                img.canvasRenderer.SetTexture(camTexture); //注意改行代码
                camTexture.Play();
            }
        }
    
    }
    

      

  • 相关阅读:
    OBJ文件格式详解
    HashMap的用法
    HashMap和Hashtable的区别
    加载物体的方法
    drawSelf(int texId)格式对应
    adb.exe诊断
    Android Eclipse如何用BlueStacks模拟器
    .md5mesh and .md5anim文件介绍
    ubuntu命令行下中文乱码的解决方案 (我采取了其中方案一与方案二,都还没成功—待定)
    Ubuntu下小巧智能的代码编辑器Scribes
  • 原文地址:https://www.cnblogs.com/scotly/p/5282606.html
Copyright © 2011-2022 走看看