PointableCanvas.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEngine.UI;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. /// <summary>
  7. /// PointerCanvas allows any IPointable to forward its
  8. /// events onto an associated Canvas via the IPointableCanvas interface
  9. /// Requires a PointableCanvasModule present in the scene.
  10. /// </summary>
  11. public class PointableCanvas : PointableElement, IPointableCanvas
  12. {
  13. [SerializeField]
  14. private Canvas _canvas;
  15. public Canvas Canvas => _canvas;
  16. protected override void Start()
  17. {
  18. base.Start();
  19. Assert.IsNotNull(Canvas);
  20. Assert.IsNotNull(Canvas.GetComponent<GraphicRaycaster>(),
  21. "PointableCanvas requires that the Canvas object has an attached GraphicRaycaster.");
  22. }
  23. private void Register()
  24. {
  25. PointableCanvasModule.RegisterPointableCanvas(this);
  26. }
  27. private void Unregister()
  28. {
  29. PointableCanvasModule.UnregisterPointableCanvas(this);
  30. }
  31. protected override void OnEnable()
  32. {
  33. base.OnEnable();
  34. Register();
  35. }
  36. protected override void OnDisable()
  37. {
  38. Unregister();
  39. base.OnDisable();
  40. }
  41. }
  42. }