SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
7.5 KiB

4 years ago
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.XR.ARSubsystems;
  4. namespace UnityEditor.XR.ARSubsystems
  5. {
  6. /// <summary>
  7. /// Extension methods for the <see cref="XRReferenceImageLibrary"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// At runtime, <see cref="XRReferenceImageLibrary"/>s are immutable. These
  11. /// Editor-only extension methods let you build and manipulate image libraries
  12. /// in Editor scripts.
  13. /// </remarks>
  14. public static class XRReferenceImageLibraryExtensions
  15. {
  16. /// <summary>
  17. /// Creates an empty <c>XRReferenceImage<c/> and adds it to the library. The new
  18. /// reference image is inserted at the end of the list of reference images.
  19. /// </summary>
  20. /// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
  21. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  22. public static void Add(this XRReferenceImageLibrary library)
  23. {
  24. if (library == null)
  25. throw new ArgumentNullException("library");
  26. library.m_Images.Add(new XRReferenceImage
  27. {
  28. m_SerializedGuid = SerializableGuidUtil.Create(Guid.NewGuid())
  29. });
  30. }
  31. /// <summary>
  32. /// Set the texture on the reference image.
  33. /// </summary>
  34. /// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
  35. /// <param name="index">The reference image index to modify.</param>
  36. /// <param name="texture">The texture to set.</param>
  37. /// <param name="keepTexture">Whether to store a strong reference to the texture. If <c>true</c>,
  38. /// the texture will be available in the Player. Otherwise, <c>XRReferenceImage.texture</c> will be set to <c>null</c>.</param>
  39. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  40. /// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
  41. public static void SetTexture(this XRReferenceImageLibrary library, int index, Texture2D texture, bool keepTexture)
  42. {
  43. ValidateAndThrow(library, index);
  44. var referenceImage = library.m_Images[index];
  45. referenceImage.m_SerializedTextureGuid = SerializableGuidUtil.Create(GetGuidForTexture(texture));
  46. referenceImage.m_Texture = keepTexture ? texture : null;
  47. library.m_Images[index] = referenceImage;
  48. }
  49. /// <summary>
  50. /// Sets the <c>XRReferenceImage.specifySize</c> value on the <c><c>XRReferenceImage</c> at <paramref name="index"/>.
  51. /// This value is read-only in the Player; it can only be modified in the Editor.
  52. /// </summary>
  53. /// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
  54. /// <param name="index">The index of the reference image within the library to modify.</param>
  55. /// <param name="specifySize">Whether <c>XRReferenceImage.size</c> is specified.</param>
  56. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  57. /// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
  58. public static void SetSpecifySize(this XRReferenceImageLibrary library, int index, bool specifySize)
  59. {
  60. ValidateAndThrow(library, index);
  61. var image = library.m_Images[index];
  62. image.m_SpecifySize = specifySize;
  63. library.m_Images[index] = image;
  64. }
  65. /// <summary>
  66. /// Sets the <c>XRReferenceImage.size</c> value on the <c><c>XRReferenceImage</c> at <paramref name="index"/>.
  67. /// This value is read-only in the Player; it can only be modified in the Editor.
  68. /// </summary>
  69. /// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
  70. /// <param name="index">The index of the reference image within the library to modify.</param>
  71. /// <param name="size"></param>
  72. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  73. /// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
  74. public static void SetSize(this XRReferenceImageLibrary library, int index, Vector2 size)
  75. {
  76. ValidateAndThrow(library, index);
  77. var image = library.m_Images[index];
  78. image.m_Size = size;
  79. library.m_Images[index] = image;
  80. }
  81. /// <summary>
  82. /// Sets the <c>XRReferenceImage.name</c> value on the <c><c>XRReferenceImage</c> at <paramref name="index"/>.
  83. /// This value is read-only in the Player; it can only be modified in the Editor.
  84. /// </summary>
  85. /// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
  86. /// <param name="index">The index of the reference image within the library to modify.</param>
  87. /// <param name="name"></param>
  88. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  89. /// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
  90. public static void SetName(this XRReferenceImageLibrary library, int index, string name)
  91. {
  92. ValidateAndThrow(library, index);
  93. var image = library.m_Images[index];
  94. image.m_Name = name;
  95. library.m_Images[index] = image;
  96. }
  97. /// <summary>
  98. /// Removes the <see cref="XRReferenceImage"/> at <paramref name="index"/>.
  99. /// </summary>
  100. /// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
  101. /// <param name="index">The index in the list of images to remove.</param>
  102. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
  103. /// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
  104. public static void RemoveAt(this XRReferenceImageLibrary library, int index)
  105. {
  106. ValidateAndThrow(library, index);
  107. library.m_Images.RemoveAt(index);
  108. }
  109. static Guid GetGuidForTexture(Texture2D texture)
  110. {
  111. if (texture == null)
  112. return Guid.Empty;
  113. string guid;
  114. long localId;
  115. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(texture, out guid, out localId))
  116. return new Guid(guid);
  117. return Guid.Empty;
  118. }
  119. static void ValidateAndThrow(XRReferenceImageLibrary library, int index)
  120. {
  121. if (library == null)
  122. throw new ArgumentNullException("library");
  123. if (library.count == 0)
  124. throw new IndexOutOfRangeException("The reference image library is empty; cannot index into it.");
  125. if (index < 0 || index >= library.count)
  126. throw new IndexOutOfRangeException(string.Format("{0} is out of range. 'index' must be between 0 and {1}", index, library.count - 1));
  127. }
  128. }
  129. }