Vufroia相机对焦问题

一,
将此代码写入新的脚本,然后放在Camera下即可(用过一次不错)

  1. using UnityEngine;
  2. using System.Collections;

    1. public class CameraMode : MonoBehaviour
  3. {
  4. // Use this for initialization
  5. void Start()
  6. { cusMode.FOCUS_MODE_CONTINUOUSAUTO);
  7. }

    1. // Update is called once per frame
  8. void Update()
  9. {
  10. #if UNITY_EDITOR
  11. if(Input.GetMouseButtonUp(0))
  12. #elif UNITY_ANDROID || UNITY_IPHONE
  13. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  14. #endif
  15. {
  16. Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
  17. }
  18. }
  19. }

二,
在使用vuforia sdk制作AR时候有时候会遇到的问题就是相机不对焦,相机对着一个图片时候在屏幕中显示的非常不清晰,之前的文字中也介绍了AR的相机对焦功能。相机对焦文章,新版本4.x和3.x对焦代码不太一样,比较简单贴一下
3.x的

  1. void Start () {
  2. CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
  3. }

经过测试,完全是可以实现对焦的。
4.x

  1. void Start () {
  2. Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

    1. }

也是经过测试可以完全实现AR相机的对焦功能。
代码已经说明,最近的就是新建一个脚本,如果使用Unity开发直接把脚本放到ARCamera组件上就可以。

  1. /*==============================================================================
    • Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
    • ==============================================================================*/
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;

    1. ///
  6. /// UI Event Handler class that handles events generated by user-tap actions
  7. /// over the UI Options Menu
  8. ///
  9. public class ImageTargetUIEventHandler : ISampleAppUIEventHandler {

    1. #region PUBLIC_MEMBER_VARIABLES
  10. public override event System.Action CloseView;
  11. public override event System.Action GoToAboutPage;
  12. #endregion PUBLIC_MEMBER_VARIABLES

    1. #region PRIVATE_MEMBER_VARIABLES
  13. private static bool sExtendedTrackingIsEnabled;
  14. private ImageTargetUIView mView;
  15. private bool mCameraFacingFront;
  16. #endregion PRIVATE_MEMBER_VARIABLES

    1. #region PUBLIC_MEMBER_PROPERTIES
  17. public ImageTargetUIView View
  18. {
  19. get {
  20. if(mView == null){
  21. mView = new ImageTargetUIView();
  22. mView.LoadView();
  23. }
  24. return mView;
  25. }
  26. }

    1. ///
  27. /// Currently, there is no mechanism to query the SDK to know whether or not extended tracking is enabled/disabled.
  28. /// Therefore, it needs to be handled at the app layer.
  29. ///
  30. public static bool ExtendedTrackingIsEnabled
  31. {
  32. get {
  33. return sExtendedTrackingIsEnabled;
  34. }
  35. }

    1. #endregion PUBLIC_MEMBER_PROPERTIES

    1. #region PUBLIC_METHODS
  36. public override void UpdateView (bool tf)
  37. {
  38. this.View.UpdateUI(tf);
  39. }

    1. public override void Bind()
  40. {
  41. this.View.mExtendedTracking.TappedOn += OnTappedToTurnOnTraking;
  42. this.View.mCameraFlashSettings.TappedOn += OnTappedToTurnOnFlash;
  43. this.View.mAutoFocusSetting.TappedOn += OnTappedToTurnOnAutoFocus;
  44. this.View.mCameraFacing.TappedOnOption += OnTappedToTurnCameraFacing;
  45. this.View.mDataSet.TappedOnOption += OnTappedOnDataSet;
  46. this.View.mCloseButton.TappedOn += OnTappedOnCloseButton;
  47. this.View.mAboutLabel.TappedOn += OnTappedOnAboutButton;
  48. EnableContinuousAutoFocus();
  49. }

    1. public override void UnBind()
  50. {
  51. this.View.mExtendedTracking.TappedOn -= OnTappedToTurnOnTraking;
  52. this.View.mCameraFlashSettings.TappedOn -= OnTappedToTurnOnFlash;
  53. this.View.mAutoFocusSetting.TappedOn -= OnTappedToTurnOnAutoFocus;
  54. this.View.mCameraFacing.TappedOnOption -= OnTappedToTurnCameraFacing;
  55. this.View.mDataSet.TappedOnOption -= OnTappedOnDataSet;
  56. this.View.mCloseButton.TappedOn -= OnTappedOnCloseButton;
  57. this.View.mAboutLabel.TappedOn -= OnTappedOnAboutButton;
  58. sExtendedTrackingIsEnabled = false;
  59. this.View.UnLoadView();
  60. mView = null;
  61. }

    1. public override void TriggerAutoFocus()
  62. {
  63. StartCoroutine(TriggerAutoFocusAndEnableContinuousFocusIfSet());
  64. }

    1. public override void SetToDefault(bool tf)
  65. {
  66. this.View.mCameraFlashSettings.Enable(tf);
  67. }

    1. #endregion PUBLIC_METHODS

    1. #region PRIVATE_METHODS

    1. ///
  68. /// Activating trigger autofocus mode unsets continuous focus mode (if was previously enabled from the UI Options Menu)
  69. /// So, we wait for a second and turn continuous focus back on (if options menu shows as enabled)
  70. ///
  71. private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet()
  72. {
  73. //triggers a single autofocus operation
  74. if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO)) {
  75. this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO;
  76. }

    1. yield return new WaitForSeconds(1.0f);

    1. //continuous focus mode is turned back on if it was previously enabled from the options menu
  77. if(this.View.mAutoFocusSetting.IsEnabled)
  78. {
  79. if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO)) {
  80. this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO;
  81. }
  82. }

    1. Debug.Log (this.View.FocusMode);

    1. }

    1. private void OnTappedOnAboutButton(bool tf)
  83. {
  84. if(this.GoToAboutPage != null)
  85. {
  86. this.GoToAboutPage();
  87. }
  88. }

    1. private void OnTappedToTurnOnTraking(bool tf)
  89. {
  90. if(!ExtendedTracking(tf))
  91. {
  92. this.View.mExtendedTracking.Enable(false);
  93. ImageTargetUIEventHandler.sExtendedTrackingIsEnabled = false;
  94. }
  95. else
  96. {
  97. ImageTargetUIEventHandler.sExtendedTrackingIsEnabled = tf;
  98. this.View.mExtendedTracking.Enable(tf);
  99. // to better demostrate the effect, we switch the augmentation models - a teapot is used for normal tracking,
  100. // a skyscraper for extended tracking.
  101. SwitchModels(tf);
  102. }
  103. OnTappedToClose();
  104. }

    1. private void OnTappedToTurnOnFlash(bool tf)
  105. {
  106. if(tf)
  107. {
  108. if(!CameraDevice.Instance.SetFlashTorchMode(true) || mCameraFacingFront)
  109. {
  110. this.View.mCameraFlashSettings.Enable(false);
  111. }
  112. }
  113. else
  114. {
  115. CameraDevice.Instance.SetFlashTorchMode(false);
  116. }

    1. Debug.Log (“CameraFacingFront = ” + mCameraFacingFront);
  117. OnTappedToClose();
  118. }

    1. //We want autofocus to be enabled when the app starts
  119. private void EnableContinuousAutoFocus()
  120. {
  121. if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
  122. {
  123. this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO;
  124. this.View.mAutoFocusSetting.Enable(true);
  125. }
  126. }

    1. private void OnTappedToTurnOnAutoFocus(bool tf)
  127. {
  128. if(tf)
  129. {
  130. if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
  131. {
  132. this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO;
  133. }
  134. else
  135. {
  136. this.View.mAutoFocusSetting.Enable(false);
  137. }
  138. }
  139. else
  140. {
  141. if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL))
  142. {
  143. this.View.FocusMode = CameraDevice.FocusMode.FOCUS_MODE_NORMAL;
  144. }
  145. }

    1. OnTappedToClose();
  146. }

    1. private void OnTappedToTurnCameraFacing(int val)
  147. {
  148. if(val == 0)
  149. {
  150. //internally, flash is always turned off everytime it tries to switch to front camera
  151. //so updating the UI options to reflect that.
  152. this.View.mCameraFlashSettings.Enable(false);

    1. if(ChangeCameraDirection(CameraDevice.CameraDirection.CAMERA_FRONT)) {
  153. mCameraFacingFront = true;
  154. }
  155. else {
  156. ChangeCameraDirection(CameraDevice.CameraDirection.CAMERA_BACK);
  157. mCameraFacingFront = false;
  158. this.View.mCameraFacing.EnableIndex(1);
  159. }
  160. }
  161. else
  162. {
  163. ChangeCameraDirection(CameraDevice.CameraDirection.CAMERA_BACK);
  164. mCameraFacingFront = false;
  165. }

    1. OnTappedToClose();
  166. }

    1. private void ResetCameraFacingToBack()
  167. {
  168. CameraDevice.Instance.Stop();
  169. CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);
  170. CameraDevice.Instance.Start();
  171. mCameraFacingFront = false;
  172. }

    1. private bool ChangeCameraDirection(CameraDevice.CameraDirection direction)
  173. {
  174. bool directionSupported = false;
  175. CameraDevice.Instance.Stop();
  176. CameraDevice.Instance.Deinit();
  177. if(CameraDevice.Instance.Init(direction)) {
  178. directionSupported = true;
  179. }
  180. CameraDevice.Instance.Start();

    1. return directionSupported;
  181. }

    1. private void OnTappedOnDataSet(int val)
  182. {
  183. if(val == 0)
  184. {
  185. ActivateDataSet(“StonesAndChips”);
  186. }
  187. else
  188. {
  189. ActivateDataSet(“Tarmac”);
  190. }

    1. OnTappedToClose();
  191. }

    1. private void OnTappedToClose()
  192. {
  193. if(this.CloseView != null)
  194. {
  195. this.CloseView();
  196. }
  197. }

    1. private void OnTappedOnCloseButton()
  198. {
  199. OnTappedToClose();
  200. }

    1. private void ActivateDataSet(string datasetPath)
  201. {
  202. //ImageTracker tracks ImageTargets contained in a DataSet and provides methods for creating, activating and deactivating datasets.
  203. ImageTracker imageTracker = TrackerManager.Instance.GetTracker();
  204. IEnumerable datasets = imageTracker.GetDataSets();

    1. IEnumerable activeDataSets = imageTracker.GetActiveDataSets();
  205. List activeDataSetsToBeRemoved = activeDataSets.ToList();

    1. //1. Loop through all the active datasets and deactivate them.
  206. foreach(DataSet ads in activeDataSetsToBeRemoved) {
  207. imageTracker.DeactivateDataSet(ads);
  208. }

    1. //Swapping of the datasets should not be done while the ImageTracker is working at the same time.
  209. //2. So, Stop the tracker first.
  210. imageTracker.Stop();

    1. //3. Then, look up the new dataset and if one exists, activate it.
  211. foreach(DataSet ds in datasets) {
  212. if(ds.Path.Contains(datasetPath)) {
  213. imageTracker.ActivateDataSet(ds);
  214. }
  215. }

    1. //4. Finally, start the image tracker.
  216. imageTracker.Start();
  217. }

    1. private void SwitchModels(bool tf)
  218. {
  219. ImageTargetTrackableEventHandler[] trackableEventHandlers = GameObject.FindObjectsOfType(typeof(ImageTargetTrackableEventHandler)) as ImageTargetTrackableEventHandler[];
  220. foreach(ImageTargetTrackableEventHandler handler in trackableEventHandlers)
  221. {
  222. if(handler.isBeingTracked)
  223. {
  224. Renderer[] rendererComponents = handler.GetComponentsInChildren();
  225. Collider[] colliderComponents = handler.GetComponentsInChildren();

    1. foreach (Renderer component in rendererComponents)
  226. {
  227. if(component.gameObject.name == “tower”)
  228. component.enabled = tf;
  229. if(component.gameObject.name == “teapot”)
  230. component.enabled = !tf;
  231. }

    1. foreach (Collider component in colliderComponents)
  232. {
  233. if(component.gameObject.name == “tower”)
  234. component.enabled = tf;
  235. if(component.gameObject.name == “teapot”)
  236. component.enabled = !tf;
  237. }
  238. }
  239. }
  240. }

    1. ///
  241. /// This method turns extended tracking on or off for all currently available targets.
  242. /// Extended tracking allows to track targets when they are not in view.
  243. /// Returns false if extended tracking is not supported on the device and true otherwise.
  244. ///
  245. private bool ExtendedTracking(bool tf)
  246. {
  247. // the StateManager gives access to all available TrackableBehavours
  248. StateManager stateManager = TrackerManager.Instance.GetStateManager();
  249. // We iterate over all TrackableBehaviours to start or stop extended tracking for the targets they represent.

    1. bool extendedTrackingStateChanged = true;
  250. foreach(var behaviour in stateManager.GetTrackableBehaviours())
  251. {
  252. var imageBehaviour = behaviour as ImageTargetBehaviour;
  253. if(imageBehaviour != null)
  254. {
  255. if(tf) {
  256. //only if extended tracking is supported
  257. if(!imageBehaviour.ImageTarget.StartExtendedTracking()) {
  258. extendedTrackingStateChanged = false;
  259. }
  260. }
  261. else {
  262. if(!imageBehaviour.ImageTarget.StopExtendedTracking()) {
  263. extendedTrackingStateChanged = false;
  264. }
  265. }
  266. }
  267. }

    1. if(!extendedTrackingStateChanged) {
  268. Debug.LogWarning(“Extended Tracking Failed!”);
  269. }

    1. return extendedTrackingStateChanged;
  270. }
  271. #endregion PRIVATE_METHODS
  272. }

