XRReferenceObjectLibraryExtensions.cs
6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
namespace UnityEditor.XR.ARSubsystems
{
/// <summary>
/// Editor extensions to the <c>XRReferenceObjectLibrary</c>.
/// </summary>
public static class XRReferenceObjectLibraryExtensions
{
/// <summary>
/// Creates a new <c>XRReferenceObject</c> and adds it to the library.
/// </summary>
/// <param name="library">The <c>XRReferenceObjectLibrary</c> being extended.</param>
/// <returns>The index in the library at which the new reference object was created.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is null.</exception>
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<XRReferenceObjectEntry>(),
m_GuidLow = guidLow,
m_GuidHigh = guidHigh
});
return library.m_ReferenceObjects.Count - 1;
}
/// <summary>
/// Removes the <c>XRReferenceObject</c> at <paramref name="index"/>.
/// </summary>
/// <param name="library">The <c>XRReferenceObjectLibrary</c> being extended.</param>
/// <param name="index">The index of the <c>XRReferenceObject</c> to remove.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is null.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <c>library.count - 1</c>.</exception>
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);
}
/// <summary>
/// Sets the name of the <c>XRReferenceObject</c> at <paramref name="index"/>.
/// </summary>
/// <param name="library">The <c>XRReferenceObjectLibrary</c> being extended.</param>
/// <param name="index">The index of the <c>XRReferenceObject</c> to modify.</param>
/// <param name="name">The new name of the <c>XRReferenceObject</c>.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is null.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <c>library.count - 1</c>.</exception>
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;
}
/// <summary>
/// Sets the entry for the given <paramref name="type"/> of the <c>XRReferenceObject</c> at index <paramref name="index"/>.
/// </summary>
/// <remarks>
/// Each reference object contains a list of "entries", one for each provider (implementation of <c>XRObjectTrackingSubsystem</c>).
/// This method sets the entry for a given type, which is the data that will be used when that provider is active.
/// </remarks>
/// <param name="library">The <c>XRReferenceObjectLibrary</c> being extended.</param>
/// <param name="index">The index of the <c>XRReferenceObject</c> to modify.</param>
/// <param name="type">The type of the <see cref="XRReferenceObjectEntry"/> being set.</param>
/// <param name="entry">The entry to use for the given <paramref name="type"/>.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is null.</exception>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="type"/> is null.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <c>library.count - 1</c>.</exception>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="type"/> does not derive from <c>XRReferenceObjectEntry</c>.</exception>
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);
}
}
}