using System;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
namespace UnityEditor.XR.ARSubsystems
{
///
/// Extension methods for the .
///
///
/// At runtime, s are immutable. These
/// Editor-only extension methods let you build and manipulate image libraries
/// in Editor scripts.
///
public static class XRReferenceImageLibraryExtensions
{
///
/// Creates an empty XRReferenceImage and adds it to the library. The new
/// reference image is inserted at the end of the list of reference images.
///
/// The being extended.
/// Thrown if is null.
public static void Add(this XRReferenceImageLibrary library)
{
if (library == null)
throw new ArgumentNullException("library");
library.m_Images.Add(new XRReferenceImage
{
m_SerializedGuid = SerializableGuidUtil.Create(Guid.NewGuid())
});
}
///
/// Set the texture on the reference image.
///
/// The being extended.
/// The reference image index to modify.
/// The texture to set.
/// Whether to store a strong reference to the texture. If true,
/// the texture will be available in the Player. Otherwise, XRReferenceImage.texture will be set to null.
/// Thrown if is null.
/// Thrown if is not between 0 and .count - 1.
public static void SetTexture(this XRReferenceImageLibrary library, int index, Texture2D texture, bool keepTexture)
{
ValidateAndThrow(library, index);
var referenceImage = library.m_Images[index];
referenceImage.m_SerializedTextureGuid = SerializableGuidUtil.Create(GetGuidForTexture(texture));
referenceImage.m_Texture = keepTexture ? texture : null;
library.m_Images[index] = referenceImage;
}
///
/// Sets the XRReferenceImage.specifySize value on the XRReferenceImage at .
/// This value is read-only in the Player; it can only be modified in the Editor.
///
/// The XRReferenceImageLibrary being extended.
/// The index of the reference image within the library to modify.
/// Whether XRReferenceImage.size is specified.
/// Thrown if is null.
/// Thrown if is not between 0 and .count - 1.
public static void SetSpecifySize(this XRReferenceImageLibrary library, int index, bool specifySize)
{
ValidateAndThrow(library, index);
var image = library.m_Images[index];
image.m_SpecifySize = specifySize;
library.m_Images[index] = image;
}
///
/// Sets the XRReferenceImage.size value on the XRReferenceImage at .
/// This value is read-only in the Player; it can only be modified in the Editor.
///
/// The XRReferenceImageLibrary being extended.
/// The index of the reference image within the library to modify.
///
/// Thrown if is null.
/// Thrown if is not between 0 and .count - 1.
public static void SetSize(this XRReferenceImageLibrary library, int index, Vector2 size)
{
ValidateAndThrow(library, index);
var image = library.m_Images[index];
image.m_Size = size;
library.m_Images[index] = image;
}
///
/// Sets the XRReferenceImage.name value on the XRReferenceImage at .
/// This value is read-only in the Player; it can only be modified in the Editor.
///
/// The XRReferenceImageLibrary being extended.
/// The index of the reference image within the library to modify.
///
/// Thrown if is null.
/// Thrown if is not between 0 and .count - 1.
public static void SetName(this XRReferenceImageLibrary library, int index, string name)
{
ValidateAndThrow(library, index);
var image = library.m_Images[index];
image.m_Name = name;
library.m_Images[index] = image;
}
///
/// Removes the at .
///
/// The being extended.
/// The index in the list of images to remove.
/// Thrown if is null.
/// Thrown if is not between 0 and .count - 1.
public static void RemoveAt(this XRReferenceImageLibrary library, int index)
{
ValidateAndThrow(library, index);
library.m_Images.RemoveAt(index);
}
static Guid GetGuidForTexture(Texture2D texture)
{
if (texture == null)
return Guid.Empty;
string guid;
long localId;
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(texture, out guid, out localId))
return new Guid(guid);
return Guid.Empty;
}
static void ValidateAndThrow(XRReferenceImageLibrary library, int index)
{
if (library == null)
throw new ArgumentNullException("library");
if (library.count == 0)
throw new IndexOutOfRangeException("The reference image library is empty; cannot index into it.");
if (index < 0 || index >= library.count)
throw new IndexOutOfRangeException(string.Format("{0} is out of range. 'index' must be between 0 and {1}", index, library.count - 1));
}
}
}