然后,加入一个CloudRecoEventHandler.cs到CloudRecognition这个物体上(下面会介绍)。这将能让你捕获图片扫描结果,结果将以TargetFinder.TargetSearchResult的结构返回给你。
public struct TargetSearchResult
{
// name of the target
public string TargetName;
// system-wide unique id of the target.
public string UniqueTargetId;
/// width of the target (in 3D scene units)
public float TargetSize;
// metadata associated with this target
public string MetaData;
//tracking rating for this target
/**
* The tracking rating represents a 5-star rating describing the
* suitability of this target for tracking on a scale from 0 to 5. A low
* tracking rating may result in poor tracking or unstable augmentation.
*/
public byte TrackingRating;
// pointer to native search result
public IntPtr TargetSearchResultPtr;
另外,你要做自己的设计的话,也可以用下面这个精简版的SimpleCloudRecoEventHandler.cs. 你需要获得的数据可以在OnNewSearchResult()方法中调用。
using System;
using UnityEngine;
///
/// This MonoBehaviour implements the Cloud Reco Event handling for this sample.
/// It registers itself at the CloudRecoBehaviour and is notified of new search results.
///
public class SimpleCloudRecoEventHandler : MonoBehaviour, ICloudRecoEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
// CloudRecoBehaviour reference to avoid lookups
private CloudRecoBehaviour mCloudRecoBehaviour;
// ImageTracker reference to avoid lookups
private ImageTracker mImageTracker;
#endregion // PRIVATE_MEMBER_VARIABLES
#region EXPOSED_PUBLIC_VARIABLES
///
/// can be set in the Unity inspector to reference a ImageTargetBehaviour that is used for augmentations of new cloud reco results.
///
public ImageTargetBehaviour ImageTargetTemplate;
#endregion
#region ICloudRecoEventHandler_IMPLEMENTATION
///
/// called when TargetFinder has been initialized successfully
///
public void OnInitialized()
{
// get a reference to the Image Tracker, remember it
mImageTracker = (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER);
}
///
/// updates to the scanning state
///
public void OnStateChanged(bool scanning)
{
}
///
/// Handles new search results
///
///
public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
{
// duplicate the referenced image target
GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
// enable the new result with the same ImageTargetBehaviour:
ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);
if (imageTargetBehaviour != null)
{
// stop the target finder
mCloudRecoBehaviour.CloudRecoEnabled = false;
}
}
///
/// register for events at the CloudRecoBehaviour
///
void Start()
{
// register this event handler at the cloud reco behaviour
CloudRecoBehaviour cloudRecoBehaviour = GetComponent();
if (cloudRecoBehaviour)
{
cloudRecoBehaviour.RegisterEventHandler(this);
}
// remember cloudRecoBehaviour for later
mCloudRecoBehaviour = cloudRecoBehaviour;
}