已编辑
我将Unity版本更改为
2018.3.8f1
,将Vuforia版本更改为8.0.10
,它可以正常工作。但是有人知道如何在最新版本中工作吗?
我正在关注this tutorial以在Unity中实现Cloud Reco。
我的SimpleCloudHandler.cs
脚本中的错误:
错误CS0246:找不到类型或名称空间名称'ICloudRecoEventHandler'(您是否缺少using指令或程序集引用?)
我的Unity版本为2018.3.14f1
我的Vuforia版本是8.1.7
本教程之后的代码:
using Vuforia;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler
{
private CloudRecoBehaviour mCloudRecoBehaviour;
private bool mIsScanning = false;
private string mTargetMetadata = "";
public ImageTargetBehaviour ImageTargetTemplate;
public void OnInitialized(TargetFinder targetFinder) {
Debug.Log ("Cloud Reco initialized");
}
public void OnInitError(TargetFinder.InitState initError) {
Debug.Log ("Cloud Reco init error " + initError.ToString());
}
public void OnUpdateError(TargetFinder.UpdateState updateError) {
Debug.Log ("Cloud Reco update error " + updateError.ToString());
}
public void OnStateChanged(bool scanning) {
mIsScanning = scanning;
if (scanning)
{
// clear all known trackables
var tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
tracker.TargetFinder.ClearTrackables(false);
}
}
// Here we handle a cloud target recognition event
public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult) {
TargetFinder.CloudRecoSearchResult cloudRecoSearchResult =
(TargetFinder.CloudRecoSearchResult)targetSearchResult;
// do something with the target metadata
mTargetMetadata = cloudRecoSearchResult.MetaData;
// stop the target finder (i.e. stop scanning the cloud)
mCloudRecoBehaviour.CloudRecoEnabled = false;
// Build augmentation based on target
if (ImageTargetTemplate) {
// enable the new result with the same ImageTargetBehaviour:
ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
ImageTargetBehaviour imageTargetBehaviour =
(ImageTargetBehaviour)tracker.TargetFinder.EnableTracking(
targetSearchResult, ImageTargetTemplate.gameObject);
}
}
void OnGUI() {
// Display current 'scanning' status
GUI.Box (new Rect(100,100,200,50), mIsScanning ? "Scanning" : "Not scanning");
// Display metadata of latest detected cloud-target
GUI.Box (new Rect(100,200,200,50), "Metadata: " + mTargetMetadata);
// If not scanning, show button
// so that user can restart cloud scanning
if (!mIsScanning) {
if (GUI.Button(new Rect(100,300,200,50), "Restart Scanning")) {
// Restart TargetFinder
mCloudRecoBehaviour.CloudRecoEnabled = true;
}
}
}
// Start is called before the first frame update
void Start()
{
mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
if (mCloudRecoBehaviour){
mCloudRecoBehaviour.RegisterEventHandler(this);
}
}
// Update is called once per frame
void Update()
{
}
}
答案 0 :(得分:2)
我在Unity 2019.1.12f1上遇到了相同的问题。问题在于该教程已过时。参见例如this site说ICloudRecoEventHandler
重命名为IObjectRecoEventHandler
和this post,建议在其中使用两个Vuforia示例BASE和BOOKS。
我最近已经研究了此问题,发现以下脚本的工作方式为SimpleCloudHandler.cs
。
如果您愿意,请保持联系,我们会互相帮助!
using UnityEngine;
using Vuforia;
public class SimpleCloudHandler : MonoBehaviour, IObjectRecoEventHandler
{
private CloudRecoBehaviour mCloudRecoBehaviour;
private bool mIsScanning = false;
private string mTargetMetadata = "";
// Use this for initialization
void Start()
{
// register this event handler at the cloud reco behaviour
mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
if (mCloudRecoBehaviour)
{
mCloudRecoBehaviour.RegisterEventHandler(this);
}
}
public void OnInitialized(TargetFinder targetFinder)
{
Debug.Log("Cloud Reco initialized");
}
public void OnInitError(TargetFinder.InitState initError)
{
Debug.Log("Cloud Reco init error " + initError.ToString());
}
public void OnUpdateError(TargetFinder.UpdateState updateError)
{
Debug.Log("Cloud Reco update error " + updateError.ToString());
}
/// <summary>
/// when we start scanning, unregister Trackable from the ImageTargetBehaviour,
/// then delete all trackables
/// This function is from CloudRecoEventHandler.cs from Vuforia Base sample
/// </summary>
public void OnStateChanged(bool scanning)
{
Debug.Log("<color=blue>OnStateChanged(): </color>" + scanning);
// Changing CloudRecoBehaviour.CloudRecoEnabled to false will call:
// 1. TargetFinder.Stop()
// 2. All registered ICloudRecoEventHandler.OnStateChanged() with false.
// Changing CloudRecoBehaviour.CloudRecoEnabled to true will call:
// 1. TargetFinder.StartRecognition()
// 2. All registered ICloudRecoEventHandler.OnStateChanged() with true.
}
// Here we handle a cloud target recognition event
public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
{
TargetFinder.CloudRecoSearchResult cloudRecoSearchResult =
(TargetFinder.CloudRecoSearchResult)targetSearchResult;
// do something with the target metadata
mTargetMetadata = cloudRecoSearchResult.MetaData;
// stop the target finder (i.e. stop scanning the cloud)
mCloudRecoBehaviour.CloudRecoEnabled = false;
}
void OnGUI()
{
// Display current 'scanning' status
GUI.Box(new Rect(100, 100, 200, 50), mIsScanning ? "Scanning" : "Not scanning");
// Display metadata of latest detected cloud-target
GUI.Box(new Rect(100, 200, 200, 50), "Metadata: " + mTargetMetadata);
// If not scanning, show button
// so that user can restart cloud scanning
if (!mIsScanning)
{
if (GUI.Button(new Rect(100, 300, 200, 50), "Restart Scanning"))
{
// Restart TargetFinder
mCloudRecoBehaviour.CloudRecoEnabled = true;
}
}
}
}