迁移sample ImageTracking_Targets 到 4000.0¶
替换插件包¶
关闭Unity
删除Unity打包生成的平台编译目录(Android的Gradle工程目录,以及iOS的XCode目录)或整个Library文件夹。
重新打开Unity工程,将老版本的EasyAR Sense Unity Plugin从工程中移除
导入4000.0版本的EasyAR Sense Unity Plugin
修改不兼容代码:最快可运行¶
重新导入sample使用的StreamingAssets
private static readonly string[] streamingAssetsFiles = new string[] {
"EasyARSamples/ImageTargets/idback.etd",
"EasyARSamples/ImageTargets/namecard.jpg",
};
[UnityEditor.InitializeOnLoadMethod]
static void ImportSampleStreamingAssets()
{
var pacakge = $"Packages/{UnityPackage.Name}/Samples~/StreamingAssets/ImageTargets/ImageTargets.unitypackage";
if (streamingAssetsFiles.Where(f => !System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, f))).Any() && System.IO.File.Exists(System.IO.Path.GetFullPath(pacakge)))
{
UnityEditor.AssetDatabase.ImportPackage(pacakge, false);
}
}
在Update中移除前置相机自动翻转画面的逻辑,移除SwitchHFlipMode()函数
private void Update()
{
//bool isFront = false;
//if(cameraDevice != null)
//{
// using (var cameraParameters = cameraDevice.cameraParameters())
// {
// if (cameraParameters.cameraDeviceType() == CameraDeviceType.Front)
// {
// isFront = true;
// }
// }
//}
var statusText = "CenterMode: " + Session.CenterMode + Environment.NewLine +
"CenterObject: " + (Session && Session.CenterObject ? Session.CenterObject.name : null) + Environment.NewLine +
//"HorizontalFlip: " + (isFront ? Session.HorizontalFlipFront : Session.HorizontalFlipNormal) + Environment.NewLine +
"Camera: " + (cameraDevice && cameraDevice.enabled ? "On" : "Off") + Environment.NewLine +
"Tracking: " + (imageTracker && imageTracker.enabled ? "On" : "Off") + Environment.NewLine + Environment.NewLine +
"Target Load Status:" + Environment.NewLine;
foreach (var item in imageTargetControllers)
{
statusText += "\t" + item.Key.gameObject.name + ": " + item.Value + Environment.NewLine;
}
Status.text = statusText;
}
使用新版本API,更新NextCamera()函数
public void NextCamera()
{
if (!cameraDevice || cameraDevice.Opened)
{
return;
}
if (CameraDeviceFrameSource.CameraCount == 0)
{
cameraDevice.Close();
return;
}
var index = cameraDevice.Index;
index = (index + 1) % CameraDeviceFrameSource.CameraCount;
cameraDevice.CameraOpenMethod = CameraDeviceFrameSource.CameraDeviceOpenMethod.DeviceIndex;
cameraDevice.CameraOpenIndex = index;
cameraDevice.Close();
cameraDevice.Open();
}
使用新版本API,更新CreateTargets()函数
private void CreateTargets()
{
// dynamically load from image (*.jpg, *.png)
var targetController = CreateTargetNode("ImageTarget-argame00");
targetController.Tracker = imageTracker;
targetController.Source = new ImageTargetController.TargetDataFileSourceData
{
PathType = PathType.StreamingAssets,
Path = "idback.etd",
};
//targetController.SourceType = ImageTargetController.DataSource.ImageFile;
//targetController.ImageFileSource.PathType = PathType.StreamingAssets;
//targetController.ImageFileSource.Path = "sightplus/argame00.jpg";
//targetController.ImageFileSource.Name = "argame00";
//targetController.ImageFileSource.Scale = 0.1f;
GameObject duck02 = Instantiate(Resources.Load("duck02")) as GameObject;
duck02.transform.parent = targetController.gameObject.transform;
// dynamically load from json string ...
foreach (var image in imageJson.images)
{
targetController = CreateTargetNode("ImageTarget-" + image.name);
targetController.Tracker = imageTracker;
//targetController.ImageFileSource.PathType = PathType.StreamingAssets;
//targetController.ImageFileSource.Path = image.image;
//targetController.ImageFileSource.Name = image.name;
//targetController.ImageFileSource.Scale = image.scale;
targetController.Source = new ImageTargetController.ImageFileSourceData
{
Path = image.image,
Name = image.name,
Scale = 0.1f,
};
var duck03 = Instantiate(Resources.Load("duck03")) as GameObject;
duck03.transform.parent = targetController.gameObject.transform;
}
}
使用新版本API, 更新AddTargetControllerEvents(ImageTargetController controller)函数
private void AddTargetControllerEvents(ImageTargetController controller)
{
if (!controller)
{
return;
}
controller.TargetFound += () =>
{
Debug.LogFormat("Found target {{id = {0}, name = {1}}}", controller.Target.runtimeID(), controller.Target.name());
};
controller.TargetLost += () =>
{
Debug.LogFormat("Lost target {{id = {0}, name = {1}}}", controller.Target.runtimeID(), controller.Target.name());
};
controller.TargetDataLoad += (bool status) =>
{
imageTargetControllers[controller] = status ? true : imageTargetControllers[controller];
Debug.LogFormat("Load target {{id = {0}, name = {1}, size = {2}}} into {3} => {4}", controller.Target == null? controller.Target.runtimeID():string.Empty,
controller.Target.name(), controller.Size, controller.Tracker.name, status);
};
}
此时Sample已经基本可以运行
重新构建场景:准备使用新功能¶
在这之后将场景中的AR Session(EasyAR)重新创建
将场景中的 ImageTargetController 重新指定 ImageTrackerFrameFilter
将Sample脚本中的 ARSession 重新指定为新创建的 AR Session(EasyAR)
此时Sample已经可以基于4000.0版本的插件运行
另外还可以调整ImageTarget的相关参数以使用新增的Texture2D加载方式。