asus4 / ARKitStreamer

AR Foundation Remote Debugging Tool for Unity
Other
191 stars 26 forks source link

ARFace's GetUndisposable throws error on UnityEditor #2

Closed asus4 closed 3 years ago

asus4 commented 5 years ago

ARFace.GetUndisposable throws on UnityEditor, since NativeArray.cs checks read/write access on Editor

// ARFace.cs
 public unsafe NativeArray<Vector3> vertices
 {
    get
    {
         // throws error on Editor
         return GetUndisposable(m_FaceMesh.vertices);
         // It works
         return m_FaceMesh.vertices;
     }
}

Error Message

NullReferenceException: Object reference not set to an instance of an object
Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:117)
Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:139)
Unity.Collections.NativeArray`1+Enumerator[T].get_Current () <0x14a2783c0 + 0x0007a> in <85963591c1a04aad836445db604e7807>:0
UnityEngine.XR.ARFoundation.ARFaceMeshVisualizer.TryCopyToList[T] (Unity.Collections.NativeArray`1[T] array, System.Collections.Generic.List`1[T] list) (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFaceMeshVisualizer.cs:46)
UnityEngine.XR.ARFoundation.ARFaceMeshVisualizer.SetMeshTopology () (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFaceMeshVisualizer.cs:61)
UnityEngine.XR.ARFoundation.ARFaceMeshVisualizer.SetVisible (System.Boolean visible) (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFaceMeshVisualizer.cs:34)
UnityEngine.XR.ARFoundation.ARFaceMeshVisualizer.UpdateVisibility () (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFaceMeshVisualizer.cs:103)
UnityEngine.XR.ARFoundation.ARFaceMeshVisualizer.OnUpdated (UnityEngine.XR.ARFoundation.ARFaceUpdatedEventArgs eventArgs) (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFaceMeshVisualizer.cs:108)
UnityEngine.XR.ARFoundation.ARFace.Update () (at Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFace.cs:123)

refs https://github.com/Unity-Technologies/arfoundation-samples/issues/343

asus4 commented 5 years ago

As a workaround, please modify GetUndisposable method in ARFace.cs

File path: Library/PackageCache/com.unity.xr.arfoundation@3.0.0-preview.4/Runtime/AR/ARFace.cs

Code:

        unsafe NativeArray<T> GetUndisposable<T>(NativeArray<T> disposable) where T : struct
        {
            // Add these line
#if UNITY_EDITOR
            return disposable;
#endif

            if (!disposable.IsCreated)
                return default(NativeArray<T>);

            return NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>(
                disposable.GetUnsafePtr(),
                disposable.Length,
                Allocator.None);
        }
asus4 commented 3 years ago

Added a patch to fix this issue