VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Canvas.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.EventSystems;
5 
6 using AVR.Core;
7 
8 /// <summary>
9 /// Namespace of the arc-vr-ui package
10 /// </summary>
11 namespace AVR.UI {
12  [RequireComponent(typeof(Canvas))]
13  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_u_i_1_1_a_v_r___canvas.html")]
14  /// <summary>
15  /// Represents a canvas for VR interaction purposes. Allows for interaction with the AVR_UIInteractionProvider.
16  /// </summary>
17  public class AVR_Canvas : AVR_Behaviour, IPointerEnterHandler, IPointerExitHandler
18  {
19  /// <summary>
20  /// If set to false, the UIInteractionprovider will disregard this canvas entirely.
21  /// </summary>
22  public bool isInteractible = true;
23 
24  /// <summary>
25  /// A list of all AVR_Canvases that have at some point been active and not destroyed since
26  /// </summary>
27  /// <value></value>
28  public static List<AVR_Canvas> all_canvases {
29  get { return _all_canvases; }
30  private set { _all_canvases = value; }
31  }
32  private static List<AVR_Canvas> _all_canvases = new List<AVR_Canvas>();
33 
34  /// <summary>
35  /// A list of all AVR_Canvases that the user is currently interacting with. NOTE: This is *not* the same as all canvases that are active in the scene.
36  /// </summary>
37  /// <value></value>
38  public static List<AVR_Canvas> active_canvases {
39  get { return _active_canvases; }
40  private set { _active_canvases = value; }
41  }
42  private static List<AVR_Canvas> _active_canvases = new List<AVR_Canvas>();
43 
44  /// <summary>
45  /// The UnityEngine.Canvas Component this object is attatched to.
46  /// </summary>
47  public Canvas canvas { get; private set; }
48 
49  void Awake() {
50  if(all_canvases==null) all_canvases = new List<AVR_Canvas>();
51  if(active_canvases==null) active_canvases = new List<AVR_Canvas>();
52 
53  all_canvases.Add(this);
54 
55  canvas = GetComponent<Canvas>();
56 
57  if(!canvas) {
58  AVR_DevConsole.cerror("AVR_Canvas does not have a Canvas component!", this);
59  Destroy(this);
60  }
61  }
62 
63  void OnDisable() {
64  if (active_canvases.Contains(this)) active_canvases.Remove(this);
65  }
66 
67  public override void OnDestroy()
68  {
69  if (all_canvases.Contains(this)) all_canvases.Remove(this);
70  if (active_canvases.Contains(this)) active_canvases.Remove(this);
71  }
72 
73  /// <summary>
74  /// Anchors this canvas to the world. (Sets parent to null)
75  /// </summary>
76  public void anchor_to_world()
77  {
78  anchor_to_transform(null);
79  }
80 
81  /// <summary>
82  /// Anchors this canvas to the players HMD. Intended for HUD UI elements.
83  /// </summary>
84  public void anchor_to_player()
85  {
86  this.transform.SetParent(AVR_PlayerRig.Instance.MainCamera.transform, true);
87  AVR_DevConsole.print(gameObject.name + " anchored to player");
88  }
89 
90  /// <summary>
91  /// Anchors the canvas to a given vr controller.
92  /// </summary>
93  /// <param name="controller"> Controller the canvas will be parented to </param>
95  {
96  this.transform.SetParent(controller.transform, true);
97  AVR_DevConsole.print(gameObject.name + " anchored to controller");
98  }
99 
100  /// <summary>
101  /// Anchor this canvas to a given transform. Effectively the same as calling transform.SetParent(...)
102  /// </summary>
103  /// <param name="t">Transform the canvas will be parented to</param>
104  public void anchor_to_transform(Transform t)
105  {
106  if(t==null) {
107  // We need to do this circus each time we parent to world because something doesnt work. I seriously can't figure out what, but either way, performance impact should be close to nothing.
108  Vector3 tmp = this.transform.position;
109  this.transform.SetParent(null, true);
110  transform.position = tmp;
111  }
112  else{
113  this.transform.SetParent(t, true);
114  }
115  }
116 
117  /// <summary>
118  /// Returns the 3d plane this canvas lies in
119  /// </summary>
120  /// <returns>3d plane this canvas lies in</returns>
121  public Plane GetPlane() {
122  Plane canvasPlane = new Plane();
123  canvasPlane.Set3Points(
124  transform.position,
125  transform.position + transform.right,
126  transform.position + transform.up
127  );
128  return canvasPlane;
129  }
130 
131  public void OnPointerEnter(PointerEventData eventData)
132  {
133  if (isInteractible && !active_canvases.Contains(this)) active_canvases.Add(this);
134  }
135  public void OnPointerExit(PointerEventData eventData)
136  {
137  if (active_canvases.Contains(this)) active_canvases.Remove(this);
138  }
139  }
140 }
Monobehaviour but with an added URL button to a documentation page.
void OnPointerExit(PointerEventData eventData)
Definition: AVR_Canvas.cs:135
void OnPointerEnter(PointerEventData eventData)
Definition: AVR_Canvas.cs:131
Manages inputs of a controller such as button presses and track-pad inputs.
override void OnDestroy()
Definition: AVR_Canvas.cs:67
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
void anchor_to_controller(AVR_ControllerInputManager controller)
Anchors the canvas to a given vr controller.
Definition: AVR_Canvas.cs:94
Plane GetPlane()
Returns the 3d plane this canvas lies in
Definition: AVR_Canvas.cs:121
void anchor_to_world()
Anchors this canvas to the world. (Sets parent to null)
Definition: AVR_Canvas.cs:76
void anchor_to_player()
Anchors this canvas to the players HMD. Intended for HUD UI elements.
Definition: AVR_Canvas.cs:84
void anchor_to_transform(Transform t)
Anchor this canvas to a given transform. Effectively the same as calling transform.SetParent(...)
Definition: AVR_Canvas.cs:104
Represents a canvas for VR interaction purposes. Allows for interaction with the AVR_UIInteractionPro...
Definition: AVR_Canvas.cs:17