8 using System.Collections;
9 using System.Reflection;
12 [RequireComponent(typeof(Camera))]
16 private Transform _head;
17 public Transform head {
get {
return _head; } }
18 public Transform offset {
get {
return _head; } }
19 public Transform origin {
get {
return _head.parent; } }
21 public new Camera camera {
get;
private set; }
24 private Transform _ears;
25 public Transform ears {
get {
return _ears; } }
29 return new Ray(_head.position, _head.forward);
32 public bool wireframe =
false;
34 static public float sceneResolutionScale
36 get {
return UnityEngine.XR.XRSettings.eyeTextureResolutionScale; }
37 set { UnityEngine.XR.XRSettings.eyeTextureResolutionScale = value; }
40 #region Enable / Disable
44 SteamVR_Render.Remove(
this);
50 var vr = SteamVR.instance;
70 while (head.childCount > 0)
71 head.GetChild(0).parent = t;
76 head.localPosition = Vector3.zero;
77 head.localRotation = Quaternion.identity;
78 head.localScale = Vector3.one;
79 head.gameObject.SetActive(
false);
86 var e = transform.GetComponentInChildren<
SteamVR_Ears>();
94 SteamVR_Render.Add(
this);
99 #region Functionality to ensure SteamVR_Camera component is always the last component on an object
103 camera = GetComponent<Camera>();
107 static Hashtable values;
109 public void ForceLast()
114 foreach (DictionaryEntry entry
in values)
116 var f = entry.Key as FieldInfo;
117 f.SetValue(
this, entry.Value);
124 var components = GetComponents<Component>();
127 for (
int i = 0; i < components.Length; i++)
130 if (c != null && c !=
this)
136 components = GetComponents<Component>();
138 if (
this != components[components.Length - 1])
141 values =
new Hashtable();
142 var fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
143 foreach (var f
in fields)
144 if (f.IsPublic || f.IsDefined(typeof(SerializeField),
true))
145 values[f] = f.GetValue(
this);
148 DestroyImmediate(
this);
156 #region Expand / Collapse object hierarchy
159 public bool isExpanded {
get {
return head != null && transform.parent == head; } }
161 const string eyeSuffix =
" (eye)";
162 const string earsSuffix =
" (ears)";
163 const string headSuffix =
" (head)";
164 const string originSuffix =
" (origin)";
165 public string baseName {
get {
return name.EndsWith(eyeSuffix) ? name.Substring(0, name.Length - eyeSuffix.Length) : name; } }
172 var _origin = transform.parent;
175 _origin =
new GameObject(name + originSuffix).transform;
176 _origin.localPosition = transform.localPosition;
177 _origin.localRotation = transform.localRotation;
178 _origin.localScale = transform.localScale;
184 head.parent = _origin;
185 head.position = transform.position;
186 head.rotation = transform.rotation;
187 head.localScale = Vector3.one;
191 if (transform.parent != head)
193 transform.parent = head;
194 transform.localPosition = Vector3.zero;
195 transform.localRotation = Quaternion.identity;
196 transform.localScale = Vector3.one;
198 while (transform.childCount > 0)
199 transform.GetChild(0).parent = head;
200 #if !UNITY_2017_2_OR_NEWER
201 var guiLayer = GetComponent<GUILayer>();
202 if (guiLayer != null)
204 DestroyImmediate(guiLayer);
205 head.gameObject.AddComponent<GUILayer>();
208 var audioListener = GetComponent<AudioListener>();
209 if (audioListener != null)
211 DestroyImmediate(audioListener);
212 _ears =
new GameObject(name + earsSuffix, typeof(
SteamVR_Ears)).transform;
214 ears.localPosition = Vector3.zero;
215 ears.localRotation = Quaternion.identity;
216 ears.localScale = Vector3.one;
220 if (!name.EndsWith(eyeSuffix))
224 public void Collapse()
226 transform.parent = null;
229 while (head.childCount > 0)
230 head.GetChild(0).parent = transform;
231 #if !UNITY_2017_2_OR_NEWER
232 var guiLayer = head.GetComponent<GUILayer>();
233 if (guiLayer != null)
235 DestroyImmediate(guiLayer);
236 gameObject.AddComponent<GUILayer>();
241 while (ears.childCount > 0)
242 ears.GetChild(0).parent = transform;
244 DestroyImmediate(ears.gameObject);
247 gameObject.AddComponent(typeof(AudioListener));
253 if (origin.name.EndsWith(originSuffix))
256 var _origin = origin;
257 while (_origin.childCount > 0)
258 _origin.GetChild(0).parent = _origin.parent;
260 DestroyImmediate(_origin.gameObject);
264 transform.parent = origin;
268 DestroyImmediate(head.gameObject);
271 if (name.EndsWith(eyeSuffix))
272 name = name.Substring(0, name.Length - eyeSuffix.Length);