From 580521e00f5305867105c815faf810733417d94a Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Thu, 6 Jun 2024 22:35:23 +0100 Subject: [PATCH] Updating for ARFoundation 6 support --- .../ARFoundationTrackableImagesLibrary.cs | 0 ...ARFoundationTrackableImagesLibrary.cs.meta | 0 .../ARFoundationTrackedImageData.cs | 0 .../ARFoundationTrackedImageData.cs.meta | 0 ...alPersistenceServiceImageTrackingModule.cs | 25 ++++++++++++++++--- ...kit.SpatialPersistence.ARFoundation.asmdef | 16 ++++++++++-- 6 files changed, 36 insertions(+), 5 deletions(-) rename Runtime/{Modules => Definitions}/ARFoundationTrackableImagesLibrary.cs (100%) rename Runtime/{Modules => Definitions}/ARFoundationTrackableImagesLibrary.cs.meta (100%) rename Runtime/{Modules => Definitions}/ARFoundationTrackedImageData.cs (100%) rename Runtime/{Modules => Definitions}/ARFoundationTrackedImageData.cs.meta (100%) diff --git a/Runtime/Modules/ARFoundationTrackableImagesLibrary.cs b/Runtime/Definitions/ARFoundationTrackableImagesLibrary.cs similarity index 100% rename from Runtime/Modules/ARFoundationTrackableImagesLibrary.cs rename to Runtime/Definitions/ARFoundationTrackableImagesLibrary.cs diff --git a/Runtime/Modules/ARFoundationTrackableImagesLibrary.cs.meta b/Runtime/Definitions/ARFoundationTrackableImagesLibrary.cs.meta similarity index 100% rename from Runtime/Modules/ARFoundationTrackableImagesLibrary.cs.meta rename to Runtime/Definitions/ARFoundationTrackableImagesLibrary.cs.meta diff --git a/Runtime/Modules/ARFoundationTrackedImageData.cs b/Runtime/Definitions/ARFoundationTrackedImageData.cs similarity index 100% rename from Runtime/Modules/ARFoundationTrackedImageData.cs rename to Runtime/Definitions/ARFoundationTrackedImageData.cs diff --git a/Runtime/Modules/ARFoundationTrackedImageData.cs.meta b/Runtime/Definitions/ARFoundationTrackedImageData.cs.meta similarity index 100% rename from Runtime/Modules/ARFoundationTrackedImageData.cs.meta rename to Runtime/Definitions/ARFoundationTrackedImageData.cs.meta diff --git a/Runtime/Modules/ARFoundationSpatialPersistenceServiceImageTrackingModule.cs b/Runtime/Modules/ARFoundationSpatialPersistenceServiceImageTrackingModule.cs index eacb94d..ee43a40 100644 --- a/Runtime/Modules/ARFoundationSpatialPersistenceServiceImageTrackingModule.cs +++ b/Runtime/Modules/ARFoundationSpatialPersistenceServiceImageTrackingModule.cs @@ -29,6 +29,8 @@ public class ARFoundationSpatialPersistenceServiceImageTrackingModule : BaseSpat private bool isStarted = false; private bool isStarting = false; + private bool hasValidTrackingProfile => profile.IsNotNull() && profile.TrackedImagesLibrary.IsNotNull(); + private ARTrackedImageManager TrackedImageManager { get @@ -107,7 +109,12 @@ public override Task StartSpatialPersistenceModule() runtimeImageLibrary = TrackedImageManager.referenceLibrary as MutableRuntimeReferenceImageLibrary; DynamicLibraryManager.OnImageLoaded += OnImageLoaded; DynamicLibraryManager.OnImageLoadFailed += OnImageLoadFailed; + +#if ARFOUNDATION_6 + TrackedImageManager.trackablesChanged.AddListener(OnTrackedImagesChanged); +#else TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged; +#endif if (profile.TrackedImagesLibrary.IsNotNull() && profile.TrackedImagesLibrary.TrackedImages != null) { DynamicLibraryManager.ProcessImages(runtimeImageLibrary, profile.TrackedImagesLibrary.TrackedImages); @@ -125,7 +132,11 @@ public override void StopSpatialPersistenceModule() { if (TrackedImageManager != null) { +#if ARFOUNDATION_6 + TrackedImageManager.trackablesChanged.RemoveListener(OnTrackedImagesChanged); +#else TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged; +#endif } OnSessionEnded(); } @@ -172,7 +183,7 @@ private IEnumerator AddToLibrary(MutableRuntimeReferenceImageLibrary mutableLibr { if (mutableLibrary is null) { - OnSpatialPersistenceError($"Library is inaccessble."); + OnSpatialPersistenceError($"Library is inaccessible."); yield return null; } if (trackedImageIds.Contains(anchorGuid)) @@ -194,7 +205,11 @@ private IEnumerator AddToLibrary(MutableRuntimeReferenceImageLibrary mutableLibr dynamicLibraryManager.ProcessImage(mutableLibrary, anchorGuid, texture, url); } +#if ARFOUNDATION_6 + private void OnTrackedImagesChanged(ARTrackablesChangedEventArgs trackedImage) +#else private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs trackedImage) +#endif { foreach (var newImage in trackedImage.added) { @@ -223,7 +238,7 @@ private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs trackedImage if (trackedImageIds.Contains(updatedImage.referenceImage.guid)) { Guid refGuid = updatedImage.referenceImage.guid; - if(trackedImageReferences.TryGetValue(updatedImage.referenceImage.guid, out var trackedGuid)) + if (trackedImageReferences.TryGetValue(updatedImage.referenceImage.guid, out var trackedGuid)) { refGuid = trackedGuid; } @@ -235,7 +250,11 @@ private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs trackedImage } } +#if ARFOUNDATION_6 + foreach (var (id, removedImage) in trackedImage.removed) +#else foreach (var removedImage in trackedImage.removed) +#endif { if (trackedImageIds.Contains(removedImage.referenceImage.guid)) { @@ -256,7 +275,7 @@ private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs trackedImage private void OnImageLoaded(ARFoundationTrackedImageData data) { - if(profile.TrackedImagesLibrary.GetTrackedImageByName(data.Name) == null) + if (hasValidTrackingProfile && profile.TrackedImagesLibrary.GetTrackedImageByName(data.Name) == null) { profile.TrackedImagesLibrary.AddTrackedImageData(data); } diff --git a/Runtime/RealityToolkit.SpatialPersistence.ARFoundation.asmdef b/Runtime/RealityToolkit.SpatialPersistence.ARFoundation.asmdef index 570b1c0..f830e29 100644 --- a/Runtime/RealityToolkit.SpatialPersistence.ARFoundation.asmdef +++ b/Runtime/RealityToolkit.SpatialPersistence.ARFoundation.asmdef @@ -7,7 +7,8 @@ "GUID:b2d046948d6452a4b8485efc9ce0f88c", "GUID:c148ca4f0506aba4bb7c9e15a44bf2e3", "GUID:a9420e37d7990b54abdef6688edbe313", - "GUID:92703082f92b41ba80f0d6912de66115" + "GUID:92703082f92b41ba80f0d6912de66115", + "GUID:dc960734dc080426fa6612f1c5fe95f3" ], "includePlatforms": [], "excludePlatforms": [], @@ -16,6 +17,17 @@ "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], - "versionDefines": [], + "versionDefines": [ + { + "name": "com.unity.xr.arfoundation", + "expression": "6.0.0", + "define": "ARFOUNDATION_6" + }, + { + "name": "com.unity.xr.arfoundation", + "expression": "[4.0.0,5.2.0]", + "define": "ARFOUNDATION_4" + } + ], "noEngineReferences": false } \ No newline at end of file