static MarsMarkerLibrary SelectMarkerLibrary()
        {
            var companionResourceSync = CompanionResourceSync.instance;
            var library = companionResourceSync.DefaultMarkerLibrary;

            if (library == null)
            {
                var libraries = CompanionEditorAssetUtils.LoadAllMarkerLibraryAssets();
                if (libraries == null || libraries.Length == 0)
                {
                    library = CompanionEditorAssetUtils.CreateAndSaveNewLibrary();
                    RefreshMarkerLibraryList();
                    companionResourceSync.DefaultMarkerLibrary = library;
                }
                else if (libraries.Length > 0)
                {
                    var session = MARSSession.Instance;
                    if (session != null && session.MarkerLibrary != null)
                    {
                        library = session.MarkerLibrary;
                    }

                    if (library == null)
                    {
                        library = libraries[0];
                    }
                }
            }

            return(library);
        }
        internal static (MarsMarkerLibrary[], string[]) RefreshMarkerLibraryList()
        {
            var updatedMarkerLibraries = CompanionEditorAssetUtils.LoadAllMarkerLibraryAssets();
            var updatedLibraryNames    = updatedMarkerLibraries.Length == 0
                ? new[] { string.Empty, "Add Marker Library" }
                : updatedMarkerLibraries.Select(lib => lib.name).ToArray();

            return(updatedMarkerLibraries, updatedLibraryNames);
        }
        static RequestHandle ImportMarker(this IUsesCloudStorage storageUser, string imagePath, string markerKey,
                                          string jsonText, Action <UnityObject> callback)
        {
            return(storageUser.DownloadMarkerImage(markerKey, (success, tempPath) =>
            {
                var destinationPath = Path.Combine(Directory.GetCurrentDirectory(), imagePath);
                try
                {
                    AssetDatabase.StartAssetEditing();

                    if (File.Exists(tempPath))
                    {
                        if (File.Exists(destinationPath))
                        {
                            File.Delete(destinationPath);
                        }

                        File.Move(tempPath, destinationPath);
                    }
                    else
                    {
                        Debug.LogError($"Couldn't find image file at path {tempPath}");
                    }
                }
                catch (IOException e)
                {
                    Debug.LogException(e);
                }
                finally
                {
                    AssetDatabase.StopAssetEditing();
                }

                // we need access to the created texture for the image before we can add it to a marker library
                AssetDatabase.Refresh();

                ApplyImageMarkerTextureImportSettings(imagePath);

                var marker = SceneSerialization.FromJson <Marker>(jsonText);

                // add the created marker to a marker library
                var markerTex = AssetDatabase.LoadAssetAtPath <Texture2D>(imagePath);
                var markerLibrary = SelectMarkerLibrary();
                Guid markerGuid;
                if (markerTex != null && markerLibrary != null)
                {
                    markerGuid = CompanionEditorAssetUtils.CreateMarkerInLibrary(markerLibrary, marker, markerTex);
                }

                // create a Proxy in the scene with a MarkerCondition looking for the marker's new guid
                var markerProxyObj = CreateMarkerProxy(marker, markerGuid);
                EditorGUIUtility.PingObject(markerProxyObj);


                var prefabSavePath = EditorUtility.SaveFilePanelInProject(k_SaveProxyTitle,
                                                                          string.Format(k_ProxyNameFormat, marker.name), k_PrefabExtension, k_SaveProxyMessage);

                // if user presses 'cancel' in the save prompt, no prefab will be saved
                if (!string.IsNullOrEmpty(prefabSavePath))
                {
                    // creation of prefab is outside the start/stop editing block so we can highlight the created asset
                    var createdPrefab = PrefabUtility.SaveAsPrefabAssetAndConnect(markerProxyObj, prefabSavePath, InteractionMode.AutomatedAction);
                    ProjectWindowUtil.ShowCreatedAsset(createdPrefab);
                    callback(createdPrefab);
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                var resourceSync = CompanionResourceSync.instance;
                CompanionMarkerUtils.SplitMarkerKey(markerKey, out _, out var cloudGuid);
                resourceSync.SetAssetGuid(AssetDatabase.AssetPathToGUID(prefabSavePath), cloudGuid);
            }));
        }