Migrate sample ImageTracking_Targets to 4000.0¶
Replace Plugin Package¶
Close Unity
Delete Unity’s platform build directories generated by the editor (for example, the Android Gradle project directory and the iOS Xcode directory), or delete the entire Library folder.
Reopen the Unity project and remove the old version of EasyAR Sense Unity Plugin from the project
Import EasyAR Sense Unity Plugin version 4000.0
Modify Incompatible Code: Minimum to get running¶
Re-import StreamingAssets used by the sample
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);
}
}
Remove the logic for automatic flipping of the front camera image in Update, remove the SwitchHFlipMode() function
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;
}
Use the new version API, update the NextCamera() function
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();
}
Use the new version API, update the CreateTargets() function
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;
}
}
Use the new version API, update the AddTargetControllerEvents(ImageTargetController controller) function
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);
};
}
The sample is now functional.
Rebuild Scene: Prepare for New Features¶
After this, recreate the AR Session (EasyAR) in the scene
Reassign ImageTrackerFrameFilter to ImageTargetController in the scene
Reassign ARSession in the Sample script to the newly created AR Session (EasyAR)
The sample is now fully functional with the 4000.0 plugin.
You can also adjust the relevant parameters of ImageTarget to use the new Texture2D loading method.