关于unity 开发android手机 相机自动对焦问题的代码
发表于2016-01-24
发现好多朋友都碰到这个问题,这是我在国外网站发现的代码,并亲自测试过可用。大家可以Copy使用,同时如果有更好的方法欢迎大家交流。
代码如下:
private bool autoFocusSet;
void Awake()
{
autoFocusSet = false;
}
public static bool enableAutoFocus()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass metaioSDKAndroid = new AndroidJavaClass("com.metaio.sdk.jni.IMetaioSDKAndroid");
object[] args = {currentActivity};
AndroidJavaObject camera = metaioSDKAndroid.CallStatic<AndroidJavaObject>("getCamera",args);
if(camera != null)
{
AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
object[] focusMode = {cameraParameters.GetStatic<string>("FOCUS_MODE_CONTINUOUS_PICTURE")};
cameraParameters.Call("setFocusMode",focusMode);
object[] newParameters = {cameraParameters};
camera.Call("setParameters",newParameters);
return true;
}
else
{
Debug.LogError("metaioSDK.enableAutoFocus:Camera not available");
return false;
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))
{
Application.Quit();
}
if(Time.time > 1f && !autoFocusSet)
{
autoFocusSet = enableAutoFocus();
}
}