跨平台并自适应显示摄像头数据
新建工程并建立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