XRReferenceImageLibraryExtensions.cs
7.54 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
namespace UnityEditor.XR.ARSubsystems
{
/// <summary>
/// Extension methods for the <see cref="XRReferenceImageLibrary"/>.
/// </summary>
/// <remarks>
/// At runtime, <see cref="XRReferenceImageLibrary"/>s are immutable. These
/// Editor-only extension methods let you build and manipulate image libraries
/// in Editor scripts.
/// </remarks>
public static class XRReferenceImageLibraryExtensions
{
/// <summary>
/// Creates an empty <c>XRReferenceImage</c> and adds it to the library. The new
/// reference image is inserted at the end of the list of reference images.
/// </summary>
/// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
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())
});
}
/// <summary>
/// Set the texture on the reference image.
/// </summary>
/// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
/// <param name="index">The reference image index to modify.</param>
/// <param name="texture">The texture to set.</param>
/// <param name="keepTexture">Whether to store a strong reference to the texture. If <c>true</c>,
/// the texture will be available in the Player. Otherwise, <c>XRReferenceImage.texture</c> will be set to <c>null</c>.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
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;
}
/// <summary>
/// Sets the <c>XRReferenceImage.specifySize</c> value on the <c>XRReferenceImage</c> at <paramref name="index"/>.
/// This value is read-only in the Player; it can only be modified in the Editor.
/// </summary>
/// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
/// <param name="index">The index of the reference image within the library to modify.</param>
/// <param name="specifySize">Whether <c>XRReferenceImage.size</c> is specified.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
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;
}
/// <summary>
/// Sets the <c>XRReferenceImage.size</c> value on the <c>XRReferenceImage</c> at <paramref name="index"/>.
/// This value is read-only in the Player; it can only be modified in the Editor.
/// </summary>
/// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
/// <param name="index">The index of the reference image within the library to modify.</param>
/// <param name="size"></param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
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;
}
/// <summary>
/// Sets the <c>XRReferenceImage.name</c> value on the <c>XRReferenceImage</c> at <paramref name="index"/>.
/// This value is read-only in the Player; it can only be modified in the Editor.
/// </summary>
/// <param name="library">The <c>XRReferenceImageLibrary</c> being extended.</param>
/// <param name="index">The index of the reference image within the library to modify.</param>
/// <param name="name"></param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
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;
}
/// <summary>
/// Removes the <see cref="XRReferenceImage"/> at <paramref name="index"/>.
/// </summary>
/// <param name="library">The <see cref="XRReferenceImageLibrary"/> being extended.</param>
/// <param name="index">The index in the list of images to remove.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="library"/> is <c>null</c>.</exception>
/// <exception cref="System.IndexOutOfRangeException">Thrown if <paramref name="index"/> is not between 0 and <paramref name="library"/><c>.count - 1</c>.</exception>
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));
}
}
}