四。
在Vuforia焦点模式下的行为(1.5版以上),描述如下:

FOCUS_MODE_NORMAL -通过设备提供的默认的对焦模式
FOCUS_MODE_TRIGGERAUTO -设置此对焦模式将触发一个自动对焦操作。
FOCUS_MODE_CONTINUOUSAUTO -首发的Android 2.3和iOS设备这一对焦模式下可切换式驱动级的连续自动对焦的摄像头。这是AR的应用程序的最佳对焦模式,因为它保证了相机聚焦在目标上,从而产生最佳的跟踪结果。
FOCUS_MODE_INFINITY -设置相机为“无穷大”,通过摄像头驱动程序的实现提供。(不支持IOS)。
FOCUS_MODE_MACRO -设置相机“微距”模式,由相机驱动程序的实现提供。这提供了特写(APPX。15厘米),在AR调校很少使用的距离急剧摄像机图像。(不支持IOS)。
我们鼓励使用FOCUS_MODE_CONTINUOUSAUTO在你的应用程序时,它可以在设备上。当设置该模式时,如果返回值setFocusMode()为TRUE时应用程序将提供锋利的照相机图像的两个上级渲染,以及超棒跟踪性能。

如果FOCUS_MODE_CONTINUOUSAUTO不可用,接下来最好的选择是实现“触摸聚焦”在你的应用程序的行为。要做到这一点,引发setFocusMode()与FOCUS_MODE_TRIGGERAUTO值每个用户触摸屏幕的时间。这种行为的缺点是,大多数摄像头的驱动程序随机选择一个方向集中(或近或远),所以你有50%的几率使图像散焦,然后集中在目标上。由于在此焦点的逻辑一定条件下的跟踪可能会丢失了一会儿,直到清晰的图像是由相机再次提供。

