unity打开移动摄像头,并自适应屏幕显示摄像头数据。兼容android和ios

跨平台并自适应显示摄像头数据
新建工程并建立UI

raw_Image的参数设置如下:

设置两个Canvas_UI和Canvas_Web的CamRender Mode都为Screen Space - Camera,并将Camera拖入。
设置Canvas_UI的层级高于Canvas_WebCam

编写脚本,并将脚本挂在Canvas_WebCam

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

public   class  MT_Camera  :  MonoBehaviour
{
    Transform  uiRoot;
    Transform  camRoot;
    RawImage  cam_Video;  
    Toggle  tog_togCamera;
    WebCamTexture  camTexture;
     public   int   w_cam   =   640 ;
     public   int   h_cam   =   480 ;    
      
     private   void   Awake()
     {
         uiRoot   =  GameObject .Find( "Canvas_UI" ).transform;
         camRoot   =  GameObject .Find( "Canvas_WebCam" ).transform;
     }
     private   void   Start()
     {
         cam_Video   =   camRoot.Find( "raw_Image" ).GetComponent< RawImage >();
         tog_togCamera   =   uiRoot.Find( "tog_ChangeCam" ).GetComponent< Toggle >();
         transform.GetComponent< CanvasScaler >().referenceResolution   =   new   Vector2 ( Screen .width,  Screen .height);
         tog_togCamera.onValueChanged.AddListener(changeCam);
         tog_togCamera.isOn   =   true ;
         ///自适应屏幕分辨率显示摄像头数据
         //宽度不变,缩放高度自适应显示摄像头数据
         //cam_Video.rectTransform.sizeDelta = new Vector2(h_cam * Screen.height / w_cam, Screen.width);
         //宽度不变,缩放宽度自适应显示摄像头数据
         cam_Video.rectTransform.sizeDelta   =   new  Vector2 ( Screen .height,   w_cam   *  Screen .width   /   h_cam);
     }
     void   changeCam( bool   isOn)
     {
         StartCoroutine(CallCamera(isOn));
     }
    IEnumerator  CallCamera( bool   isOn)
     {
         yield   return  Application .RequestUserAuthorization( UserAuthorization .WebCam);
         if   ( Application .HasUserAuthorization( UserAuthorization .WebCam))
         {
             if   (camTexture   !=   null )
                 camTexture.Stop();          
            WebCamDevice []   cameraDevices   =  WebCamTexture .devices;          
             string   deviceName   =   "" ;          
             for   ( int   i   =   0 ;   i   <   cameraDevices.Length;   i++)
             {
                 //如果是前置摄像机  
                 if   ( WebCamTexture .devices[i].isFrontFacing   &&   isOn)
                 {
                     deviceName   =  WebCamTexture .devices[i].name;
                     TurnCam(isOn);
                     break ;
                 }
                 //如果是后置摄像机  
                 else   if   (! WebCamTexture .devices[i].isFrontFacing   &&   !isOn)
                 {
                     deviceName   =  WebCamTexture .devices[i].name;
                     TurnCam(isOn);
                     break ;
                 }
             }
             camTexture   =   new  WebCamTexture (deviceName,   w_cam,   h_cam,   12 );
             cam_Video.texture   =   camTexture;          
             camTexture.Play();
         }
     }
     /// <summary>
     /// 翻转plane,正确显示摄像头数据
     /// </summary>
     /// <param name=" isOn ">If set to <c>true</c> is turn.</param>
     public   void   TurnCam( bool   isOn)
     {
         #if   UNITY_IOS   ||   UNITY_IPHONE
        if (!isOn)
            cam_Video.rectTransform.localEulerAngles = new Vector3(180, 0, 90);
        else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 0, -90);
         #elif   UNITY_ANDROID
         if   (!isOn)
             cam_Video.rectTransform.localEulerAngles   =   new  Vector3 ( 180 ,   180 ,   90 );
         else   cam_Video.rectTransform.localEulerAngles   =   new  Vector3 ( 0 ,   180 ,   90 );
         #endif
     }
}

分别发布到xcode和android测试

项目下载链接:https://download.csdn.net/download/qq_16929759/10421612

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值