getNumberOfCameras是静态方法,因此您需要CallStatic您的“ cameraClass”
在“ android.hardware.Camera”中没有“ getCamera”方法(或者至少我在文档中找不到).您应该改用“ Camera.open”,但是有一个窍门. ‘Camera.open’要求您的应用程序具有相机permises,除非您使用内置相机功能,否则它们不会由Unity生成.
以下适用于Android ICS:
using UnityEngine;
#if UNITY_ANDROID
public class CameraParametersAndroid
{
public static float HorizontalViewAngle { get; protected set; }
public static float VerticalViewAngle { get; protected set; }
public static int numCameras { get; protected set; }
static CameraParametersAndroid()
{
AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");
numCameras = cameraClass.CallStatic("getNumberOfCameras");
// This is an ugly hack to make Unity
// generate Camera permisions
WebCamDevice[] devices = WebCamTexture.devices;
// Camera.open gets back-facing camera by default
// you should check for exceptions
int camID = 0;
AndroidJavaObject camera = cameraClass.CallStatic("open", camID);
// I'm pretty sure camera will never be null at this point
// It will either be a valid object or Camera.open would throw an exception
if (camera != null)
{
AndroidJavaObject cameraParameters = camera.Call("getParameters");
HorizontalViewAngle = cameraParameters.Call("getHorizontalViewAngle");
VerticalViewAngle = cameraParameters.Call("getVerticalViewAngle");
}
else
{
Debug.LogError("[CameraParametersAndroid] Camera not available");
}
}
}
#endif