这个拉伸可能只是我有的问题。。。先记录一下
首先检测有没有摄像机的权限
安卓平台:Permission.HasUserAuthorizedPermission(Permission.Camera)
ios:Application.HasUserAuthorization(UserAuthorization.WebCam)
但是这里我有个问题,哪位大神能告诉我下,为啥安卓检查的这个方法要是IEnumerator,我 void就不行。。。 也没啥返回值啊
string deviceName = cameraDevices[0].name;
camTexture = new WebCamTexture(deviceName);
showImage.canvasRenderer.SetTexture(camTexture);
camTexture.Play();
webCamTexture = camTexture;
问题就是showImage的大小 不能是ui自适应的屏幕大小,应该根据相机的尺寸去设置。
float cameraSizeRatio = (float)camTexture.width / (float)camTexture.height;
float screenRatio = (float)UnityEngine.Screen.width / (float)UnityEngine.Screen.height;
if (cameraSizeRatio < screenRatio)
{
showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width, UnityEngine.Screen.height * screenRatio / cameraSizeRatio);
}
else
{
showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width / screenRatio * cameraSizeRatio, UnityEngine.Screen.height);
}
就不拉伸了
附
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Android;
public class WebCameraController : MonoBehaviour
{
WebCamTexture camTexture;
public RawImage showImage;
/// <summary>
/// 已开启摄像头
/// </summary>
/// <returns></returns>
private bool hasUseCamera = false;
private void Update()
{
if (!hasUseCamera)
{
#if UNITY_IOS
StartCoroutine(CallCameraIOS());
#elif UNITY_ANDROID
// CallCameraAndriod();
StartCoroutine(CallCameraAndriod());
#else
// CallCameraAndriod();
StartCoroutine(CallCameraIOS());
#endif
}
}
public WebCamTexture webCamTexture;
///??? void 就不行 IEnumerator 就可以?????
IEnumerator CallCameraAndriod()
{
Debug.Log("CallCameraAndriod");
yield return null;
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}
if (Permission.HasUserAuthorizedPermission(Permission.Camera) && !hasUseCamera)
{
SetwebCam();
}
}
IEnumerator CallCameraIOS()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam) && !hasUseCamera)
{
SetwebCam();
}
}
private void SetwebCam()
{
Debug.Log("SetwebCam");
hasUseCamera = true;
if (camTexture != null)
camTexture.Stop();
WebCamDevice[] cameraDevices = WebCamTexture.devices;
if (cameraDevices.Length > 0)
{
string deviceName = cameraDevices[0].name;
Debug.Log(deviceName);
camTexture = new WebCamTexture(deviceName);
showImage.canvasRenderer.SetTexture(camTexture);
camTexture.Play();
webCamTexture = camTexture;
// Debug.Log("camTexture.width " + camTexture.width + " camTexture.height " + camTexture.height);
float showImageHeight = showImage.GetComponent<RectTransform>().sizeDelta.y;
float showImageWidth = showImage.GetComponent<RectTransform>().sizeDelta.x;
// Debug.Log("showImageWidth " + showImageWidth + " showImageHeight" + showImageHeight);
// Debug.Log("UnityEngine.Screen.width " + UnityEngine.Screen.width + " UnityEngine.Screen.height " + UnityEngine.Screen.height);
// showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.height * camTexture.width / camTexture.height, UnityEngine.Screen.height);
showImage.GetComponent<RectTransform>().sizeDelta = new Vector2(UnityEngine.Screen.width, UnityEngine.Screen.width / (float)camTexture.width * (float)camTexture.height);
}
else
{
Debug.LogError(" 没有连接的摄像头 ");
}
}
private void OnDestroy()
{
camTexture.Stop();
}
public WebCamTexture GetCamTexture()
{
return camTexture;
}
}