using System;
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
namespace UnityEditor.XR.ARSubsystems
{
///
/// Editor extensions to the XRReferenceObjectLibrary.
///
public static class XRReferenceObjectLibraryExtensions
{
///
/// Creates a new XRReferenceObject and adds it to the library.
///
/// The XRReferenceObjectLibrary being extended.
/// The index in the library at which the new reference object was created.
/// Thrown if is null.
public static int Add(this XRReferenceObjectLibrary library)
{
if (library == null)
throw new ArgumentNullException("library");
ulong guidLow, guidHigh;
Guid.NewGuid().Decompose(out guidLow, out guidHigh);
library.m_ReferenceObjects.Add(new XRReferenceObject
{
m_Entries = new List(),
m_GuidLow = guidLow,
m_GuidHigh = guidHigh
});
return library.m_ReferenceObjects.Count - 1;
}
///
/// Removes the XRReferenceObject at .
///
/// The XRReferenceObjectLibrary being extended.
/// The index of the XRReferenceObject to remove.
/// Thrown if is null.
/// Thrown if is not between 0 and library.count - 1.
public static void RemoveAt(this XRReferenceObjectLibrary library, int index)
{
if (library == null)
throw new ArgumentNullException("library");
if (index < 0 || index >= library.count)
throw new IndexOutOfRangeException(string.Format("index {0} is out of range [0 - {1}]", index, library.count - 1));
library.m_ReferenceObjects.RemoveAt(index);
}
///
/// Sets the name of the XRReferenceObject at .
///
/// The XRReferenceObjectLibrary being extended.
/// The index of the XRReferenceObject to modify.
/// The new name of the XRReferenceObject.
/// Thrown if is null.
/// Thrown if is not between 0 and library.count - 1.
public static void SetReferenceObjectName(this XRReferenceObjectLibrary library, int index, string name)
{
if (library == null)
throw new ArgumentNullException("library");
if (index < 0 || index >= library.count)
throw new IndexOutOfRangeException(string.Format("index {0} is out of range [0 - {1}]", index, library.count - 1));
var referenceObject = library.m_ReferenceObjects[index];
referenceObject.m_Name = name;
library.m_ReferenceObjects[index] = referenceObject;
}
///
/// Sets the entry for the given of the XRReferenceObject at index .
///
///
/// Each reference object contains a list of "entries", one for each provider (implementation of XRObjectTrackingSubsystem).
/// This method sets the entry for a given type, which is the data that will be used when that provider is active.
///
/// The XRReferenceObjectLibrary being extended.
/// The index of the XRReferenceObject to modify.
/// The type of the being set.
/// The entry to use for the given .
/// Thrown if is null.
/// Thrown if is null.
/// Thrown if is not between 0 and library.count - 1.
/// Thrown if does not derive from XRReferenceObjectEntry.
public static void SetReferenceObjectEntry(this XRReferenceObjectLibrary library, int index, Type type, XRReferenceObjectEntry entry)
{
if (library == null)
throw new ArgumentNullException("library");
if (type == null)
throw new ArgumentNullException("type");
if (!type.IsSubclassOf(typeof(XRReferenceObjectEntry)))
throw new ArgumentException("The type must derive from XRReferenceObjectEntry", "type");
if (index < 0 || index >= library.m_ReferenceObjects.Count)
throw new IndexOutOfRangeException(string.Format("index {0} is out of range [0 - {1}]", index, library.m_ReferenceObjects.Count - 1));
var referenceObject = library.m_ReferenceObjects[index];
for (int i = 0; i < referenceObject.m_Entries.Count; ++i)
{
if (referenceObject.m_Entries[i].GetType() == type)
{
if (entry == null)
{
referenceObject.m_Entries.RemoveAt(i);
}
else
{
referenceObject.m_Entries[i] = entry;
}
return;
}
}
// There isn't an entry for the given type, so add it.
referenceObject.m_Entries.Add(entry);
}
}
}