FOCUS_MODE_INFINITY和FOCUS_MODE_MACRO是在某些应用场合可使用的,如上所述。

FOCUS_MODE_NORMAL设置相机在默认模式下的摄像头驱动程序定义。Setting Focus Mode with the Vuforia Unity Extension C# API

bool focusModeSet = CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

if (!focusModeSet) {
Debug.Log(“Failed to set focus mode (unsupported mode).”);
}

来自http://blog.csdn.net/liang_704959721/article/details/39227101

五。
Camera Focus Modes
Continuous Autofocus and Other Focus Modes
This article describes the different focus modes available in Vuforia (since v1.5 and above).
The behavior of the focus modes in Vuforia (since v1.5) is described below:
1. FOCUS_MODE_NORMAL - the default focus mode provided by the device
2. FOCUS_MODE_TRIGGERAUTO - setting this focus mode will trigger a single autofocus operation.
3. FOCUS_MODE_CONTINUOUSAUTO - starting in Android 2.3 and iOS devices this focus mode allows to switch-on driver level continuous autofocus for cameras. This is the optimal focus mode for AR apps, since it guarantees that the camera is focused on the target, thus yielding the best tracking results.
4. FOCUS_MODE_INFINITY - sets the camera to “infinity”, as provided by the camera driver implementation. (Not supported on iOS).
5. FOCUS_MODE_MACRO - sets the camera to “macro” mode, as provided by the camera driver implementation. This provides a sharp camera image for distances of close-ups (appx. 15 cm), rarely used in AR set-ups. (Not supported on iOS).
We encourage using FOCUS_MODE_CONTINUOUSAUTO in your applications whenever it is available on the device. When setting this mode, if the return value of setFocusMode() is TRUE your application will provide sharp camera images for both superior rendering, as well as for robust tracking performance.
If the FOCUS_MODE_CONTINUOUSAUTO is not available, the next best option is to implement a ‘touch to focus’ behavior in your app. To do this, trigger setFocusMode() with FOCUS_MODE_TRIGGERAUTO value each time the user touches the screen. The disadvantage of this behavior is that most camera drivers pick a random direction to focus (near or far), so you have a 50% chance that the image will defocus and then focus on the target. Under certain conditions due this focus logic the tracking can be lost for a moment until a sharp image is provided again by the camera.
FOCUS_MODE_INFINITY and FOCUS_MODE_MACRO are usable in certain application scenarios, as described above.
FOCUS_MODE_NORMAL sets the camera into the default mode as defined by the camera driver.
Setting Focus Mode with the Vuforia C++ API
Setting Focus Mode with the Vuforia C++ API

