TrackingImageDatabaseContextMenu.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using UnityEditor;
  15. using UnityEngine;
  16. /// <summary> A tracking image database context menu. </summary>
  17. public static class TrackingImageDatabaseContextMenu
  18. {
  19. /// <summary> Message describing the supported image format list. </summary>
  20. private const string _SupportedImageFormatListMessage = "PNG and JPEG";
  21. /// <summary> The supported image extensions. </summary>
  22. private static readonly List<string> k_SupportedImageExtensions = new List<string>()
  23. {
  24. ".png", ".jpg", ".jpeg"
  25. };
  26. /// <summary> The unsupported image extensions. </summary>
  27. private static readonly List<string> k_UnsupportedImageExtensions = new List<string>()
  28. {
  29. ".psd", ".tiff", ".tga", ".gif", ".bmp", ".iff", ".pict"
  30. };
  31. /// <summary> Adds assets to new tracking image database. </summary>
  32. [MenuItem("Assets/Create/NRSDK/TrackingImageDatabase", false, 2)]
  33. private static void AddAssetsToNewTrackingImageDatabase()
  34. {
  35. var selectedImagePaths = new List<string>();
  36. bool unsupportedImagesSelected = false;
  37. selectedImagePaths = GetSelectedImagePaths(out unsupportedImagesSelected);
  38. if (unsupportedImagesSelected)
  39. {
  40. var message = string.Format("Some selected images could not be added to the TrackingImageDatabase because " +
  41. "they are not in a supported format. Supported image formats are {0}.",
  42. _SupportedImageFormatListMessage);
  43. Debug.LogWarningFormat(message);
  44. EditorUtility.DisplayDialog("Unsupported Images Selected", message, "Ok");
  45. }
  46. if (selectedImagePaths.Count > 0)
  47. {
  48. var newDatabase = ScriptableObject.CreateInstance<NRTrackingImageDatabase>();
  49. var newEntries = new List<NRTrackingImageDatabaseEntry>();
  50. foreach (var imagePath in selectedImagePaths)
  51. {
  52. var fileName = Path.GetFileName(imagePath);
  53. var imageName = fileName.Replace(Path.GetExtension(fileName), string.Empty);
  54. newEntries.Add(new NRTrackingImageDatabaseEntry(imageName,
  55. AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath)));
  56. }
  57. newEntries = newEntries.OrderBy(x => x.Name).ToList();
  58. foreach (var entry in newEntries)
  59. {
  60. newDatabase.Add(entry);
  61. }
  62. string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  63. if (selectedPath == string.Empty)
  64. {
  65. selectedPath = "Assets";
  66. }
  67. else if (Path.GetExtension(selectedPath) != string.Empty)
  68. {
  69. selectedPath = selectedPath.Replace(Path.GetFileName(selectedPath), string.Empty);
  70. }
  71. var newAssetPath = AssetDatabase.GenerateUniqueAssetPath(
  72. Path.Combine(selectedPath, "TrackingImageDatabase.asset"));
  73. AssetDatabase.CreateAsset(newDatabase, newAssetPath);
  74. EditorUtility.FocusProjectWindow();
  75. Selection.activeObject = newDatabase;
  76. }
  77. }
  78. /// <summary> Adds assets to new tracking image database validation. </summary>
  79. /// <returns> True if it succeeds, false if it fails. </returns>
  80. [MenuItem("Assets/Create/NRSDK/TrackingImageDatabase", true, 2)]
  81. private static bool AddAssetsToNewTrackingImageDatabaseValidation()
  82. {
  83. bool unsupportedSelected;
  84. return GetSelectedImagePaths(out unsupportedSelected).Count > 0;
  85. }
  86. /// <summary> Gets selected image paths. </summary>
  87. /// <param name="unsupportedImagesSelected"> [out] True if unsupported images selected.</param>
  88. /// <returns> The selected image paths. </returns>
  89. private static List<string> GetSelectedImagePaths(out bool unsupportedImagesSelected)
  90. {
  91. var selectedImagePaths = new List<string>();
  92. unsupportedImagesSelected = false;
  93. foreach (var GUID in Selection.assetGUIDs)
  94. {
  95. var path = AssetDatabase.GUIDToAssetPath(GUID);
  96. var extension = Path.GetExtension(path).ToLower();
  97. if (k_SupportedImageExtensions.Contains(extension))
  98. {
  99. selectedImagePaths.Add(AssetDatabase.GUIDToAssetPath(GUID));
  100. }
  101. else if (k_UnsupportedImageExtensions.Contains(extension))
  102. {
  103. unsupportedImagesSelected = true;
  104. }
  105. }
  106. return selectedImagePaths;
  107. }
  108. }
  109. }