1
2
3
4
5
6bool focusModeSet = Vuforia::CameraDevice::getInstance().setFocusMode(
Vuforia::CameraDevice::FOCUS_MODE_CONTINUOUSAUTO);

if (!focusModeSet) {
LOG(“Failed to set focus mode (unsupported mode).”);
}

Setting Focus Mode with the Vuforia Java API

1
2
3
4
5
6boolean focusModeSet = CameraDevice.getInstance().setFocusMode(
CameraDevice.FOCUS_MODE.FOCUS_MODE_CONTINUOUSAUTO);

if (!focusModeSet) {
Log.d(“Sample Log Tag”, “Failed to set focus mode (unsupported mode).”);
}
Setting Focus Mode with the Vuforia Unity Extension C# API

1
2
3
4
5bool focusModeSet = CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

if (!focusModeSet) {
Debug.Log(“Failed to set focus mode (unsupported mode).”);

You can configure autofocus when Vuforia initializes by adding the following code to a script that inherits from Monobehaviour. This registers a callback with the VuforiaBehaviour that will set a focus mode when the Vuforia process has started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21void Start ()
{
VuforiaBehaviour.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
VuforiaBehaviour.Instance.RegisterOnPauseCallback(OnPaused);
}

private void OnVuforiaStarted()
{
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}

private void OnPaused(bool paused)
{
if (!paused) // resumed
{
// Set again autofocus mode when app is resumed
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
}

在一些卡牌(卡片)游戏应用上,如果不能很好的对焦就会导致用户体验不佳,假若让系统相机去自动对焦这样出来的效果就会好很多,好的下面来说说—— 说之前,先科普一下,不喜略过直接往下看哈~ 对焦也叫对光、聚焦。通过照相机对焦机构变动物距和相距的位置,使被拍物成像清晰的过程就是对焦。而对焦的方式有以下几种:自动对焦、手动对焦、多重对焦以及全息自动对焦、我们这里讲的就是自动对焦,传统相机上的傻瓜式对焦方法。以下是相关的代码实现: 基于触摸屏,一种是直接将脚本文件挂载到ARCamera,一种是用NGUI、将其放到新建的GameManger底下,不过两种思路都是相同的,自动对焦的代码要写在start()方法里面,如图:(切记:using Vuforia;这个需要在一开始新建的脚本中引入)

接下来分析代码: [csharp] view plain copy //自动对焦变量 private string label; private float touchduration; private Touch touch; Start()方法,初始化并进行自动对焦调用 [csharp] view plain copy void Start () { if (Input.touchCount > 0) { touchduration += Time.deltaTime; touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Ended && touchduration < 0.2f) { StartCoroutine(“SingleOrDouble”); } } else { touchduration = 0; } } [csharp] view plain copy IEnumerator SingleOrDouble() { yield return new WaitForSeconds(0.3f); if (touch.tapCount == 1) { Debug.Log(“Single”); OnSingleTapped(); } else if (touch.tapCount == 2) { StopCoroutine(“SingleOrDouble”);//否则会触发两次Double Touch Debug.Log(“Double”); OnDoubleTapped(); } } private void OnSingleTapped() { TriggerAutoFocus(); label = “Tap the Screen!”; } private void OnDoubleTapped() { label = “Double Tap the Screen!”; } 触发自动对焦方法: [csharp] view plain copy public void TriggerAutoFocus() { StartCoroutine(TriggerAutoFocusAndEnableContinuousFocusIfSet()); } private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet() { CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO); yield return new WaitForSeconds(1.0f); CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); } void OnGUI() { GUI.Label(new Rect(10, 0, 100, 100), “-&-C*-Z*-D*-&->” + label); } 最后在update()方法写上: [csharp] view plain copy void Update () { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } 有的童鞋,也将自动对焦的代码写在update()方法里面,即是start()方法空着,由于我这边的功能实现上还有别的,所以我做了这样的处理,希望大家多多交流哈,不吝赐教。 [csharp] view plain copy using UnityEngine; using System.Collections; using Vuforia; public class AutoFocusControl : MonoBehaviour { private string label; private float touchduration; private Touch touch; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } if (Input.touchCount > 0) { touchduration += Time.deltaTime; touch = Input.GetTouch(0); if(touch.phase == TouchPhase.Ended && touchduration < 0.2f) { StartCoroutine(“SingleOrDouble”); } } else { touchduration = 0; } } IEnumerator SingleOrDouble() { yield return new WaitForSeconds(0.3f); if (touch.tapCount == 1) { Debug.Log(“Single”); OnSingleTapped(); } else if (touch.tapCount == 2) { StopCoroutine(“SingleOrDouble”); Debug.Log(“Double”); OnDoubleTapped(); } } private void OnSingleTapped() { TriggerAutoFocus(); label = “Tap the Screen!”; } private void OnDoubleTapped() { label = “Double Tap the Screen!”; } public void TriggerAutoFocus() { StartCoroutine(TriggerAutoFocusAndEnableContinuousFocusIfSet()); } private IEnumerator TriggerAutoFocusAndEnableContinuousFocusIfSet() { CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO); yield return new WaitForSeconds(1.0f); CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); } void OnGUI() { GUI.Label(new Rect(10, 0, 100, 100), “—->” + label); } } 最后的最后,说说一些综合的东西。 1.测试在手机上面实测,效果比较明显 2.关于vuforia sdk 3.x和4.x的代码异同,当然现在已经去到5.x版本了,不过拾人牙慧,以下上代码: 3.x版本的: [csharp] view plain copy void Start () { CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); } 4.x版本的: [csharp] view plain copy void Start () { Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); } - 转载请保留原文链接:http://www.chinaar.com/Vuforia/1988.html

猜你喜欢

转载自blog.csdn.net/Chenzan524/article/details/78293188